Skip to content
// Sheets · Apps Script

Fix "Cannot call SpreadsheetApp.getUi() from this context" in Apps Script.

Fix SpreadsheetApp.getUi() context errors by separating a spreadsheet menu action from a scheduled check. Use an explicit spreadsheet ID and inspect the execution log before scheduling.

“I am getting "Cannot call SpreadsheetApp.getUi() from this context" when my Apps Script trigger fires and I cannot figure out why it works in the editor but fails on a schedule.”

The script

copy · paste · trigger
Code.gs
Apps Script
// Install in a spreadsheet-bound Apps Script project.
// Use fictional data in a test copy; review log privacy before real use.
const CHECK_SPREADSHEET_ID = 'REPLACE_WITH_TEST_SPREADSHEET_ID';

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Checks')
    .addItem('Count Data rows', 'checkFromMenu')
    .addToUi();
}

function countDataRows() {
  const book = SpreadsheetApp.openById(CHECK_SPREADSHEET_ID);
  const sheet = book.getSheetByName('Data');
  if (!sheet) throw new Error('Expected a sheet named Data.');
  return Math.max(0, sheet.getLastRow() - 1);
}

function checkFromMenu() {
  const rows = countDataRows();
  SpreadsheetApp.getUi().alert('Data rows excluding header: ' + rows);
}

function checkFromSchedule() {
  const rows = countDataRows();
  console.log('Data rows excluding header: ' + rows);
}

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

Walkthrough

Choose the entry point that matches the job

For an interactive result, open the spreadsheet and choose Checks → Count Data rows. This menu path uses the UI of the spreadsheet containing the bound script. A time-driven execution has no current spreadsheet UI for an alert. Set that trigger to checkFromSchedule, which calls the same worker and records its result without a dialog.

If the job is only an occasional check, keep the menu action and skip the trigger. Use a scheduled entry point when the check must run without someone opening the spreadsheet. Additional OAuth scopes do not create an interactive UI context.

Run a small acceptance check before scheduling

Make a test spreadsheet with a Data tab, one header row and two fictional data rows. Replace CHECK_SPREADSHEET_ID with that spreadsheet's ID. Run checkFromSchedule manually and inspect the execution log: the expected result is “Data rows excluding header: 2”. This counts the last populated row minus the header, not the number of nonblank records. Review who can access execution logs before adapting the check to real data.

Reload the bound spreadsheet and run the Checks menu action. Check that its alert reports the same result. Then configure one time-driven trigger for checkFromSchedule and inspect that trigger's actual execution. A successful manual run does not prove the scheduled run completed.

Handle failure without hiding it

Keep missing-sheet and permission failures visible in Executions. Do not wrap the entire worker in a catch block that turns every failure into “UI unavailable”. Add email or other notifications only when the workflow requires them, with an explicit recipient and a separate test of that delivery.

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

3 questions
Does leaving the spreadsheet open fix the scheduled trigger?
No. Choose a background entry point that does not call getUi(). Another browser tab being open does not give a time-driven execution an interactive UI.
Can a standalone script create this spreadsheet menu?
Use a script bound to the spreadsheet for this menu example. The explicit spreadsheet ID makes the worker's target clear; it does not supply an interactive UI to a standalone script.
Does a successful execution prove someone received a notification?
No. This example logs a row count and sends no notification. Verify any later delivery separately.
// one good script a week

Get a working Apps Script snippet in your inbox, weekly.