Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spread operator only returns the first value in Google apps script

I am totally stuck with a problem.

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading