4

I'm writing a script for use in Google Sheets where it returns an array of arrays for the purpose of filling out multiple columns and rows.
I've got

var results = new Array(2);
var info = new Array(2);
info[0] = "some string";
info[1] = "other string";
results[0] = info;
results[1] = info;
return results;

The first column will be blank when it should have "some string", while the second contains "other string"

Rubén
  • 34,714
  • 9
  • 70
  • 166

2 Answers2

6

Assuming that you are asking about Custom Functions in Google Spreadsheets, your code currently returns a 2x2 table with the values:

|----------------------------|
| some string | other string |
| some string | other string |
|----------------------------|

If you want the following:

|----------------------------|
| some string | other string |
|----------------------------|

Then your code should be:

var results = new Array(2);
var info = new Array(2);
info[0] = "some string";
info[1] = "other string";
results[0] = info;
return results;
Motin
  • 4,853
  • 4
  • 44
  • 51
  • Simply put: A Google Sheets custom function returns an Array of rows, each element of which is an Array of cell values. If it returns a scalar instead that's a single cell's value. – Matthew Feb 06 '19 at 03:46
1
var results = new Array(2);
var info = new Array(2);
info[0] = "some string";
info[1] = "other string";
results[0] = info;
return results;

I tried your code on multiple rows containing data but got error that Google Sheet can't write as it will overwrite next line.

I made changes to first line then it worked

var results = new Array(1);
var info = new Array(2);
info[0] = "some string";
info[1] = "other string";
results[0] = info;
return results;

Thank You. Snapshot of code used in actual project