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
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()); } } } } } }
Produto
from Mês 1
to Mês 4
is the same.Mês
is sorted. H2:J38
.L2:P11
.I could understand like above. If my understanding is correct, how about this modification? Please think of this as just one of several answers.
setValues()
.getValues()
are prepared and put to the sheet using setValues()
.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);
}
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.