Apps Script Get UI

The getUi() context error in Google Apps Script occurs when you try to call getUi() outside of a valid UI context. Here’s why it happens and how to fix it.


🔴 Error Message

Exception: Cannot call getUi() from this context.

🛠️ Why Does This Happen?

  1. You’re running the function manually from the script editor.
    • getUi() works only when the script is executed within the UI of a Google Workspace app (Docs, Sheets, Slides, or Forms).
    • Running it manually from the Apps Script editor does not have a UI context.
  2. You’re using getUi() in a standalone script (not bound to a document).
    • If your script is not attached to a Google Sheets, Docs, or Slides file, there is no UI to modify.
  3. You’re trying to use getUi() in a Time-Driven trigger.
    • Time-based triggers (e.g., running every hour) run in the background and do not have access to the UI.

✅ Correct Ways to Use getUi()

Solution 1: Ensure It’s a Bound Script

If your script is meant for Google Sheets, make sure it’s bound to a Google Sheet file:

  • Open Google SheetsExtensions → Apps Script.
  • Paste your script inside this bound project.

Then, use:

function onOpen() {
var ui = SpreadsheetApp.getUi(); // Works only in bound scripts
ui.createMenu('My Custom Menu')
.addItem('Say Hello', 'sayHello')
.addToUi();
}

function sayHello() {
SpreadsheetApp.getUi().alert('Hello, World!');
}

Fix: Now, reload the Google Sheet, and the menu should appear.


Solution 2: Do Not Run onOpen() Manually

🚫 Do NOT click “Run” in Apps Script Editor for onOpen()
Instead, reload the Google Sheet, and the function will execute automatically.


Solution 3: Use getUi() Only in Interactive Functions

If you need to run a UI-related function manually, do not use onOpen(). Instead, create a separate function:

function showPrompt() {
var ui = SpreadsheetApp.getUi();
var response = ui.prompt("Enter your name:");
Logger.log(response.getResponseText());
}

Fix: Run showPrompt() from Google Sheets, not the Apps Script Editor.


Solution 4: Use Installable Triggers for UI Modifications

If you need onOpen() to run automatically and modify the UI, but it’s not working, set up an Installable Trigger:

  1. Go to Apps Script Editor.
  2. Click on Triggers (clock icon).
  3. Add a new trigger:
    • Function: onOpen
    • Event Source: From spreadsheet
    • Event Type: On open

Fix: Now onOpen() will run every time the file is opened.


🚀 Summary

What Not to DoWhat to Do Instead
Run onOpen() manually in Apps Script EditorReload the file so it runs automatically
Use getUi() in a standalone scriptUse it only in a bound script (Docs, Sheets, Slides)
Call getUi() in a time-driven triggerCall it in an interactive function triggered by a user

Still Not Working?

  • Are you running the script in a bound Google Sheets/Docs/Slides script?
  • Are you reloading the document instead of running onOpen() manually?
  • Are you not using a time-driven trigger?

Try these solutions and let me know what issue you’re facing! 🚀