// Sheets · Apps Script

Get a spreadsheet's ID from its URL or the active file.

Two ways to pull a Google Sheets spreadsheet ID in Apps Script: getActiveSpreadsheet().getId() for the file you're in, and a one-line regex to extract it from any pasted /spreadsheets/d/<id>/edit URL.

I need the spreadsheet ID so I can open or reference a specific Google Sheet in my Apps Script code, either the file I'm already editing or one I only have a URL for.

The script

copy · paste · trigger
getSpreadsheetId.gs
Apps Script
// Two ways to get a spreadsheet ID in Apps Script
// 1. Active file  2. Extract from a URL string

function getActiveFileId() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var id = ss.getId();
  Logger.log('Active spreadsheet ID: ' + id);
  return id;
}

function extractIdFromUrl(url) {
  // Matches the 44-char ID between /spreadsheets/d/ and the next slash
  var match = url.match(/\/spreadsheets\/d\/([a-zA-Z0-9_-]+)/);
  if (!match) {
    throw new Error('No spreadsheet ID found in URL: ' + url);
  }
  return match[1];
}

function openByUrl() {
  var url = 'https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms/edit';
  var id = extractIdFromUrl(url);
  var ss = SpreadsheetApp.openById(id);
  Logger.log('Opened: ' + ss.getName());
}

Need a variant? Gnaw writes a custom version from one sentence — fields, triggers, edge cases handled.

Walkthrough

The one-liner for the file you're already in

If your script is bound to a spreadsheet (meaning it was created from Extensions > Apps Script inside that file), SpreadsheetApp.getActiveSpreadsheet().getId() returns the ID without any URL parsing. The ID is a 44-character alphanumeric string that uniquely identifies the file in Drive, regardless of what you rename it later.

The first time I needed this, I spent ten minutes digging through Drive API docs before realizing the bound-script shortcut existed. If you're in a standalone script rather than a bound one, getActiveSpreadsheet() returns null, and you'll need openById() or openByUrl() with an explicit ID instead.

Once you have the ID, you can store it in PropertiesService.getScriptProperties() so a standalone utility script can reference the same file across runs without hardcoding the URL.

Pulling the ID out of a pasted URL

A Google Sheets URL always contains the segment /spreadsheets/d/ followed immediately by the file ID, then a slash. The regex /\/spreadsheets\/d\/([a-zA-Z0-9_-]+)/ captures everything between that prefix and the next delimiter, which covers the standard /edit, /view, and /copy suffixes as well as URLs with #gid= sheet anchors.

match() returns an array where index 0 is the full match and index 1 is the first capture group, so match[1] is the clean ID. If the URL is malformed or the user pastes something that isn't a Sheets link, match returns null, which is why the explicit null-check and descriptive error message matters here — a silent undefined sneaking into openById() produces a confusing 'Argument must be a string' error far from the actual mistake.

This pattern handles URLs copied from the browser address bar, shared links with ?usp=sharing appended, and the embed URLs Sheets generates for publishing, because the /spreadsheets/d/ segment is consistent across all of them.

When to use openById vs openByUrl

SpreadsheetApp.openById(id) is the right call once you have the ID string. SpreadsheetApp.openByUrl(url) also exists and accepts the full URL directly, but it does the same regex extraction internally and then calls openById, so skipping the extraction step is fine if you want less code. The explicit extraction approach is useful when you need the ID itself, not just an open spreadsheet, such as when building a Drive API URL, passing the ID to a Sheets API batchUpdate call, or storing it in a properties key.

Both methods require that the script's running user has at least view access to the target file. If the file is in a shared drive, the OAuth scope https://www.googleapis.com/auth/spreadsheets covers it, but a Drive-restricted scope won't. The appsscript.json manifest needs 'oauthScopes' to include spreadsheets if you're deploying as an add-on.

Want a custom version?

Describe your sheet and the rule you want. Gnaw writes the Apps Script — fields, triggers, edge cases — in one shot.

FAQ

4 questions
Where do I find the spreadsheet ID in the URL?
It's the long string between /spreadsheets/d/ and the next forward slash. In https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms/edit, the ID is 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms.
Why does getActiveSpreadsheet() return null in my script?
Your script is standalone rather than bound to a spreadsheet. Standalone scripts have no 'active' spreadsheet. Use SpreadsheetApp.openById('your-id-here') with the ID you copied from the URL.
Does the spreadsheet ID change if I rename the file or move it to a different folder?
No. The ID is assigned when the file is created and stays the same forever. Renaming, moving to a different Drive folder, or even moving between shared drives does not change it.
Can I use SpreadsheetApp.openByUrl() instead of extracting the ID manually?
Yes. SpreadsheetApp.openByUrl(url) accepts the full URL and returns the Spreadsheet object directly. Use extractIdFromUrl() only when you need the ID string itself, for example to pass to the Sheets REST API or store in script properties.
// one good script a week

Get a working Apps Script snippet in your inbox, weekly.