google-apps-script - 如何减少在 for 循环中使用 setValues() 和 copyto() 的谷歌应用程序脚本的运行时间

标签 google-apps-script google-sheets

如何减少在 forloop 中使用 setValues() 和 copyto() 的 Google 应用脚本的运行时间

找到一些代码并修改它 用于复制格式复制和粘贴

我不知道为什么使用它 setValues() 和 CopyTo() 但它工作..

我认为问题是 forloop 中的某个类。 但, 在这种情况下,我不知道如何将 setValues(),copyto() 与 forloop 分开

它工作得很好,但是在 forloop 中通过 setValues() 需要很多时间 如何将其与 forloop 分开以加快速度

function Duplicate_Format() {
  const SS = SpreadsheetApp.getActiveSpreadsheet();
  const Source_NotationRange = "B6:B47";
  const Target_Sheet = SS.getSheets();
  const Source_Data_sheet = SpreadsheetApp.getActive().getSheetByName('01.01');
  const Source_Data_Range = Source_Data_sheet.getRange(Source_NotationRange);
  var Location_First = 4;
  var Location_Quantity = 366;
  const Location_Last = Location_First + Location_Quantity -1;
  
  for( let j = Location_First ; j <= Location_Last; j++){
   var Target_Sheets = Target_Sheet[j];
   var Target_Range = Target_Sheets.getRange(Source_NotationRange);
   Target_Range.setValues(Source_Data_Range.getValues());
   Source_Data_Range.copyTo(Target_Range, {formatOnly:true});
  }
}

最佳答案

我相信您的目标如下。

  • 您希望降低脚本的处理成本。

为了实现这一目标,我建议使用 Sheets API。本例中,修改后的脚本流程如下。

  1. 为 Sheets API 中的 batchUpdate 方法的 CopyPasteRequest 创建请求正文。
  2. 使用创建的请求正文向 Sheets API 发出请求。

修改后的脚本:

在运行脚本之前,please enable Sheets API at Advanced Google services .

function Duplicate_Format() {
  const SS = SpreadsheetApp.getActiveSpreadsheet();
  const Source_NotationRange = "B6:B47";
  const Target_Sheet = SS.getSheets();
  const Source_Data_sheet = SpreadsheetApp.getActive().getSheetByName('01.01');
  const Source_Data_Range = Source_Data_sheet.getRange(Source_NotationRange);
  var Location_First = 4;
  var Location_Quantity = 366;
  const Location_Last = Location_First + Location_Quantity -1;

  // I modified below script.
  // 1. Create the request body for the CopyPasteRequest of the method of batchUpdate in Sheets API.
  var requests = [];
  var srcStartRow = Source_Data_Range.getRow() - 1;
  var srcEndRow = srcStartRow + Source_Data_Range.getNumRows();
  var srcStartCol = Source_Data_Range.getColumn() - 1;
  var srcEndCol = srcStartCol + Source_Data_Range.getNumColumns();
  for( let j = Location_First ; j <= Location_Last; j++){
    var Target_Sheets = Target_Sheet[j];
    var Target_Range = Target_Sheets.getRange(Source_NotationRange);
    var dstStartRow = Target_Range.getRow() - 1;
    var dstEndRow = dstStartRow + Target_Range.getNumRows();
    var dstStartCol = Target_Range.getColumn() - 1;
    var dstEndCol = dstStartCol + Target_Range.getNumColumns();
    requests.push({
      copyPaste:{
        source:{sheetId:Source_Data_sheet.getSheetId(),startRowIndex:srcStartRow,endRowIndex:srcEndRow,startColumnIndex:srcStartCol,endColumnIndex:srcEndCol},
        destination:{sheetId:Target_Sheets.getSheetId(),startRowIndex:dstStartRow,endRowIndex:dstEndRow,startColumnIndex:dstStartCol,endColumnIndex:dstEndCol},
        pasteType:"PASTE_NORMAL"
      }
    });
  }

  // 2. Request to Sheets API using the created request body.
  var res = Sheets.Spreadsheets.batchUpdate({requests: requests}, SS.getId());
}

引用文献:

关于google-apps-script - 如何减少在 for 循环中使用 setValues() 和 copyto() 的谷歌应用程序脚本的运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62826505/

相关文章:

google-sheets - Google Sheets - 从一个范围内重复值 'n' 次

javascript - 在 Google Apps 脚本中设置值

google-sheets - 分配唯一 ID

google-apps-script - Google App 脚本,.getValue 返回错误

google-apps-script - 驱动器 PDF 修订 ID 被忽略

google-apps-script - 单元格处于事件状态时突出显示整行

使用库调试 Google 电子表格脚本

google-apps-script - 如何解释Google服务的配额

concurrency - 访问电子表格时锁定脚本不起作用

javascript - 循环期间出现 Google Sheet Script Null 错误