简体   繁体   English

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

[英]Google App Script - Google Spreadsheets Move Row based on cell value efficiently

I am trying to move rows based on cell values, the function I have written works. 我正在尝试根据单元格值(我编写的函数)移动行。 However when there are more than 24 rows to move the script times out. 但是,当要移动的行超过24行时,脚本将超时。 Is there any way to make the loop run faster or more efficiently, as it will always time out. 有什么方法可以使循环更快或更有效地运行,因为它总是会超时。

The script reads the cell value of column 7(G) and if it matches one of the sales persons names it copies it over to the sheet that has their name. 该脚本读取第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); } } } 

Here is a copy of the sheet with the script. 这是带有脚本的工作表副本。 The script can be run from the script editor. 可以从脚本编辑器运行脚本。

Google Sheet Link Google表格链接

Thank you 谢谢

I have optimized the below code. 我优化了以下代码。 Now, the total runtime 0.864 seconds. 现在,总运行时间为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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google App Script:根据列中的值将行(仅行中选定的列)移动到另一个选项卡 - Google App Script: move row (only selected columns within the row) to another tab based on value in column 如果该行中的单元格值为 0,则删除 Google 电子表格中的一行 - Delete a row in Google Spreadsheets if value of cell in said row is 0 Google表格:根据单元格值将一行数据移动到另一张表格 - Google Sheets: Move a row of data to another sheet based on cell value Apps 脚本 Google 表格 - 根据单元格值移动行 - Apps Scripts Google Sheets - Move Row based on Cell value 根据单元格值滚动一行到屏幕顶部:Google Sheet Script - Scroll a row to the top of the screen based on cell value: Google Sheet Script 编辑谷歌表格应用脚本中过滤行中的单元格值 - Edit cell value from filtered row in app script for google sheets 谷歌应用程序脚本返回单元格中的行值 - google apps script return row value in a cell 如何根据单元格中的值将一行从一个Google电子表格移动到另一个? - How to move a row from one google spreadsheet to another based on a value in a cell? 在谷歌应用脚​​本中阅读多个电子表格 - Reading multiple spreadsheets in google app script Google表格脚本:根据单元格值设置行格式-尝试优化脚本 - Google Sheets Script: Format Row Based on Cell Value - Trying to Optimize Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM