我有两个电子表格。我想将spreadsheet1
的C列与spreadsheet2
的A列相匹配。这两个电子表格都有超过8000个记录。由于记录数量庞大,我的脚本不断出现exceeded maximum execution time
错误。这是脚本
function compare() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetList1=ss.getSheetByName("spreadsheet1");
var sheetList2=ss.getSheetByName("spreadsheet2");
var sheet1Data=sheetList1.getRange(2,3,sheetList1.getLastRow(),1).getValues();
var sheet2Data=sheetList2.getRange(1,1,sheetList2.getLastRow(),1).getValues();
for (i in sheet2Data){
var row = 2;
for (j in sheet1Data){
if (sheet1Data[j][0]==sheet2Data[i][0]){
sheetList1.getRange("A"+row).setValue('Inactive');
}
row++;
}
}
}
优化此脚本的任何建议。或者如何处理这个错误? 在此先感谢:)
修改
感谢您的精彩回复。有一个问题。如果我在newSheet1Data
语句之前推送if
数组中的数据,那么它会写Inactive
两次。即如果有两行,则将inactive
写入四行。类似
newSheet1Data.push(sheet1Data[j]);
if (sheet1Data[j][2]==sheet2Data[i][0]){
newSheet1Data[j][0]='Inactive';
}
如果我在if
语句中推送数据并且没有匹配,则它找不到行并给出此错误TypeError: Cannot set property "0.0" of undefined to "Inactive"
。喜欢
if (sheet1Data[j][0]==sheet2Data[i][0]){
newSheet1Data.push(sheet1Data[j]);
newSheet1Data[j][0]='Inactive';
}
答案 0 :(得分:2)
您应该避免在循环中调用电子表格API,尤其是当您有大量数据时。
这个想法是只播放数组并在结束后将结果写回来。 下面的代码执行它(基于您的代码,如果col C sheet1中的数据与sheet2 col A中的数据相同,则在sheet1 col A中写入一个字符串)。 我希望我没有犯错,但我没有机会测试我的代码...它可能需要一些调试; - )
function compare() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetList1=ss.getSheetByName("Sheet1");
var sheetList2=ss.getSheetByName("Sheet2");
var sheet1Data=sheetList1.getDataRange().getValues();// get the whole sheet in an array
var sheet2Data=sheetList2.getRange(1,1,sheetList2.getLastRow(),1).getValues();
var newSheet1Data = [] ; // create a new array to collect data
for (i=0;i<sheet1Data.length;++i){
for (j=0;j<sheet2Data.length;++j){
if(i!=j){continue};
newSheet1Data.push(sheet1Data[i]); // add the full row to target array
if (sheet1Data[i][2]==sheet2Data[j][0]){
newSheet1Data[i][0]='Inactive';//if condition is true change column A
break
}
}
}
newSheet1Data.shift();// remove first row (probably headers ?)
sheetList1.getRange(2,1,newSheet1Data.length,newSheet1Data[0].length).setValues(newSheet1Data); // write back to sheet1 in one batch
}
function compare() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetList1=ss.getSheetByName("Sheet1");
var sheetList2=ss.getSheetByName("Sheet2");
var sheet1Data=sheetList1.getDataRange().getValues();// get the whole sheet in an array
var sheet2Data=sheetList2.getRange(1,1,sheetList2.getLastRow(),1).getValues();
var newSheet1Data = [] ; // create a new array to collect data
for (i=0;i<sheet1Data.length;++i){
newSheet1Data.push(sheet1Data[i]); // add the full row to target array
for (j=0;j<sheet2Data.length;++j){
if (sheet1Data[i][2]==sheet2Data[j][0]){
newSheet1Data[i][0]='Inactive';//if condition is true change column A
break;// don't continue after condition was true, this will speed up the process
}
}
}
newSheet1Data.shift();// remove first row (probably headers ?)
sheetList1.getRange(2,1,newSheet1Data.length,newSheet1Data[0].length).setValues(newSheet1Data); // write back to sheet1 in one batch
}