. Star the issue if you are affected by the same problem and want to see it fixed. Posting here as well since this may be of interest to the community.
Array arguments received by custom functions seem to get mangled when the same custom function appears twice in the same spreadsheet formula. For example, a formula like this will not give the expected result:
=getFirstElementRowByRow( "same id", transpose( getFirstElementRowByRow("same id", A1:A10) ) )
Here, the string argument "same id" does not need to actually be used by the custom function. But when that unused string argument differs, the formula gives the expected result:
=getFirstElementRowByRow( "outer function", transpose( getFirstElementRowByRow("inner function", A1:A10) ) )
Curiously, the latter formula (with "outer function", "inner function") will give the correct result when A1:A10 contains at least one null value, i.e, a blank cell or a cell with a formula like =iferror(1/0). It also seems to give the correct result when the argument is not a Nx1 array like A1:A10 but a NxM array like A1:B10.
The custom function will at times also report that it received a single value rather than a 2D array as expected.
Here's a publicly editable spreadsheet that demonstrates the issue:
Cheers --Hyde
Here is a custom function code sample that reliably reproduces the issue:
/**
* Gets the first element of a 2D array row-by-row.
* Takes a 2D array[numRows][numColumns] and returns a 2D array[numRows][1].
*
* @param {"inner function"} id A text string that identifies this function call (for error messages).
* @param {A1:A10} array A vertical spreadsheet range.
* @return {Object[][]} The first element of each row as a 2D array[numRows][1] with exactly one element per subarray.
* @customfunction
*/
function getFirstElementRowByRow(id, array) {
if (!Array.isArray(array)) {
return id + ': got the single element ' + JSON.stringify(array);
}
if (!Array.isArray(array[0])) {
return id + ': got the 1D array ' + JSON.stringify(array);
}
var numRows = array.length;
var result = new Array(numRows); // [Rhino]
for (var row = 0; row < numRows; row++) {
result[row] = new Array(1); // [Rhino]
result[row][0] = array[row][0];
}
return result;
}
Follow these steps to reproduce the problem:
1. Create a new spreadsheet and paste the above script in Tools > Script editor.
2. Insert values in cells A1:A10 of a sheet.
3. Enter the formula =getFirstElementRowByRow( "outer function", transpose( getFirstElementRowByRow("inner function", A1:A10 ) ) in cell C1 of the sheet.
The expected result is the single value in cell A1, but the formula instead returns the 10 values in A1:A10.