0

Sorry I can't actually comment the following answer because I'm too low on reputation.

I'm actually trying to implement the solution of "ken" from Write an array of values to a range of cells in a spreadsheet

I want get my array ["Adam","Barb","Chris"] to appear on 3 differents lines.

Which is:

function addArrayToSheetColumn(sheet, column, values) {
  const range = [column, "1:", column, values.length].join("");
  const fn = function(v) {
    return [ v ];
  };
  sheet.getRange(range).setValues(values.map(fn));
}
const ss = SpreadsheetApp.getActiveSpreadsheet();
const cache = ss.getSheetByName("_cache_");
const results = ["Adam","Barb","Chris"];
addArrayToSheetColumn(cache, "A", results);

I get the error:

TypeError: Cannot read property 'length' of undefined

But as it points to the function, I don't get why the error appear.

vector
  • 1,032
  • 1
  • 4
  • 16
Gron465
  • 25
  • 1
  • 9

1 Answers1

1

That's because maybe you're trying to run just addArrayToSheetColumn function after selecting it, without actual arguments being passed to this function.

Select primary_Func() and run, it will work:

function primary_Func()
{
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const cache = ss.getSheetByName("_cache_");
  const results = ["Adam","Barb","Chris"];
  addArrayToSheetColumn(cache, "A", results);// passing arguments to this function
}

function addArrayToSheetColumn(sheet, column, values) {
  const range = [column, "1:", column, values.length].join("");
  const fn = function(v) {
    return [ v ];
  };
  sheet.getRange(range).setValues(values.map(fn));
}
vector
  • 1,032
  • 1
  • 4
  • 16
  • Thank you. I tried again with the solution of "ken" and yours and it both works. I think I had an issue with my array because it was not completely the same as ken's. But I think yours is clean. – Gron465 Dec 30 '21 at 15:34