如何在double循环中减少对sheet.getRange的调用并减少执行时间

时间:2019-04-23 19:09:16

标签: javascript google-apps-script google-sheets spreadsheet

由于执行超时,我想减少以下函数中对sheet.getRange的调用。

我尝试重新排列循环并将数据解析为双精度数组,但是电子表格的大小每天都在变化,我只需要按名称引用列即可。

function runDuplicateRemover() {
 var sheet= SpreadsheetApp.getActive().getSheetByName('Sheet 1');
 var rangeData = sheet.getDataRange();
 var lastRow = rangeData.getLastRow();
 var Cdata = sheet.getDataRange().getValues();
  // here I am accessing the column which is used to find duplicates
 var colCRM = Cdata[0].indexOf("CRM ID")+1;
 var arrayOfDuplicates = [];

  for(i=1; i<lastRow; i++){
    var cellToCompare =  sheet.getRange(i+1,colCRM);
// I am just changing all the colors to see the execution
    cellToCompare.setBackground("#88b4fc");
    var crmToCompare =cellToCompare.getValue();
   //checks to see that this value is not already contained in the rows to delete
   if (!cellToCompare.isBlank() && (arrayOfDuplicates.indexOf(i)+1)==0 ){
       arrayOfDuplicates.push(i);
       cellToCompare.setBackground("#f9d9f9");

   for (j = i+1; j<lastRow; j++) {
      var cellCurrent = sheet.getRange(j+1,colCRM);
      cellCurrent.setBackground("#f2fc88");
      var crmCurrent = cellCurrent.getValue();   
      if (crmToCompare == crmCurrent) {

           arrayOfDuplicates.push(j);
           cellCurrent.setBackground("#fc92f1");

          }  
        }

       //pops last value since that's the only one I want to keep
      sheet.getRange(arrayOfDuplicates.pop()+1,colCRM).setBackground("#dbf7d4");

      }  
    }



   for (t = arrayOfDuplicates.length-1; t>=0; t--) {
      sheet.deleteRow(arrayOfDuplicates[t]+1);
    }
  }

我想减少对sheet.getRange的调用,但是我不知道如何删除行,然后将数据返回到工作表而又不会弄乱所有列的顺序。

0 个答案:

没有答案