简体   繁体   中英

How to speed up Google Apps Script runtime?

I am a beginner in the programming world, I created a script to go through table1 and bring the values ​​as they are in table2 according to the periods informed in it.

The script works correctly because it is very slow at run time. I would like some help to try and make him faster.

I searched for arrays but I could not understand what I could do to apply this function.

link spreadsheet : https://docs.google.com/spreadsheets/d/1mXPCEC50rw49GU42wM-VoQjVFqeGpw7O7A_eo5tZMms/edit#gid=1483928389

link Script : https://script.google.com/d/MRZzi4FhForjALEjRtYg7-CuYWxd3EZHO/edit?mid=ACjPJvFf4IPyfgW5E8N_XRpLXHxjNzQMt2gpkcMd0L7nom15r8yLAaUfGyxmhInUC2B9cVfhDFNmRVqycy7hfyOjIv3IcxkEPxOWx8aSfeGF6zjklcEyKc-daU6m3hDBVTEDq_HpC-gowQI&uiv=2

 function teste() { var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Página6"); var tabela = ss.getRange(3, 9, ss.getLastRow(), 1).getValues(); for(var l1 = 3 ; l1 <= tabela.length ; l1 ++ ){ var tab1 = ss.getRange(l1,8).getValue(); for( var l2 = 3 ; l2 <= tabela.length ; l2 ++ ){ var tab2 = ss.getRange(l2,12).getValue(); if (tab1 == tab2){ for( var col = 13 ; col < 17 ; col ++){ var coluna = ss.getRange(2, col).getValue(); if( ss.getRange(l1, 9).getValue() == ss.getRange(2, col).getValue()){ ss.getRange(l2, col).setValue(ss.getRange(l1, 10).getValue()); } } } } } }

  • You want to convert the table as the following image. You want to convert the left table to the right table.

  • Each number of Produto from Mês 1 to Mês 4 is the same.
  • The number of Mês is sorted.
  • The place of left and right tables is fixed in your sample.
    • Left table is H2:J38 .
    • Right table is L2:P11 .
  • You want to speed up of your script by modifying.

I could understand like above. If my understanding is correct, how about this modification? Please think of this as just one of several answers.

Modification points:

  • In your script, the important point is that the values for putting to the sheet are prepared, and then the prepared values are put to the sheet using setValues() .
  • In this modification, also, the values retrieved by getValues() are prepared and put to the sheet using setValues() .

Modified script:

function teste() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Página6");
  var values = ss.getRange(3, 8, ss.getLastRow() - 2, 5).getValues();

  // Value are parsed and put them to an object.
  var obj = values.reduce(function(o, e) {
    if (o[e[0]]) {
      o[e[0]][e[1]] = e[2];
    } else {
      o[e[0]] = {};
      o[e[0]][e[1]] = e[2];
    }
    return o;
  }, {});

  // Create an array for putting values to sheet.
  var ar = values.filter(function(e) {return e[4]}).map(function(e) {return Object.keys(obj[e[4]]).map(function(f) {return obj[e[4]][f]})});

  // Put the values.
  ss.getRange(3, 13, ar.length, ar[0].length).setValues(ar);
}

References:

If I misunderstood your question and this was not the result you want, I apologize.

Read this, it should help you depending on the complexity of your project. https://developers.google.com/apps-script/guides/support/best-practices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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