What is getRange in Google Sheets Apps Script?

getRange returns a Range object pointing at one or more cells on a sheet. You call it on a Sheet to read with getValue or getValues and write with setValue or setValues. Correct range targeting is the foundation of almost every Sheets script.

When to use it

Use getRange whenever your script touches specific cells: read a settings block, format a header row, update a status column, or grab the active table before sorting.

When to skip it

Skip hard-coded A1 addresses when a named range or dynamic getLastRow sizing will survive column inserts. Fragile literals like D:D break when someone adds a column left of D.

How it works

  1. 1

    On a sheet, call getRange('B2:F100') with A1 notation for a fixed block.

  2. 2

    Or getRange(row, column, numRows, numColumns) with 1-based indexes for dynamic sizing.

  3. 3

    getRange('A:A') selects a full column. getDataRange() selects the current data rectangle.

  4. 4

    Named ranges use spreadsheet.getRangeByName('InvoiceTotal') for stable references.

  5. 5

    Chain methods like getRange(2, 1, lastRow - 1, 5) after getLastRow for data-only areas.

  6. 6

    Active selection uses SpreadsheetApp.getActiveRange() when the user highlighted cells.

Examples in Google Sheets

Dynamic data block

const lastRow = sheet.getLastRow(); sheet.getRange(2, 1, lastRow - 1, 6).getValues() skips the header when lastRow is accurate.

Named range sort

spreadsheet.getRangeByName('OrdersTable').sort({column: 4, ascending: false}) keeps sort logic stable when the name is maintained.

Format header

sheet.getRange(1, 1, 1, sheet.getLastColumn()).setFontWeight('bold') formats only the header row width.

Build this without starting from a blank cell

Use a Better Sheets tool for getRange, then watch a walkthrough when you want the full pattern.

Better Sheets resources

Common mistakes

  • Off-by-one in getRange(row, col, numRows, numCols) by passing end row instead of row count.
  • getRange on the spreadsheet object vs sheet object confusion; most calls need a Sheet first.
  • Assuming getLastRow includes empty rows in the middle of data; it only helps for contiguous blocks from row 1.
  • Hard-coding Sheet1 when the tab was renamed, causing null reference errors.
  • getRange larger than the sheet limits without splitting writes.

Frequently asked questions

What is A1 notation vs numeric getRange?
A1 uses letters and numbers like C5:E10. Numeric form uses start row, start column, number of rows, and number of columns, all 1-based.
How do I get the selected range?
SpreadsheetApp.getActiveRange() returns what the user selected. It can be null if nothing is selected.
Can getRange span multiple sheets?
No. Each range belongs to one sheet. Read each tab separately and combine in code.
What is getRangeList?
getRangeList accepts multiple A1 strings on one sheet for batch format operations on non-contiguous areas.
How do named ranges work in scripts?
Define names in the Sheets UI or script, then getRangeByName on the spreadsheet. Renaming in the UI updates the script reference.
Why does getRange return null?
getSheetByName failed because the tab name changed, or the range string is invalid.
Can I getRange a whole row?
Yes. getRange('5:5') selects row 5. Full column is getRange('C:C').
Is getRange the same as getActiveCell?
getActiveCell is one cell. getActiveRange can be multiple cells the user highlighted.

Related Tutorials

Watch how getRange works

Browse more tutorials
Deep Inside Dark Habits Google Script

Deep Inside Dark Habits Google Script

Go inside this apps script and sheet. See how it's designed and created.
Add Tasks to Google Tasks From Google Sheets

Add Tasks to Google Tasks From Google Sheets

Create tasks in Google Tasks from Google Sheets using Apps Script
How do I reference a different spreadsheet in Apps Script?

How do I reference a different spreadsheet in Apps Script?

I'll show you how to get text from one spreadsheet file to another with ...
Embed a Number in a Website from a Google Sheet

Embed a Number in a Website from a Google Sheet

Ever wanted to display some data from a google sheet on your site? Witho...
Automate Google Sheets With Zero Experience

Automate Google Sheets With Zero Experience

By the end of this video you should be knowledgeable about how to automa...
Scrape Google Maps

Scrape Google Maps

Scrape any query in Google Maps into your Google Sheets. Enter your API ...

Related blog posts

Guides that explain getRange in more depth.

Browse the blog

Related glossary terms

Done reading about getRange?

Membership unlocks 636+ tutorials, unlimited generators, and every template. Practical lessons. Zero fluff.

Need this once

Jump to a free tool or a single tutorial for this topic.

Learning Sheets for real

Unlock the full library, generators, and templates with membership.