Spread operator only returns the first value in Google apps script

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));

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. Use console.log instead.

function tgi09(){
  console.log(1,2,3);//Logs 1 2 3
  Logger.log(1,2,3); //Logs 1.0
}

And yet if pasteFormatRange had been and array instead of a range it would have worked:

function myfunk() {
  const pasteAndFormatRange = [
    [1, 2, 3],
    [4, 5, 6]
  ];
  Logger.log([...pasteAndFormatRange]);
}

7:22:52 AM  Notice  Execution started
7:22:52 AM  Info    [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
7:22:53 AM  Notice  Execution completed

Leave a Comment