首页 > 解决方案 > 如何运行函数加速

问题描述

谷歌电子表格应用程序脚本 - 如何运行函数加速[使用此代码]

我想让它现在更快

我制作了一些脚本来制作许多关于 366 的复制工作表。但是,谷歌电子表格运行功能的时间有限。所以我必须等待并运行第二个功能。

复制和工作表名称更改代码。工作表名称为 1 月 1 日至 12 月 31 日,包括 2 月 29 日,例如[01.01][01.02][01.03]....[02.29]...[12.30][12.31]

我正在等待一些切肉刀解决方案谢谢你的领导

function Sheets_Copying_with_Name(){
 var source = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = source.getSheets()[0]; 
 var Typing_Month =1;
 var Typing_Day=1;
 var MAX_Day = 31; 

  for (Typing_Month; Typing_Month< 13; Typing_Month++)
  {
    var destination = SpreadsheetApp.openById("Sheet ADDRESS");   
    if ((Typing_Month==1)||(Typing_Month==3)||(Typing_Month==5)||(Typing_Month==7)||(Typing_Month==8)||(Typing_Month==10)||(Typing_Month==12))  { MAX_Day=31; }
    if ((Typing_Month==4)||(Typing_Month==6)||(Typing_Month==9)||(Typing_Month==11)) { MAX_Day=30; }
    if ( Typing_Month==2) { MAX_Day=29;}
    for( Typing_Day; Typing_Day < MAX_Day +1; Typing_Day++ )
    { if( (Typing_Month < 10) && (Typing_Day < 10)) {  sheet.copyTo(destination).setName("0"+Typing_Month+".0"+Typing_Day);}
      if( (Typing_Month < 10) && (Typing_Day > 9)) {  sheet.copyTo(destination).setName("0"+Typing_Month+"."+Typing_Day);}
      if( (Typing_Month > 9) && (Typing_Day < 10)) { sheet.copyTo(destination).setName(Typing_Month+".0"+Typing_Day);}
      if( (Typing_Month > 9) && (Typing_Day > 9)) {  sheet.copyTo(destination).setName(Typing_Month+"."+Typing_Day);}
    }
Typing_Day = 1;
  }
}

标签: google-apps-scriptoptimizationgoogle-sheets

解决方案


我相信你的情况和目标如下。

  • 你的脚本工作正常。但是你想减少脚本的处理成本。

为此,这个答案怎么样?

问题和解决方法:

在您的脚本中,工作表被复制到 for 循环中。循环数似乎约为 360。在这种情况下,处理成本会很高。所以在这个答案中,为了降低脚本的成本,我想使用 Sheets API 提出以下流程。

  1. 将源工作表复制到目标电子表格。
  2. 使用 Sheets API 的 duplicateSheet 请求创建用于复制源工作表的请求正文。
  3. 使用 Sheets API 的 deleteSheet 请求添加删除源工作表的请求正文。
  4. 使用创建的请求正文向 Sheets API 的 batchUpdate 方法请求。

当上述流程反映到您的脚本时,它变成如下。

修改后的脚本:

在使用此脚本之前,请在 Advanced Google services 中启用 Sheets API

function Sheets_Copying_with_Name(){
  var destinationSpreadsheetId = "###";  // Please set the destinaton Spreadsheet ID.

  var source = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = source.getSheets()[0]; 
  var Typing_Month =1;
  var Typing_Day=1;
  var MAX_Day = 31; 

  // I modified below script.
  var destination = SpreadsheetApp.openById(destinationSpreadsheetId);
  var sourceSheet = sheet.copyTo(destination).setName("sourceSheet");
  SpreadsheetApp.flush();
  var sourceSheetId = sourceSheet.getSheetId();
  var requests = [];
  for (Typing_Month; Typing_Month< 13; Typing_Month++) {
    if ((Typing_Month==1)||(Typing_Month==3)||(Typing_Month==5)||(Typing_Month==7)||(Typing_Month==8)||(Typing_Month==10)||(Typing_Month==12))  { MAX_Day=31; }
    if ((Typing_Month==4)||(Typing_Month==6)||(Typing_Month==9)||(Typing_Month==11)) { MAX_Day=30; }
    if ( Typing_Month==2) { MAX_Day=29;}
    for( Typing_Day; Typing_Day < MAX_Day +1; Typing_Day++ ) {
      if( (Typing_Month < 10) && (Typing_Day < 10)) {
        requests.push({duplicateSheet: {sourceSheetId: sourceSheetId, newSheetName: "0"+Typing_Month+".0"+Typing_Day}});
      }
      if( (Typing_Month < 10) && (Typing_Day > 9)) {
        requests.push({duplicateSheet: {sourceSheetId: sourceSheetId, newSheetName: "0"+Typing_Month+"."+Typing_Day}});
      }
      if( (Typing_Month > 9) && (Typing_Day < 10)) {
        requests.push({duplicateSheet: {sourceSheetId: sourceSheetId, newSheetName: Typing_Month+".0"+Typing_Day}});
      }
      if( (Typing_Month > 9) && (Typing_Day > 9)) {
        requests.push({duplicateSheet: {sourceSheetId: sourceSheetId, newSheetName: Typing_Month+"."+Typing_Day}});
      }
    }
    Typing_Day = 1;
  }
  requests.push({deleteSheet: {sheetId: sourceSheetId}});
  Sheets.Spreadsheets.batchUpdate({requests: requests}, destinationSpreadsheetId);
}
  • 当我在我的环境中测试这个脚本时,脚本的处理时间大约是 30 秒。在这种情况下,脚本完成后,需要等待完全复制工作表。在此之后,请重新打开目标电子表格。这样,我可以确认所有工作表都已创建。请注意这一点。

笔记:

  • 在您的脚本中,我认为当脚本运行时,在目标电子表格中创建了大约 360 张工作表。在这种情况下,请注意以下限制。参考

    对于在 Google 表格中创建或转换为 Google 表格的电子表格,最多可包含 500 万个单元格或 18,278 列(ZZZ 列)。

  • 如果发生与此限制相关的错误,请减少源工作表的单元格数量。请注意这一点。

参考:


推荐阅读