Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Google Sheets, writing array to sheet via script

I’ve a sheet which I’m using arrays in the script (to save on time)

I have no problems writing a 2d Array to my sheets, however for some reason I can’t get a 1D array to write properly

This is a screenshot in debug mode of my array.
It’s just a list of prices (500 rows)
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my code (I added in the log to show the counts)

Logger.log (costPerItem.length) 
shWorkspace.getRange(1,12,costPerItem.length,1).setValues(costPerItem);

I just want to put the data into the 12th column on the tab that I’ve called shWorkspace earlier in the code.

But I get the error:

Exception: The parameters (number[]) don’t match the method signature for SpreadsheetApp.Range.setValues.

And I can’t figure out why.

I’ve used almost identical code:

shProductData.getRange(1,1,fullDataArrayProducts.length,fullDataArrayProducts[0].length).setValues(fullDataArrayProducts)

To apply a 2d array and this works fine

>Solution :

Posting a flattened array as a column

let a = CostPerItem.map(e => [e]);
sheet.getRange(1,1,a.length, a[0].length).setValues(a);

function lfunko() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  let CostPerItem = ["Cost Per Item", 1, 2, 3, 4, 5];
  let a = CostPerItem.map(e => [e]);
  sh.getRange(1, 1, a.length, a[0].length).setValues(a);
}
Cost Per Item
1
2
3
4
5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading