Home > Enterprise >  Google Script - Applying part of a 2d array to sheet
Google Script - Applying part of a 2d array to sheet

Time:11-03

I've got a sheet which I've used arrays to retrieve data and do some validations/calculations etc.

At the end of my code, I want to apply the updated values back to the sheet.

I've written this line.

shTest.getRange(1,1,sourceArray.length,sourceArray[0].length).setValues(sourceArray);

which works perfectly fine in applying the values of the full 2d array (which has 5 columns) to the sheet.

However I want to apply just the 5th column to column E in my sheet shTest (I declared shTest as a variable for my test sheet earlier in the code)

I got as far as this:

shTest.getRange(1,5,sourceArray.length,1).setValues(sourceArray);

But I couldn't workout how to amend the sourceArray part of my code (after .setValues) to specify that I want the data from the 5 column of the array.

CodePudding user response:

In your situation, how about the following modification?

From:

shTest.getRange(1,5,sourceArray.length,1).setValues(sourceArray);

To:

shTest.getRange(1,5,sourceArray.length,1).setValues(sourceArray.map(r => [r[4]]));
  • By this modification, the values of column "E" (the 5 column in your question) are put to the column "E" of "shTest" sheet.

Reference:

  • Related