I’m trying to insert some values in a Google sheet using a Google Apps Script, but when I’m spreading the range that I defined earlier, it only returns the first value.
I expected Logger.log(...pasteAndFormatRange);
, Logger.log(pasteAndFromatRange.flat()
, and Logger.log(pasteAndFormatRange[0], pasteAndFormatRange[1], pasteAndFormatRange[2], pasteAndFormatRange[3]);
to return 2.0, 1.0, 626.0, 9.0
, but it only returns 2.0
.
I expected Logger.log(pasteAndFormatRange);
and Logger.log([...pasteAndFormatRange]);
to return [2.0, 1.0, 626.0, 9.0]
, which it does.
I have made sure that the typeof
the array is in fact an object
. I have tried making a new array from this array in several ways, but the new array behaves the same way.
Also this worked just fine a week ago… I guess my next step would be to define the range inside the .getRange()-method directly, but then I wouldn’t be able to keep my code DRY 🙁
Here is the code:
let pasteAndFormatRange;
if (action === 'override') {
pasteAndFormatRange = [2, 1, target.getLastRow()-1, target.getLastColumn()];
}
Logger.log(pasteAndFormatRange); // returns [2.0, 1.0, 626.0, 9.0]
Logger.log([...pasteAndFormatRange]); // returns [2.0, 1.0, 626.0, 9.0]
Logger.log(...pasteAndFormatRange); // returns 2.0
Logger.log(pasteAndFormatRange[0], pasteAndFormatRange[1], pasteAndFormatRange[2], pasteAndFormatRange[3]); // returns 2.0
target.getRange(...pasteAndFormatRange).setValues(data.slice(1));
2
Answers
The issue isn’t with the spread operator.
Looking at
Logger.log
‘s documentation, it takes only one argument, and thus, silently ignores the others. Keeping it as an array should do the trick, as you’ve seen.Logger
doesn’t support multiple arguments. Useconsole.log
instead.