简体   繁体   English

Google Apps 脚本占用了太多时间,有没有办法减少运行时间?

[英]Google Apps Script is taking too much time, is there a way to reduce the runtime?

function dataManp() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("My-Sheet-1");
  var pasteSheet = ss.getSheetByName("My-Sheet-2");
  var clearContentRange = pasteSheet.getRange("A1:Z100");
  clearContentRange.clearContent();

  var source = copySheet.getRange("a1:f100");

  var destination = pasteSheet.getRange("a1:f100");

  source.copyTo(destination, {formatOnly:true , contentsOnly:true});
  source.copyTo(destination,SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS,false);

  var rows = pasteSheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  var rowsDeleted = 0;
  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    if (row[3] == '') {
      var deleteRowNum = (parseInt(i)+1) - rowsDeleted
      pasteSheet.deleteRow(deleteRowNum);
     // temp_array[i] = i
      rowsDeleted++;
    }
  }
  pasteSheet.deleteColumn(2)
}

Hi,你好,

I have written the following script to perform the following operations,我编写了以下脚本来执行以下操作,

  1. Copy data from My-Sheet-1 and paste to My-Sheet-2My-Sheet-1复制数据并粘贴到My-Sheet-2
  2. Deletion of rows that corresponds to empty cells in column 3.删除与第 3 列中的空单元格对应的行。
  3. After that deletion of column 2在删除第 2 列之后

Rigthnow, the My-Sheet-1 contains only 60 rows and 20, the script is taking approximately 7 secs to complete. Rigthnow, My-Sheet-1仅包含 60 行和 20 行,脚本大约需要 7 秒才能完成。 However in future the number of rows may extend to some 1000. Is there a way to optimize the above code, so that it takes less time to complete .但是将来行数可能会扩展到大约 1000 行。有没有办法优化上述代码,从而减少完成的时间

My observation is that, copy and pasting the data takes just milli secs.我的观察是,复制和粘贴数据只需几毫秒。 The major time consuming part are the operations, I am performing in the pasteSheet after pasting it.主要耗时的部分是操作,我在粘贴后在pasteSheet中执行。 It may be helpful, if we can copy My-Sheet-1 to a temporary variable (copy everything including the formulas, format specifications, values, text etc..) and perform all operations in the temporary variable and then paste everything in the temporary variable to the desired target sheet.如果我们可以将My-Sheet-1复制到临时变量(复制所有内容,包括公式、格式规范、值、文本等)并在临时变量中执行所有操作,然后将所有内容粘贴到临时变量中,这可能会有所帮助可变到所需的目标表。 But, I don't know, how to copy everything in a sheet to a temporary variable, also, I am not sure, whether this will reduce the time or not.但是,我不知道如何将工作表中的所有内容复制到临时变量中,而且我不确定这是否会减少时间。 I would be glad, if I can get some help on this as well (ie copying everything in a sheet to a temporary variable, perfrom operations on the variables and then paste data in the variable to a new sheet)如果我也能在这方面得到一些帮助,我会很高兴(即将工作表中的所有内容复制到临时变量,对变量执行操作,然后将变量中的数据粘贴到新工作表)

Thank you谢谢

Edit - 1编辑 - 1

Would like to add that, My-Sheet-1 contains mixed data (ie numerics, color formatted text, formulas in some cells etc)想补充一点, My-Sheet-1包含混合数据(即数字、颜色格式的文本、某些单元格中的公式等)

Explanation:解释:

deleteRow() takes some time per execution, so it's not recommended to use on hundreds of rows in a loop. deleteRow()每次执行需要一些时间,因此不建议在循环中使用数百行。

Simple answer would be:简单的答案是:

  1. Make a 2D array for Sheet1 using getValues() .使用getValues()为 Sheet1 创建一个二维数组。
  2. Delete / filter out array elements depending if row 2 is blank.删除/过滤出数组元素,具体取决于第2行是否为空白。
  3. Use setValues() to write the filtered array into Sheet2.使用setValues()将过滤后的数组写入 Sheet2。

Sample Code:示例代码:

function dataManp() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("My-Sheet-1");
  var pasteSheet = ss.getSheetByName("My-Sheet-2");
  var lr = copySheet.getLastRow();
  var clearContentRange = pasteSheet.getRange(1,1,lr,26);
  clearContentRange.clearContent();

  var source = copySheet.getRange(1,1,lr,6);
  var destination = pasteSheet.getRange(1,1,lr,6);

  source.copyTo(destination, {formatOnly:true , contentsOnly:true});
  source.copyTo(destination,SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS,false);
  destination.clearContent();

  var values = source.getValues();

  var temp_array = [];
  for (var i = 0; i < lr; i++) {
    var rowValue = values[i];
    if (rowValue[2] != '') {
      temp_array.push(rowValue);
    }
  }

  var newDest = pasteSheet.getRange(1,1,temp_array.length,6)
  newDest.setValues(temp_array);
  pasteSheet.deleteColumn(2);
}

One caveat is that you need to have the same format for all rows in a column.需要注意的是,您需要对列中的所有行使用相同的格式。

Sample Input:样本输入:

在此处输入图像描述

Sample Output:样品 Output:

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM