javascriptgoogle-apps-scriptdebuggingspread-syntax

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

Solution

  • 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
    }