Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude a header row from getDataRange()?

I want to fetch the range of all cells with data in it, but not including the header row.

I found that getDataRange() captures all the cells with data in it, and to avoid including the first row, I thought I could just delete the first element in what I am assuming is an array?

How can I do this/ is there a more elegant way to approach this problem?

like image 958
Dylan Avatar asked Dec 17 '22 16:12

Dylan


1 Answers

  • You want to retrieve the values without the 1st row using getDataRange().

If my understanding is correct, how about this sample script? Please think of this as just one of several answers.

Sample script 1 :

In this sample, offset() is used. In this case, before the values are retrieved, the range is modified.

var sheet = SpreadsheetApp.getActiveSheet();
var headerRowNumber = 1;
var values = sheet.getDataRange().offset(headerRowNumber, 0, sheet.getLastRow() - headerRowNumber).getValues();
Logger.log(values)

Sample script 2 :

In this sample, shift() is used. In this case, after the values were retrieved, the values are modified.

var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getDataRange().getValues();
values.shift();
Logger.log(values)

References:

  • offset()
  • shift()

If I misunderstood your question and this was not the direction you want, I apologize.

like image 102
Tanaike Avatar answered Dec 28 '22 06:12

Tanaike