Using Google Apps Script, is there a way to write the values in a Google Spreadsheet array to a range without looping?
I am thinking something like the following to put one name each into cells A1:A3
function demoWriteFromArray() {
var employees=["Adam","Barb","Chris"];
ssActive = SpreadsheetApp.getActiveSheet();
rgMyRange = ssActive.getRange("A1:A3");
rgMyRange.setValue(employees)
}
Problem with above is that after execution, A1:A3
all contain ={"Adam","Barb","Chris"} and display "Adam"
.
Range.setValue() is used for setting the same value in every cell of the range, while setValues is used to set an array of values into the corresponding cells in the range. Be aware that this method expects a multi-dimensional array, with the outer array being rows and the inner array being columns. So in your case the data should look like:
var employees=[["Adam"],["Barb"],["Chris"]];