Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
Length of an array in Google Sheets App Script

I'm trying to determine the length of an array, so that I can use the length to determine how many times a for loop will need to iterate.

I'm trying to read from 1 row across many columns to determine which cells need to be written to. The code will write data to the rows, but will need to determine if the header has already been written or if the header needs writing also. In this function I am focusing on checking the headers to see if the value is already written.

I understand that the range values D2:Z2 is inefficient and I am open to another way around this. I'd be interested to know if anyone is able to determine why the length is 1.0 instead of 4, and if anyone else can think of a way to compare the values, the then determine the column to write the new set of headers in.

Code

 function getCurrentHeader() {

  //gets active spreadsheet, saves to array
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Records");
  var rangeValues = Array;
  //D2:Z2 is the current largest range available 15/12/21 
  rangeValues = sheet.getRange("D2:Z2").getValues(); 
  return rangeValues;
}


function checkCurrentHeaders(){
  //gets the array of headers
  var current = getCurrentHeader();
  //Logger.log(current);
  var leng = current.length;

  Logger.log(leng);

}

The above is what i've written so far, but even with the array being logged to view the values, the .length is returning a value of 1, there are 4 values in the array so i was expecting 4 as the return. I've also noted that the output is in two sets of square brackets. Please see the log information below.

Logger

11:52:32 AM Notice  Execution started
11:52:32 AM Info    [[B, A, C, DF, , , , , , , , , , , , , , , , , , , ]]
11:52:32 AM Info    1.0
11:52:32 AM Notice  Execution completed
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

getValues() returns 2 dimensional array. In your script, rangeValues = sheet.getRange("D2:Z2").getValues() is the 2 dimensional array like [[D2, E2, F2,,,]]. By this, when you check current.length of var current = getCurrentHeader(), 1 is returned. In this case, 1 is the number of rows. I think that this is the reason of your issue.

When you want to retrieve the number of columns, please modify as follows.

From:

var leng = current.length;

To:

var leng = current[0].length;

or

From:

rangeValues = sheet.getRange("D2:Z2").getValues(); 

To:

rangeValues = sheet.getRange("D2:Z2").getValues()[0];

or

From:

rangeValues = sheet.getRange("D2:Z2").getValues(); 

To:

rangeValues = sheet.getRange("D2:Z2").getValues()[0].filter(String); // In this case, the empty cells are ignored.

References:

  • getRange(a1Notation)
  • getValues()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!