如何加快运行功能的速度

问题描述 投票:1回答:1

Google电子表格应用程序脚本-[如何使用此代码加快运行功能]

我想让它现在变得更快

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

复制和工作表名称更改代码。工作表名称为1月1日至12月31日,包括2月29日like [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-script optimization google-sheets
1个回答
0
投票
我相信您的情况和目标如下。

    您的脚本工作正常。但是您想减少脚本的处理成本。
  • 为此,这个答案如何?

    问题和解决方法:

    在您的脚本中,工作表被复制到for循环中。循环数大约为360。在这种情况下,处理成本将很高。因此,在此答案中,为了减少脚本的成本,我想使用Sheets API提出以下流程。

      将源工作表复制到目标电子表格。
  • 使用Sheets API的duplicateSheet请求创建用于复制源工作表的请求正文。
  • 使用Sheets API的deleteSheet请求添加用于删除源工作表的请求正文。
  • 使用创建的请求主体对Sheets API的batchUpdate方法的请求。
  • 当上述流程反映到您的脚本时,它将变成如下。

    修改的脚本:

    使用此脚本之前,please enable Sheets API at Advanced Google services

    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张纸。在这种情况下,请注意以下限制。 Ref

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

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

  • 参考:

  • DuplicateSheetRequest
  • DeleteSheetRequest
  • © www.soinside.com 2019 - 2024. All rights reserved.