我是尝试解决此问题的代码的新手。我有2张(A& B)。每周一次,应该捕获来自表A的数据,并且需要根据一个公共栏(A栏)将其粘贴在表B中
工作表A 工作表B
我的代码:
var ssA = SpreadsheetApp.openById('12dmkCZH4V-2g1brKcGen3iD958_39MoBP77ebMtRnlw');
var sheetA = ssA.getSheetByName('Sheet2');
Logger.log(sheetA.getLastRow());
var dataA = sheetA.getRange('A2:A').getValues();
var dataA_values = sheetA.getRange('M2:M').getValues();
Logger.log(dataA);
// gets spreadsheet B and the range of data
var ssB = SpreadsheetApp.openById('1isb3NJJHCyWKOlshHkrOkd_Q_j88LUZu9jux5VbQDwg');
var sheetB = ssB.getSheetByName('Sheet5');
var dataB = sheetB.getRange('A2:A').getValues();
Logger.log(sheetB.getLastRow());
Logger.log(dataB);
// loops through column A of spreadsheet A & B and compares
for(var i = 0; i < dataA.length; i++) {
Logger.log("A-" +dataA[i]);
for(var j = 0; j < dataB.length; j++)
{
Logger.log("B-" + dataB[j]);
// checks to see if ith value in 2nd row is the same
if (dataA[i].toString() == dataB[j].toString()){
var value = dataA_values[i].toString();
Logger.log("value:"+ value);
// used i+1 because index of range is 1, while index of the data array is 0
// sheetB.getRange(j+1, 3).setValue(new Date()); //set current date for the column as header
sheetB.getRange(j+2, 3).setValue(value);
} // end if
} // end i
}
在比较系统日期和表B中的日期时需要一些帮助,并在匹配数据时复制内容。我有一个日期比较的示例代码,但卡住了
function onDate()
{
var d = new Date();
Logger.log(d);
var date1 = d.getDate();
Logger.log("date1...." + date1);
var month1 = d.getMonth()+1;
Logger.log("month1...." + month1);
var year1 = d.getFullYear();
Logger.log("year1...." + year1);
// var timeStamp = d.getTime();
// Logger.log(timeStamp);
// var currentTime = d.toLocaleTimeString();
// Logger.log(currentTime);
// Logger.log(Utilities.formatDate(d, 'GMT-5', 'dd MMM yyyy'));
var ssB = SpreadsheetApp.openById('1isb3NJJHCyWKOlshHkrOkd_Q_j88LUZu9jux5VbQDwg');
var sheetB = ssB.getSheetByName('Sheet2');
var dateB = sheetB.getRange('D1:D1').getValues();
Logger.log(typeof(dateB));
var dB = new Date(dateB)
Logger.log(dB);
var date2 = dB.getDate();
Logger.log("date2...." + date2);
var month2 = dB.getMonth()+1;
Logger.log("month2...." + month2);
var year2 = dB.getFullYear();
Logger.log("year2...." + year2);
// Logger.log(Utilities.formatDate(dB, 'GMT-5', 'dd MMM yyyy'));
// var dB = dateB.toString();
// Logger.log(dateB);
// Logger.log(dB);
// Logger.log(Utilities.formatDate(dB, 'GMT-5', 'dd MMM yyyy'));
var row = 1;
// var sheetDate = sheetB.getRange(row,getColIndexByName()).getValue();
if (year1==year2 && month1==month2 && date1==date2)
{
return true;
}
return false;
}
答案 0 :(得分:0)
我使用容器绑定脚本测试了这一点,并且使用了Sheet A&amp; B在同一电子表格中,因此您需要更新两个函数中的ss
值以及工作表名称。
function copyData() {
var ss = SpreadsheetApp.getActive();
var sheetB = ss.getSheetByName("Sheet B");
var originalValues = getOriginalValues();
var bCustomers = sheetB.getRange("A2:A").getValues();
var printArray = []; // Faster to print all values at once
for (var i=0; i<bCustomers.length; i++) {
printArray.push([originalValues[bCustomers[i][0]] || ""]);
}
var dates = sheetB.getRange(1, 1, 1, sheetB.getLastColumn()).getValues();
var today = new Date();
today.setHours(0,0,0,0); // Set hours to 0 so that we can compare just the date value
// In this for loop, we will iterate through all of the date header values.
// If we find today's date, then we will print the values in that row.
for (var i=0; i<dates[0].length; i++) {
var columnDate = new Date(dates[0][i]);
columnDate.setHours(0,0,0,0); // Set hours to 0 so that we can compare just the date value
if (columnDate.valueOf() === today.valueOf()) {
sheetB.getRange(2, i+1, printArray.length, 1).setValues(printArray);
break; // No need to look any further; we're done!
}
}
}
function getOriginalValues() {
var ss = SpreadsheetApp.getActive();
var sheetA = ss.getSheetByName("Sheet A");
var originalValues = {}; // Using an object means that searching for the correct value later will be much faster
var customers = sheetA.getRange("A2:A").getValues();
var amounts = sheetA.getRange("M2:M").getValues();
for (var i=0; i<customers.length; i++) {
if (customers[i] == "") {
/**
* This can be optimized depending on your data.
* In the current implementation, I'm assuming that once an empty value
* is found in SheetA,ColumnA, then there is no more data. If there is the possibility
* of an empty row in the middle of the data, then use `continue` instead.
*
* You could also adjust the `getRange()` method to only get the range where
* data exists by using a `getLastRow()` method, but I find using the A1 notation to be
* more easily understood.
*/
break;
}
originalValues[customers[i][0]] = amounts[i][0];
}
return originalValues;
}
您可以每天set up a trigger运行,或设置每周触发器以每周运行copyData()
功能。 (看起来你希望它在星期二运行。)