increment - Incrementing +1 through range -
i trying randomise set of increments on large number of variables.
so example, have 5,000 people, each of these people has base value of 1, adding 1 has variable affect each of these people. example adding 1 person a, means there base number increments 1.1. adding 1 person b increments base 1.4 , on.
i have figured out how different values assigned each +1 action vlookups, need loop +1 increments through range.
i have base values setup in column b:b can't figure out how loop script. have far:
function increment(){
var ss = spreadsheetapp.getactivespreadsheet(); var sheet = ss.getsheets()[1];
var range = sheet.getrange("b1"); var value = range.getvalue();
for (var = 1; >= range; i++);
range.setvalue(value + 1);
}
but increments 1 "b1". how loop runs continously, every second through b1, b2, b3, b4 etc adding +1 each time?
simply adding range name using "b" + in getrange() function, , move loop.
or can use offset() function instead.
the code
for (var = 1; >= range; i++); range.setvalue(value + 1);
is loop without running command range.setvalue(value + 1); because first line ending semi-colon.
finally, code should below.
function increment() { var ss = spreadsheetapp.getactivespreadsheet(); var sheet = ss.getsheets()[1]; //var range = sheet.getrange("b1"); //var value = range.getvalue(); // change 5000 amount of need (var = 1; <= 5000; i++) { var range = sheet.getrange("b" + i); var value = range.getvalue(); range.setvalue(value + 1); } }
if using offset function code below.
function increment() { var ss = spreadsheetapp.getactivespreadsheet(); var sheet = ss.getsheets()[1]; var range = sheet.getrange("b1"); //var value = range.getvalue(); // change 5000 amount of need (var = 1; <= 5000; i++) { var value = range.offset(i, 0).getvalue(); range.offset(i, 0).setvalue(value + 1); } }
edit improve execute time
function increment() { var ss = spreadsheetapp.getactivespreadsheet(); var sheet = ss.getsheets()[1]; // change 5000 amount of need var formulas = new array(5000); (var = 0; < 5000; i++) { formulas[i] = "=r[0]c[-1]+1"; } // assume column c not used var ranges = sheet.getrange("c1:c5000"); ranges.setformulasr1c1(formulas) ranges.copyvaluestorange(sheet, 2, 2, 1, 5000); ranges.clearcontent(); }
Comments
Post a Comment