// Drive · Apps Script

Fix "No item with the given ID could be found" in Apps Script.

The "No item with the given ID could be found" error in Apps Script almost always means the script is running as an account that lacks access to the file, or the ID string is malformed. Here is how to diagnose and fix both causes.

I'm getting "No item with the given ID could be found" in my Apps Script even though the file opens fine in my browser, and I need to understand why the script can't find it.

The script

copy · paste · trigger
openById-safe.gs
Apps Script
// Opens a file by bare ID, logs the effective account,
// and catches access errors with a readable message.

function openSheetSafely() {
  var fileId = '1A2B3C4D5E6F7G8H9I0J'; // bare ID only, no URL
  var effectiveUser = Session.getEffectiveUser().getEmail();
  Logger.log('Running as: ' + effectiveUser);

  try {
    var ss = SpreadsheetApp.openById(fileId);
    Logger.log('Opened: ' + ss.getName());
  } catch (e) {
    Logger.log('ERROR — check that ' + effectiveUser + ' has at least Viewer access to ' + fileId);
    Logger.log(e.message);
  }
}

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

Walkthrough

The script runs as a different account than you

Apps Script executes as the account that authorized the script, which is not always the account currently open in your browser. When a script runs from a time-driven trigger, it runs as the trigger owner. When deployed as a web app with "Execute as: Me," it runs as the deploying account. When deployed as "Execute as: User accessing the web app," it runs as whoever hits the URL, but only if that person has also granted OAuth consent. In none of these cases does the runtime account automatically inherit your own Drive permissions.

The first thing to log is Session.getEffectiveUser().getEmail(). Paste that address into Drive and share the target file with it, with at least Viewer access. That single step resolves the error in the majority of cases I have seen. If the file is in a Shared Drive and that account is not a member, you also need to add it to the drive membership, not just share the specific file.

Service accounts used with the Advanced Drive Service follow the same rule. The service account's email (something like [email protected]) needs explicit Drive access, because it has no ambient access to anything in your personal Drive.

Extracting the bare ID from a URL

A Google Sheets URL looks like https://docs.google.com/spreadsheets/d/1A2B3C4D5E6F7G8H9I0J/edit#gid=0. The ID is the segment between /d/ and the next slash: 1A2B3C4D5E6F7G8H9I0J. Passing the full URL to SpreadsheetApp.openById() throws this exact error, because the method expects only the ID segment.

Whitespace is the quiet version of the same problem. If you copy the ID from a cell in a spreadsheet, a trailing newline or a non-breaking space (common when copying from some browser UIs) gets embedded invisibly. Add .trim() when reading IDs from cells: var fileId = sheet.getRange('B2').getValue().toString().trim(). I keep a one-line trim wrapper in a utils file because I have been burned by this more than once.

Drive folder IDs work the same way. DriveApp.getFolderById() and DriveApp.getFileById() both require the bare ID, and both throw the same error message for the wrong account or a URL-shaped string.

Trigger ownership and shared scripts

Container-bound scripts (attached to a Sheet or Doc) are owned by whoever created the file, but triggers on those scripts run as whoever installed the trigger. Two people can install two separate triggers from the same script, and each trigger runs as its own installer. If a colleague installs the trigger and then loses access to a file the script opens, their trigger fails. Yours still works. This asymmetry is the reason "it works for me but not for them" is almost always a trigger-ownership issue, not a code bug.

To audit trigger ownership, open the Apps Script editor, go to Triggers (the clock icon), and check the owner column. If the trigger owner is not the account that has Drive access to all files the script touches, either re-install the trigger as the correct account or share the relevant files with the current trigger owner.

For scripts that must access files across multiple accounts reliably, the cleanest approach is a service account with domain-wide delegation (if on Google Workspace) or making the files accessible to anyone with the link and ensuring the runtime account is authenticated. Domain-wide delegation is admin-configured and outside the scope of a single script fix, but it is worth knowing it exists when per-user sharing becomes unmanageable.

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
Why does the error only happen on the time-driven trigger, not when I run the function manually?
Manual runs execute as your logged-in account, which has Drive access. The time-driven trigger runs as the trigger owner (the account that created the trigger). If that account differs from yours, it may not have access to the file. Log Session.getEffectiveUser().getEmail() inside the trigger function to confirm which account is actually executing.
I shared the file with the script's account but still get the error — what else could it be?
Three things to check in order: (1) the ID string has stray whitespace or is a full URL — add .trim() and extract just the segment between /d/ and the next slash; (2) the file is in a Shared Drive and the account is not a drive member, not just a file-level share; (3) the file was moved to Trash — trashed files return this same error even when the ID is valid and access is granted.
My web app uses "Execute as: User accessing the web app" and users report the error randomly.
Each user must have independently authorized the OAuth scopes the script requires. If a user hits the web app without completing OAuth consent (for example, via a direct API call or a stale bookmark), the script runs with no Drive token and cannot open any file. Make sure the web app redirects unauthenticated users through the OAuth flow before executing any Drive operations.
DriveApp.getFileById() and SpreadsheetApp.openById() both throw this error — are they the same fix?
Yes. Both methods require the same two conditions: a bare file ID (no URL, no whitespace) and Drive access for the effective account at runtime. The error message is identical across DriveApp, SpreadsheetApp, DocumentApp, and SlidesApp because they all delegate to the same underlying Drive API call.