Google App脚本在删除列时超时

时间:2019-05-09 15:37:28

标签: google-apps-script google-sheets

我有一个脚本,该脚本可删除日期过去的行,但是随着电子表格的开始超时而删除。据我了解,我们对超时设置了6分钟的硬性限制,因此有人可以建议如何使此速度更快

function approveRequests() {

  // Initialising
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //var scheduleSheet = ss.getSheetByName("Combined");
  var sheet = SpreadsheetApp.getActiveSheet();


  var pastSheet = ss.getSheetByName("Left");
  var lastColumn = ss.getLastColumn();

  var allsheets = ss.getSheets();
  for(var s in allsheets){
    var sheet = allsheets[s];

    // Stop iteration execution if the condition is meet.
    if(
       (sheet.getName() == "ABCDEF") || 
       (sheet.getName() == "ABCDEF") || 
       (sheet.getName() == "ABCDEF") || 
       (sheet.getName() == "ABCDEF") || 
       (sheet.getName() == "ABCDEF") ||
       (sheet.getName() == "ABCDEF") ||
       (sheet.getName() == "ABCDEF") || 
       (sheet.getName() == "ABCDEF") || 
       (sheet.getName() == "ABCDEF") 
      ) continue;

     for(var i = sheet.getLastRow(); i > 0; i--){

    // Check if the value is a valid date
       // 15 is for Col O
    var dateCell = sheet.getRange(i, 15).getValue();
    if(isValidDate(dateCell)){
      var today = new Date();
      var test = new Date(dateCell);

      // If the value is a valid date and is a past date, we remove it from the sheet to paste on the other sheet
      if(test < today){

        var rangeToMove = sheet.getRange(i, 1, 1, sheet.getLastColumn()).getValues();
        pastSheet.getRange(pastSheet.getLastRow() + 1, 1, 1, sheet.getLastColumn()).setValues(rangeToMove);
        sheet.deleteRow(i);

      }
    }
     }

  } // end of loop


}

// Check is a valid date
function isValidDate(value) {
  var dateWrapper = new Date(value);
  return !isNaN(dateWrapper.getDate());
}

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

function approveRequests() {
  var excl=['ABCDEF','GHIJKL'];//excluded sheeets
  var ss=SpreadsheetApp.getActive();
  var pastSheet=ss.getSheetByName("Left");
  var shts=ss.getSheets();
  var tdv=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()).valueOf();
  for(var i=0;i<shts.length;i++) {
    var sh=shts[i];
    var rg=sh.getDataRange();
    var vA=rg.getValues();
    var d=0;//sh delete counter
    if(excl.indexOf(sh.getName())>-1) {continue;}
    for(var i=1;i<vA.length;i++) {//assumed one header row
    if(isDate(vA[i][14]) && new Date(vA[i][14]).valueOf()<tdv) {
      pastSheet.appendRow(vA[i]);
      sh.deleteRow(i-d+1);
      d++;
    }                            }
  } 
}

// Check is a valid date
function isDate(date){
  return(Object.prototype.toString.call(date) === '[object Date]');
}

感谢@Christoph:return(Object.prototype.toString.call(date) === '[object Date]');

Best Practices