How you can Add a Common Customized Menu to A number of Google Workspace Apps

Shubham
5 Min Read

Use Google Apps Script to create a customized menu that can work inside Google Sheets, Google Docs, Slides and Google Varieties.


Including a customized menu in Google Sheets, Docs, Slides, and Varieties utilizing Google Apps Script is simple.

As an illustration, the next code snippet provides a customized menu to the dad or mum Google Sheet that reveals the spreadsheet identify upon clicking.

perform onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('➯ Customized menu');
  menu.addItem('Present spreadsheet identify', 'showName');
  menu.addToUi();
}

perform showName() {
  const fileName = SpreadsheetApp.getActiveSpreadsheet().getName();
  SpreadsheetApp.getUi().alert(fileName);
}

The above code defines two capabilities: onOpen which executes when the app opens, and showName which is triggered when the menu merchandise is clicked.

This method will be utilized to create customized menus in different Google Workspace apps, together with Varieties, Slides, and Google Docs.

The code above is particular to Google Sheets and won’t work should you use it so as to add customized menus in Google Varieties or Slides. That’s as a result of it’s worthwhile to name SpreadsheetApp.getUi() to get the sheet’s UI occasion whereas the Google Type’s UI will be accessed by means of a distinct FormApp.getUi() technique.

This generally is a drawback as a result of some Google add-ons, Document Studio for instance, will be launched from a number of Google Workspace apps. How do you establish the presently energetic Workspace software (Sheets, Docs, Slides, or Varieties) and add menu objects which are particular to that app?

Determine the Present Workspace App

The getContainer perform is your secret weapon for figuring out the presently energetic Google Workspace software. It really works by iterating by means of an inventory of identified app courses, like DocumentApp and SpreadsheetApp.

For every class, it makes an attempt to entry the UI object. If the decision is profitable and doesn’t throw an exception, it signifies that the corresponding app is energetic and returns that app class.

const getContainer = () => {
  const apps = [DocumentApp, SpreadsheetApp, FormApp, SlidesApp];
  const activeApp = apps.discover((app) => {
    strive {
      app.getUi();
      return true;
    } catch (f) {
      return false;
    }
  });

  return activeApp;
};

Now that you already know the present Workspace software, we will modify the onOpen perform to create a dynamic common menu. The menu title comprises the identify of the energetic software and features a menu merchandise to show the app-specific file identify.

const onOpen = () => {
  const app = getContainer();
  const ui = app.getUi();
  const appName = String(app).change('App', '');
  const menu = ui.createMenu(`Customized menu in ${appName}`);
  menu.addItem(`Present ${appName} identify`, 'showAppName');
  menu.addToUi();
};

The showAppName perform makes use of a swap assertion to find out the suitable technique for retrieving the file identify based mostly on the energetic app.

const showAppName = () => {
  const app = getContainer();
  let fileName;
  if (app === DocumentApp) {
    fileName = DocumentApp.getActiveDocument().getName();
  } else if (app === SpreadsheetApp) {
    fileName = SpreadsheetApp.getActiveSpreadsheet().getName();
  } else if (app === SlidesApp) {
    fileName = SlidesApp.getActivePresentation().getName();
  } else if (app === FormApp) {
    fileName = FormApp.getActiveForm().getTitle();
  }
  app.getUi().alert(`You're looking at ${fileName}`);
};
Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *