javascript - Fill a range (multiple columns) down multiple rows - google-apps-script -


i had success filling single column (a) value found in range a1...

  var ss = spreadsheetapp.getactivespreadsheet().getsheetbyname("sheet1");   var lastrow = ss.getdatarange().getnumrows();     var rngval = ss.getrange("a1").getvalue()   ss.getrange("a2:a"+lastrow).setvalue(rngval) 

so thought on easy-street, , tried modify/apply larger range filling multi-column range values found in range c1:h1...

  var ss = spreadsheetapp.getactivespreadsheet().getsheetbyname("sheet1");   var lastrow = ss.getdatarange().getnumrows();     var rngval = ss.getrange("c1:h1").getvalues()   ss.getrange("c2:h"+lastrow).setvalues(rngval) 

apparently there bit more slapping "s" onto end of word "value".

the error reads follows:

incorrect range height, 1 should 10

(fyi: var lastrow = 11)

btw, no error if use value instead of values, although end cells full of value found in range c1.

so i'm close.... or way off. 1 of those.

help???

the error message quite explicit... size of array must fit range in both getvalues , setvalues. try :

function myfunction() {   var ss = spreadsheetapp.getactivespreadsheet().getsheetbyname("sheet1");   var lastrow = ss.getlastrow()     var rngval = ss.getrange("c1:h"+lastrow).getvalues();// array of 10 "rows" , 6 "columns"   ss.getrange("c2:h"+(lastrow+1)).setvalues(rngval);//write array range has same size. (starting row2 must ends on lastrow+1 keep same number of "rows" } 

this function shift range c1:h last row c2:h lastrow+1, not sure useful that's not point here ;-)


edit : sorry, didn't understand requirement... here code reproduce data c1:h1 in rows below

function myfunction() {   var ss = spreadsheetapp.getactivespreadsheet().getsheetbyname("sheet1");   var lastrow = ss.getlastrow()     var rngval = ss.getrange("c1:h1").getvalues();// array of first row   var rowdata = rngval[0]   var newdata = []   for(n=1;n<lastrow;++n){newdata.push(rowdata)}   ss.getrange("c2:h"+lastrow).setvalues(newdata);//write array range has same size. (starting row2 must ends on lastrow+1 keep same number of "rows" } 

edit2 following comment below :

a word of explanation :

when using range.getvalues() 2 dimensions array, meaning array of arrays can represented follow : [[data1,data2,data3],[data4,data5,data6]] data1, 2 & 3 value of first array (index 0) , data 4,5 & 6 values of second array (index 1). if want values in first array have write : value = arrayname[0] , return 1 dimension array [data1,data2,data3], that's used rowdata.

now need 2 dimension array again able write new data range in spreadsheet. therefor create new array (var newdata=[] or var newdata = new array() same), , in loop add rowdata array new array... result array of arrays, looking , can write directly sheet in 1 single setvalues statement.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -