Basics
Limit file access with @OnlyCurrentDoc
Add /** @OnlyCurrentDoc */ to a bound Sheets, Docs, Slides, or Forms script so Apps Script requests access only to the open file instead of every file of that type in Google Drive.
1 of 7
What @OnlyCurrentDoc does
When a script leaves automatic scope detection on and calls SpreadsheetApp, DocumentApp, SlidesApp, or FormApp, Apps Script requests broad access to every file of that type in your Google Drive. Adding /** @OnlyCurrentDoc */ in a file-level JSDoc comment tells Apps Script to request the matching currentonly scope instead.
With a currentonly scope, the consent screen asks for access only to the files where the script or add-on is used. The script can still read and write the open file, because currentonly limits which files the script can reach rather than making access read-only.
- SpreadsheetApp changes from https://www.googleapis.com/auth/spreadsheets to https://www.googleapis.com/auth/spreadsheets.currentonly.
- DocumentApp changes from https://www.googleapis.com/auth/documents to https://www.googleapis.com/auth/documents.currentonly.
- SlidesApp changes from https://www.googleapis.com/auth/presentations to https://www.googleapis.com/auth/presentations.currentonly.
- FormApp changes from https://www.googleapis.com/auth/forms to https://www.googleapis.com/auth/forms.currentonly.
2 of 7
Exact syntax and capitalization
Place /** @OnlyCurrentDoc */ in a JSDoc block comment at the top of a .gs file. Apps Script scans every script file in the project, so one file-level annotation applies to automatic scope detection across the project.
Apps Script checks the tag with exact capitalization: capital O, capital C, and capital D. Writing @onlycurrentdoc or @onlyCurrentDoc is ignored without an error, and Apps Script silently requests the full service scope instead.
/**
* @OnlyCurrentDoc
*/
function logCurrentSheetSummary() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getActiveSheet();
Logger.log(spreadsheet.getName() + ' / ' + sheet.getName() + ': ' + sheet.getLastRow() + ' rows.');
} 3 of 7
Methods that fail under @OnlyCurrentDoc
A currentonly scope authorizes only the open container document. Any built-in editor method that opens another file or creates a new file fails at runtime with a permission error when the project holds only a currentonly scope.
Use @OnlyCurrentDoc in container-bound scripts and editor add-ons that call getActiveSpreadsheet(), getActiveDocument(), getActivePresentation(), or getActiveForm(). Standalone scripts have no container file, so those active-file methods return null and @OnlyCurrentDoc leaves the script unable to open target files by ID.
- openById() and openByUrl() on SpreadsheetApp, DocumentApp, SlidesApp, and FormApp require the full service scope, not currentonly.
- create() on those four services creates a new file outside the current document and requires the full service scope.
- If your script needs to open another spreadsheet or copy data from a template file by ID, remove @OnlyCurrentDoc or list explicit oauthScopes in appsscript.json.
4 of 7
Services that @OnlyCurrentDoc does not narrow
@OnlyCurrentDoc narrows only the four built-in editor services: SpreadsheetApp, DocumentApp, SlidesApp, and FormApp. It has no effect on other Google services or direct HTTP requests.
If a bound spreadsheet script also calls GmailApp, CalendarApp, DriveApp, or UrlFetchApp, Apps Script still requests the normal scopes for those services alongside spreadsheets.currentonly.
- Advanced Google Services (such as the Advanced Sheets, Docs, Slides, or Drive APIs) do not work with currentonly scopes and need their standard API scopes.
- Direct REST requests sent with UrlFetchApp and ScriptApp.getOAuthToken(), including Google Sheets /export PDF downloads, are not authorized by spreadsheets.currentonly.
- DriveApp, GmailApp, and CalendarApp have no current-document equivalent and keep their regular scopes.
5 of 7
How appsscript.json oauthScopes overrides it
@OnlyCurrentDoc changes automatic scope detection only. When your appsscript.json manifest defines an oauthScopes array, Apps Script ignores @OnlyCurrentDoc completely and requests the exact list in appsscript.json.
Pick one mechanism per project so the manifest and source files never disagree. Keep automatic detection with /** @OnlyCurrentDoc */ for small bound scripts, or list https://www.googleapis.com/auth/spreadsheets.currentonly directly in appsscript.json when you need explicit control over every scope in the project.
6 of 7
Override a library with @NotOnlyCurrentDoc
If your project imports an Apps Script library that declares @OnlyCurrentDoc, automatic scope detection can narrow the parent project to current-document scopes too. When the parent script needs to open other files with openById() or openByUrl(), that inherited restriction causes a runtime permission error.
Add /** @NotOnlyCurrentDoc */ in a file-level JSDoc comment in the parent project to opt back out of current-only narrowing, or declare explicit oauthScopes in appsscript.json.
/**
* @NotOnlyCurrentDoc
*/
function logLinkedSpreadsheetName() {
const spreadsheetId = 'PASTE_THE_ID_FROM_YOUR_SPREADSHEET_URL';
const spreadsheet = SpreadsheetApp.openById(spreadsheetId);
Logger.log('Linked spreadsheet: ' + spreadsheet.getName());
} Remember
The short version
- Use exact capitalization in /** @OnlyCurrentDoc */; lowercase @onlycurrentdoc is ignored by Apps Script.
- @OnlyCurrentDoc narrows built-in SpreadsheetApp, DocumentApp, SlidesApp, and FormApp calls to their .currentonly scopes, which still allow both reading and writing the open file.
- openById(), openByUrl(), create(), Advanced Services, and direct REST or /export requests are not covered by .currentonly scopes.
- An explicit oauthScopes array in appsscript.json overrides @OnlyCurrentDoc, so use one or the other.
- Use /** @NotOnlyCurrentDoc */ in a parent project if an imported library declares @OnlyCurrentDoc and the parent project needs broader file access.
- Check Overview, then Project OAuth Scopes, and revoke prior grants at https://myaccount.google.com/permissions to test the narrowed consent screen.
SpreadsheetApp.SpreadsheetSpreadsheetApp.SheetDocumentApp.DocumentSlidesApp.PresentationFormApp.FormKeep going
Put it to work
- Task guide Add a custom menu to a Google Sheet
Add a menu named Helpful tools to the menu bar of a Google Sheet. Its one item shows which cell is selected. Save the script, reload the Sheet, and the menu appears.
- Task guide Add a timestamp when a row changes
When you change a status cell, the script writes the current date and time into the same row. You save the script and nothing else needs to be installed.
- Task guide Write a custom function in Google Sheets
Write your own formula for Google Sheets. This one converts Celsius to Fahrenheit, works on one cell or a whole range, and shows a clear message when a cell holds text instead of a number.
- Basics Apps Script permissions and authorization
Before a script can read your sheet or send your mail, you give it permission to access a list of OAuth scopes. This page walks through each authorization screen, explains @OnlyCurrentDoc, and shows how to revoke access.
- Basics Bound scripts and standalone scripts
Every Apps Script project is either attached to one Google file or standing on its own. The kind of project affects how your code opens files, who can see the code, and where you find it later.
- Basics Common Apps Script errors and what they mean
Apps Script error messages say what failed, for example TypeError: Cannot read properties of null (reading 'getRange') or ReferenceError. Find the exact message below for its cause and fix.
- Diagnostic You do not have permission to call X
Fix permission errors from simple triggers, custom functions, explicit oauthScopes, and @OnlyCurrentDoc conflicts with openById().
- Diagnostic Authorization is required to perform that action
Fix triggers and scripts that fail with Authorization is required after changing scopes or revoking access.
- Book chapter Understand the runtime and permissions
Compare two Apps Script projects to see where code runs, what a bound project adds, and how the manifest, OAuth scopes, and running account limit access.
- Book chapter Share a versioned Apps Script library
Build a reusable Google Apps Script library project, attach it by Script ID at a pinned version, and manage consumer upgrades and rollbacks.
- Book chapter Install and release a Workspace add-on
Install the Drive checklist add-on for testing, rehearse a versioned release with a draft tester, and prepare the Marketplace listing and OAuth settings.