首页 > 解决方案 > Apps 脚本中循环执行缓慢

问题描述

基本上,我正在寻找的是您可以提供给我的任何建议,以使这段代码执行得更快。

这段代码的目的基本上是通过测试一些条件来获得分数和与选择的每个配置文件相关的问题,并将这些分数打印在“结果表”中,并在分配有配置文件名称的表中打印问题。

通过一些快速和基本的测试,我发现每当循环开始打印结果或问题时,它都是缓慢执行开始的地方。

很抱歉我几周前刚开始在 Apps Script 中编程的代码混乱。

编辑

更具体地说,我试图找出是否有任何解决方法可以在不同的工作表和配置文件的值中打印问题。我看了一下类似的问题,他使用 for..in 解决了一个循环,我仍在尝试找出是否有任何其他逻辑过程可以执行来获取这些值。

var iGrade1 = 0;
 var iGrade2 = 0.5;
 var iGrade3 = 1;
 var resultsSheet = SpreadsheetApp.getActive().getSheetByName('Results');

 var iStartingColumn = 5;
 var iCellD = 'D';
 var iCellE = 'E';
 var iTotCounter = 2;
 var amountProfiles = profileSheet.getRange
 (2, perfilSheet.getLastColumn()).getValue();

 var iTotPerf = 0;
 var iPT = 1;
 var aResizeP = 1;

  for( i = 2; i< questionsSheet.getLastRow()  ; i++ ){
    //Range 
    sBI = 'B' + i;
    sDI = 'D' + i;

   //Copies values like 1|2|3|4|, 3|4|,  2|3|, etc.
   profileCodes = questionsSheet.getRange(sDI).getValue();


    for(j = 0; j < amountProfiles; j++){
//There is a checkbox where user selects if he wants      that profile or not
    if(profileSheet.getRange(1,iStartingColumn).getValue()
    === 'Yes'){
    var sheetProfile =             SpreadsheetApp.getActive().getSheetByName
(profileSheet.getRange(3, iStartingColumn).getValue());

    var totProfile = profileSheet.getRange(perfilSheet.getLastRow(), iStartingColumn).getValue();
    //User can select in a checkbox between 'Bad' 'Regular' and 'Good'
     if( ( questionsSheet.getRange( sBI ).getValue() === '× Bad' )  ){     
       if(profileCodes.split("|").indexOf( String( perfilSheet.getRange(2, iStartingColumn).getValue() ) ) > -1 ){
         iTotPerf = iTotPerf + iGrade1; }
    }

     else if( ( questionsSheet.getRange( sBI ).getValue() === ' ± Regular' )  ){
      if(  profileCodes.split("|").indexOf( String( perfilSheet.getRange(2, iStartingColumn).getValue() ) ) > -1 ){
         iTotPerf = iTotPerf + iGrade2; }
    }

    else if( ( questionsSheet.getRange( sBI ).getValue() === '✓ Good' )  ){
       if(profileCodes.split("|").indexOf( String( perfilSheet.getRange(2, iStartingColumn).getValue() ) ) > -1 ){
         iTotPerf = iTotPerf + iGrade3; }
    }

    if ( profileCodes.split('|').indexOf(String
    (perfilSheet.getRange(2, iStartingColumn).getValue()))
    > -1){
    questionsSheet.getRange('A' + i + ':' + 'C' + i)
    .copyTo(sheetProfile.getRange('A' + iPT),SpreadsheetApp
    .CopyPasteType.PASTE_NORMAL,false);
    sheetProfile.autoResizeColumn(aResizeP);
    }

    if(iTotPerf != 0){
    resultadosSheet.getRange(iCellD + iTotCounter).setValue(resultadosSheet.getRange(iCellD + iTotCounter).getValue() + iTotPerf);
    resultsSheet.getRange(iCellE + iTotCounter).setValue( ( (resultsSheet.getRange(iCellD + iTotCounter).getValue() * 100)/ totPerfil )/ 100);
    }
    }

    iTotPerf = 0;
    iTotCounter++;
    iStartingColumn++;   
    }


    iPT += 1; 
    iTotCounter = 2;
    iStartingColumn = 5; 
  }

标签: javascriptperformanceoptimizationgoogle-apps-scriptgoogle-sheets

解决方案


推荐阅读