Diagnostic reference
Cannot call SpreadsheetApp.getUi() from this context
Fix Exception: Cannot call SpreadsheetApp.getUi() from this context in Apps Script when running standalone scripts, time triggers, web apps, or custom functions.
Updated December 6, 2026
Apps Script error
Exception: Cannot call SpreadsheetApp.getUi() from this context.
What it means
SpreadsheetApp.getUi() (and DocumentApp.getUi(), SlidesApp.getUi(), FormApp.getUi()) requires an open Google Workspace editor window attached to a container-bound script. Calling it from a standalone script, a time-driven or form-submit trigger, a web app (doGet/doPost), or a cell custom function fails because no editor UI session exists.
Related error messages
- Exception: Cannot call DocumentApp.getUi() from this context.
- Exception: Cannot call SlidesApp.getUi() from this context.
- Exception: Cannot call FormApp.getUi() from this context.
- Exception: You do not have permission to call SpreadsheetApp.getUi.
Before and after
The code before and after the fix.
function runDailyCleanup() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Archive');
if (!sheet) {
// Fails when run from a time-driven trigger, web app, or standalone project.
SpreadsheetApp.getUi().alert('Archive sheet is missing.');
return;
}
Logger.log('Checked ' + sheet.getName());
}function runDailyCleanup() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Archive');
if (!sheet) {
Logger.log('Archive sheet is missing.');
return;
}
Logger.log('Checked ' + sheet.getName());
}How to fix it
- Replace getUi().alert() and getUi().prompt() with Logger.log() or console.log() inside background triggers and standalone scripts.
- To show an alert or menu, put the script inside the file via Extensions > Apps Script and run it from the open file or a custom menu item.
- Never call getUi() inside a custom cell function (=MYFUNC()) or inside doGet() / doPost(); return a value or HtmlService output instead.
Things to check
- Even inside a bound script, running onOpen() directly from the script editor toolbar can fail if the parent document is not open in an interactive session.
- myFunction flags getUi() calls in web apps, background triggers, and custom functions statically under rules MF93010 and MF91003.
In the Apps Script editor
Catch errors like this before clicking Run.
myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes. Writing Apps Script with an AI coding agent? Connect the Apps Script MCP server to run the same static checks before pasting code.