Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get values in a column and put them into an array? Google Sheets

I am trying to take a column of names and put them into an array.

function printDriver() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 var driverArray = [];
 driverArray = sheet.getRange("H2:H34").getValues();
 driverArray.join('').split(''); 

I am unsure if this is the correct way to do it. Names only appear in H2:H34 of sheet 0 in my spreadsheet. I want to get all those values, put them into an array and delete empty values but I believe that I end up with an empty array.

like image 959
Ryan Capule Avatar asked Aug 31 '25 20:08

Ryan Capule


1 Answers

Solution:

You just need to flat the array and then filter out the empty cells.

You can do that with one line of code:

driverArray = sheet.getRange("H2:H34").getValues().flat().filter(r=>r!="");

function printDriver() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 var driverArray = sheet.getRange("H2:H34").getValues().flat().filter(r=>r!="");
 }

driverArray is the desired array.

Related:

What does the range method getValues() return and setValues() accept?

like image 128
soMarios Avatar answered Sep 04 '25 17:09

soMarios