Google App脚本 - Google Spreadsheets有效地根据单元格值移动行

时间:2017-10-25 12:50:02

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

我正在尝试根据单元格值移动行,我编写的函数有效。但是,当有超过24行移动脚本超时时。有没有办法让循环运行更快或更有效,因为它总是超时。

该脚本读取第7列(G)的单元格值,如果它与其中一个销售人员名称匹配,则将其复制到具有其名称的工作表。



function CopyDataToNewFile() {
    
  // How Many Columns over to copy
  var columsCopyCount  = 11; // A=1 B=2 C=3 ....
  
  // What Column to Monitor
  var columnsToMonitor  = 7; // A=1 B=2 C=3 ....MONITORS SALES PERSON NAME
  
   //TARGET SPREAD SHEETS
  var salesPerson1  = "Lorna";
  var salesPerson2  = "Sarah";
  var salesPerson3  = "Mark";
 
  //SOURCE SPREAD SHEET
  var ss = SpreadsheetApp.openById('1S3F0Dekyda4g77j_a150Obz0IDNKtWMU2WlGDSXdcD4');
  var sourceSpreadSheetSheetID = ss.getSheetByName("importdata");
  var sourceSpreadSheetSheetID1 = ss.getSheetByName(salesPerson1);
  var sourceSpreadSheetSheetID2 = ss.getSheetByName(salesPerson2);
  var sourceSpreadSheetSheetID3 = ss.getSheetByName(salesPerson3);
  
  var numRows = sourceSpreadSheetSheetID.getLastRow()+1;
    
  
  for(var i = 2; i < numRows; i++)
  {
  var r = sourceSpreadSheetSheetID.getRange(i, columnsToMonitor);

  var rValue = r.getValue();
    
  if(rValue == salesPerson1) {
      var targetRange = sourceSpreadSheetSheetID1.getRange(sourceSpreadSheetSheetID1.getLastRow() + 1, 1);
      sourceSpreadSheetSheetID.getRange(i, 1, 1, columsCopyCount).copyTo(targetRange), {contentsOnly: true};
      //sourceSpreadSheetSheetID.deleteRow(i);
    
    }else if (rValue == salesPerson2) {
        var targetRange = sourceSpreadSheetSheetID2.getRange(sourceSpreadSheetSheetID2.getLastRow() + 1, 1);
      sourceSpreadSheetSheetID.getRange(i, 1, 1, columsCopyCount).copyTo(targetRange), {contentsOnly: true};
      //sourceSpreadSheetSheetID.deleteRow(i);
      
    }else if (rValue == salesPerson3) {
       var targetRange = sourceSpreadSheetSheetID3.getRange(sourceSpreadSheetSheetID3.getLastRow() + 1, 1);
      sourceSpreadSheetSheetID.getRange(i, 1, 1, columsCopyCount).copyTo(targetRange), {contentsOnly: true};
      //sourceSpreadSheetSheetID.deleteRow(i);
    
    }else {//Fail Safe
      var targetRange = sourceSpreadSheetSheetID1.getRange(sourceSpreadSheetSheetID1.getLastRow() + 1, 1);
      sourceSpreadSheetSheetID.getRange(i, 1, 1, columsCopyCount).copyTo(targetRange), {contentsOnly: true};
      //sourceSpreadSheetSheetID.deleteRow(i);
    }
  
  }
  
}
&#13;
&#13;
&#13;

以下是带有脚本的工作表的副本。该脚本可以从脚本编辑器运行。

Google Sheet Link

谢谢

1 个答案:

答案 0 :(得分:1)

我优化了以下代码。现在,总运行时间为0.864秒。

function CopyDataToNewFile() {

    // How Many Columns over to copy
    var columsCopyCount = 11; // A=1 B=2 C=3 ....

    // What Column to Monitor
    var columnsToMonitor = 7; // A=1 B=2 C=3 ....MONITORS SALES PERSON NAME

    //TARGET SPREAD SHEETS
    var salesPerson1 = "Lorna";
    var salesPerson2 = "Sarah";
    var salesPerson3 = "Mark";

    //SOURCE SPREAD SHEET
    var ss = SpreadsheetApp.openById('164nb8HbOPX8204KFlrF0BZeuZ-rCjoxojYT5jvEIuNU');
    var sourceSpreadSheetSheetID = ss.getSheetByName("importdata");
    var sourceSpreadSheetSheetID1 = ss.getSheetByName(salesPerson1);
    var sourceSpreadSheetSheetID2 = ss.getSheetByName(salesPerson2);
    var sourceSpreadSheetSheetID3 = ss.getSheetByName(salesPerson3);

    var data = sourceSpreadSheetSheetID.getRange(2, 1, sourceSpreadSheetSheetID.getLastRow() - 1, sourceSpreadSheetSheetID.getLastColumn()).getValues();

    var lorna = [];
    var sarah=[];
    var mark=[];


    for (var i = 0; i < data.length; i++) {

        var rValue = data[i][6];

        if (rValue == salesPerson1) {
            lorna.push(data[i]);
        } else if (rValue == salesPerson2) {
            sarah.push(data[i]);
        } else if (rValue == salesPerson3) {
            mark.push(data[i]);
        } else { //Fail Safe
            lorna.push(data[i]);
        }
    }

    if(lorna.length > 0){
      sourceSpreadSheetSheetID1.getRange(sourceSpreadSheetSheetID1.getLastRow() + 1, 1, lorna.length, lorna[0].length).setValues(lorna);
    }

    if(sarah.length > 0){
       sourceSpreadSheetSheetID2.getRange(sourceSpreadSheetSheetID2.getLastRow() + 1, 1, sarah.length, sarah[0].length).setValues(sarah);
    }

    if(mark.length > 0){
      sourceSpreadSheetSheetID3.getRange(sourceSpreadSheetSheetID3.getLastRow() + 1, 1, mark.length, mark[0].length).setValues(mark);
    }

    //Will delete the rows of importdata once the data is copided to other sheets
   sourceSpreadSheetSheetID.deleteRows(2, sourceSpreadSheetSheetID.getLastRow() - 1);
}