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

252
Views
How to get the last 1000 rows dynamically and sort them using GAS?

I'm trying to get this to sort only the last 1000 rows, dynamically. This is the code I'm using:

function sort(e){
const formRespSheet = e.source.getSheetByName('Form Responses 1');
let lr = formRespSheet.getLastRow()-1;
let firstRow = 2
 if (lr > 1000) {
   firstRow = lr - 1000;
 }
 Logger.log('First Row: ' + firstRow)
 Logger.log('Last Row: ' + lr)
 formRespSheet.getRange(firstRow, 1, lr, 16).sort(14);
}

It gives me the correct firstRow and lr, but it also throws The coordinates of the range are outside the dimensions of the sheet, besides running forever (15 sec).

Could anyone help me find the flaw here (besides myself)?

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The signature of getRange uses numRows and numColumns, not lastRow and lastColumn.

Because you start selecting at row 2 it tries to get a range one row beyond what the spreadsheet has.

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!