我正在自学应用脚本,并创建了一个简单的函数来在出现错误时删除行。这是代码:
//GLOBALS
var SS = SpreadsheetApp.openById("the_sheet_ID"); //just a placeholder because I didn't want to post the real ID
var SHEET = SS.getSheetByName("300x250");
var RANGE = SHEET.getDataRange();
var DELETE_VAL = "#N/A";
var COL_TO_SEARCH = 2; // The column to search for the DELETE_VAL (Zero is first)
function deleteEachRow(){
var rangeVals = RANGE.getValues();
//Reverse the 'for' loop.
for(var i = rangeVals.length-1; i >= 0; i--){
if(rangeVals[i][COL_TO_SEARCH] === DELETE_VAL){
SHEET.deleteRow(i+1);
};
};
};
我已经收到来自应用脚本的电子邮件,告诉我发生服务器错误。以下是电子邮件的副本:
您的脚本Deleting_Rows最近未能完成成功。故障摘要如下所示。配置该脚本的触发器,或更改您的接收设置以后的失败通知,请单击此处。
4/16/20 1:27 AM deleteEachRow很抱歉,发生服务器错误。请稍等,然后重试。基于时间的4/16/20 1:27 AM
真诚地,
Google Apps脚本
该函数定时运行每分钟,因此通常会自行修复。我已经看到此问题是由于已删除功能的触发器所致,或者是单击此处的链接已损坏,但我的功能并未被删除,并且该链接有效。如果错误与我的代码或Google的服务器有关,则我无法找到任何有关导致该错误的原因,如何解决该错误的信息。任何人所拥有的任何信息或见解都将非常有帮助!
由于这是服务器的零星错误,您可以通过使用try / catch语句捕获错误来解决该问题,如果出现错误,请在使用sleep函数稍等片刻后重试执行代码这个:
function deleteEachRow(){
var i= 0;
while(i < 3) {
try {
//GLOBALS
var SS = SpreadsheetApp.openById("1e-JtL0KacshxjWMRSIwwCzHeg0oj7OtzIw_-m_VV8HQ"); //just a placeholder because I didn't want to post the real ID
var SHEET = SS.getSheetByName("300x250");
var RANGE = SHEET.getDataRange();
var DELETE_VAL = "#N/A";
var COL_TO_SEARCH = 2; // The column to search for the DELETE_VAL (Zero is first)
var rangeVals = RANGE.getValues();
//Reverse the 'for' loop.
for(var i = rangeVals.length-1; i >= 0; i--){
if(rangeVals[i][COL_TO_SEARCH] === DELETE_VAL){
SHEET.deleteRow(i+1);
};
};
// If it gets here, there were no errors
break;
}
catch(e) {
sleep(5000);
i++;
}
}
};