Yes, you can get a Google Sheets URL from a script using Google Apps Script: call SpreadsheetApp.getActiveSpreadsheet().getUrl() to return the full URL of the current spreadsheet. For a spreadsheet opened by ID, use SpreadsheetApp.openById('SPREADSHEET_ID').getUrl(). This works in both bound and standalone scripts, and the URL is the same one you see in your browser’s address bar.
Why You Might Need the URL in a Script
Automation often requires sharing a link or logging where data lives. Common use cases include:
- Sending a spreadsheet link in an email notification after a form submission.
- Storing the URL in a cell or a separate log sheet for audit trails.
- Generating a clickable link in a Google Doc or Slack message via webhook.
- Creating a backup copy and recording its new URL.
Because getUrl() returns a string, you can concatenate it with other text or use it directly in MailApp.sendEmail() or UrlFetchApp.
How to Get the URL: Code Examples
Here are the three most common ways, with sample code you can paste into the Apps Script editor (Extensions > Apps Script).
1. Active spreadsheet (bound script)
function getActiveUrl() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getUrl();
Logger.log(url);
}
2. Spreadsheet by ID (standalone script)
function getUrlById() {
var id = 'YOUR_SPREADSHEET_ID';
var ss = SpreadsheetApp.openById(id);
var url = ss.getUrl();
Logger.log(url);
}
3. Get URL of a specific sheet (tab) inside the spreadsheet
Note: getUrl() returns the spreadsheet’s URL, not a specific tab. To link to a particular tab, append #gid= plus the sheet’s ID (which you can get with sheet.getSheetId()):
function getSheetUrl() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetId = sheet.getSheetId();
var url = ss.getUrl() + '#gid=' + sheetId;
Logger.log(url);
}
That last one is handy when you want to jump directly to a specific tab.
Understanding the URL Structure
A typical Google Sheets URL looks like this:
https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/edit#gid=0
Breaking it down:
- Base domain – always
docs.google.com/spreadsheets/d/ - Spreadsheet ID – a long alphanumeric string unique to the file.
- /edit – the view mode; you can replace with
/copyor/exportfor other actions. - #gid= – the sheet tab’s ID (0 for the first sheet).
When you call getUrl(), you get the full /edit URL without the #gid part. That’s why you need to append it manually if you want a specific tab.
Common Problems and Solutions
Even though getUrl() is straightforward, users run into a few issues:
- “Cannot read property ‘getUrl’ of null” – This happens when
SpreadsheetApp.getActiveSpreadsheet()returns null because the script isn’t bound to a spreadsheet (e.g., you opened a standalone script and ran it). UseopenById()instead. - URL is not updating after renaming the file – The URL contains the ID, not the name, so it stays the same. That’s expected.
- Getting a URL for a sheet in another file – Use
SpreadsheetApp.openByUrl(url)first to get the spreadsheet object, then callgetUrl()on it. - URL appears as “undefined” in logs – Check that you declared the variable correctly and that the spreadsheet exists. Also ensure you’re using the correct case:
getUrl()(capital U).
How to Use the URL in Email or Cell
Once you have the URL string, you can embed it in a formula or a script. For example, to put the URL in cell A1:
function putUrlInCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getUrl();
ss.getActiveSheet().getRange('A1').setValue(url);
}
To email it:
function emailUrl() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getUrl();
MailApp.sendEmail('recipient@example.com', 'Spreadsheet link', 'Here is your link: ' + url);
}
You can also make the cell a hyperlink using the HYPERLINK formula in the sheet itself (not via script): =HYPERLINK("URL","Click here").
Alternatives: Getting the URL Without Script
If you only need the URL once, you don’t need a script:
- Copy it from the browser address bar.
- In Google Sheets, go to File > Share > Publish to web – this gives a different link that you can embed.
- Use the
HYPERLINKfunction manually in a cell.
Scripts are only necessary when you need to automate the retrieval, such as in a trigger or a custom function.
Security and Sharing Considerations
Remember that the URL alone does not grant access. The file’s sharing permissions (Viewer, Commenter, Editor) determine who can open it. If you send the URL to someone without access, they’ll see a “Request access” screen. Always check the sharing settings before distributing a link.
Also, be careful when storing URLs in scripts or logs – if the script is shared, others may see the URL. For sensitive spreadsheets, keep the script private and avoid logging full URLs.
Frequently Asked Questions
Q: Can I get the URL of a specific cell or range?
No, Google Sheets URLs only point to the spreadsheet or a specific tab (via #gid). You cannot link to a specific cell in the URL. To highlight a cell, you’d need to use the Google Sheets API with a custom view, which is not supported in the standard URL.
Q: Does getUrl() work in a custom function?
Yes, but with restrictions. Custom functions cannot access the active spreadsheet’s URL directly because they don’t have access to the spreadsheet’s metadata. You’d need to use SpreadsheetApp.getActiveSpreadsheet().getUrl() inside a custom function, but it will return null. Instead, use a regular function triggered from a menu or button.
Q: How do I get the URL of a spreadsheet from a different Google account?
You can’t. Apps Script runs under the account that owns the script. To access a spreadsheet from another account, you need to use the Drive API with OAuth2, which is more complex. For most cases, you’ll work with spreadsheets in the same account.
Q: Why does my URL have a different format when I use getUrl()?getUrl() always returns the standard format: https://docs.google.com/spreadsheets/d/ID/edit. If you see a different format (like a shortened link), it’s because someone used a URL shortener or a different sharing method. The script returns the canonical URL.
Q: Can I get the URL of a spreadsheet that is not open?
Yes, use SpreadsheetApp.openById(id) or openByUrl(url) to open it programmatically, then call getUrl(). This works for any spreadsheet you have access to, as long as you have the ID or URL.
Related
- R Add Year to Date? How to Do It in R
- Online Casio Graphing Calculator: How to Use It
- Balancing a Budget Worksheet: Step-by-Step Guide
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Add Values to a Dropdown List: Easy MethodsTo add a value to an Excel dropdown, edit the list named in its Data Validation settings or…
- How to Create a Template Form in Word: Step by StepCreate a reusable form in Word by adding content controls for text, dates, checkboxes, and dropdowns, restricting editing…
- How to Save As in Excel and Create a New CopyTo save an Excel workbook under a different name or in another location, open it and choose File…
- Google Sheets: How to Duplicate ColumnsTo duplicate a column in Google Sheets, click its letter at the top, copy it with Ctrl+C (Command+C…
- Free Calendar Excel Templates: How to ChooseYes—free calendar Excel templates are available, including templates you can find from Excel’s New screen and downloadable workbooks…
- CSV vs Excel Workbook: Key Differences and When to Use EachA CSV file stores rows and columns of plain text, while an Excel workbook can store data alongside…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.