Skip to content

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.

Restrict a bound Sheets script to the open spreadsheet
/**
 * @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.

Opt a parent project out when a library declares @OnlyCurrentDoc
/**
 * @NotOnlyCurrentDoc
 */
function logLinkedSpreadsheetName() {
  const spreadsheetId = 'PASTE_THE_ID_FROM_YOUR_SPREADSHEET_URL';
  const spreadsheet = SpreadsheetApp.openById(spreadsheetId);
  Logger.log('Linked spreadsheet: ' + spreadsheet.getName());
}

7 of 7

Verify the detected scopes and re-authorize

After adding or changing @OnlyCurrentDoc, save the file and open Overview in the Apps Script editor, then read Project OAuth Scopes. Confirm that the listed URL ends in .currentonly. If you still see the full scope URL, check for a capitalization typo in @OnlyCurrentDoc or an explicit oauthScopes array in appsscript.json.

Editing the annotation changes future authorization prompts, not permissions you already granted to the project. To apply the narrower grant on an account that authorized the script earlier, open https://myaccount.google.com/permissions, remove access for the project, and run a function again.

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.

Keep going

Put it to work

All basics