Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created December 4, 2017 05:32
Show Gist options
  • Save tanaikech/f05e7d82b754b0348463b8dec1c741ad to your computer and use it in GitHub Desktop.
Save tanaikech/f05e7d82b754b0348463b8dec1c741ad to your computer and use it in GitHub Desktop.
Retrieving Instance of User-Interface Environment

Retrieving Instance of User-Interface Environment

This sample script is for retrieving an instance of user-interface environment for Spreadsheet, Document and Slides. When I create applications which use user interface (for example, sidebar, dialog and so on), the user interface can be used for Spreadsheet, Document and Slides. If the application doesn't use the methods depend on Spreadsheet, Document and Slides, this script can give 3 choices to users.

function getUi() {
    var ui;
    try {
        ui = SpreadsheetApp.getUi();
    } catch(e) {}
    try {
        ui = DocumentApp.getUi();
    } catch(e) {}
    try {
        ui = SlidesApp.getUi();
    } catch(e) {}
    return ui || null;
}

function main() {
    var ui = getUi();
    if (ui) {
        ui.alert('Hello, world!');
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment