Google Sheets: 如何让自定义函数的返回值溢出成一行?

Google Sheets: how to make returned value of custom function overflow into a row?

我编写了一个自定义函数,其中 return 是一个简单的数组。 (这是对多张纸进行简单的脏 3D 查找)。以下是代码,如果有帮助的话:

function get3DCellValues(startSheet, endSheet, cell) {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sum = 0;
var cellValues = [];
for (var i = (startSheet); i < endSheet; i++ ) {
    var sheet = sheets[i];
    var val = sheet.getRange(cell).getValue();
    cellValues.push(val);
}
//Logger.log(cellValues);
return cellValues;
}

问题是当我 return cellValues 时,值会向下溢出列。但我希望它通过行向右溢出。有办法吗?谢谢。

Google 的 guide 关于自定义函数 returning 值有这样的说法:

Every custom function must return a value to display, such that:

If a custom function returns a value, the value displays in the cell the function was called from. If a custom function returns a two-dimensional array of values, the values overflow into adjacent cells as long as those cells are empty

但这似乎对我没有帮助。

数组中的每个条目代表一行。 例如[[1,2],[3,4]] 将是两行 [1,2][3,4].

[1,2,3,4] 被解释为 [[1],[2],[3],[4]],所以它有 4 行,每行一个值。

如果你只想要一行,你可以写 [[1,2,3,4]].

所以你必须像这样更改你的代码

...
var cellValues = [[]];
...
cellValues[0].push(val);

SpiderPig 非常明确的回答对我帮助很大。

如果你总是想return只有一行,那么你也可以将代码写成

...
var cellValues = [];
...
cellValues.push(val);
...
return [cellValues];

这将 return 一个数组,其中包含您的 cellValues 数组作为其第一个也是唯一的条目