Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.