googleworkspace / apps-script-samples

Apps Script samples for Google Workspace products.
https://developers.google.com/apps-script
Apache License 2.0
4.49k stars 1.83k forks source link

How to have a Google Workspace Add-on homepageTrigger multiple times? #435

Open catmanjan opened 8 months ago

catmanjan commented 8 months ago

We have a Google Workspace add-on that operates via a sidebar, so we use homepageTrigger so that the user can open the sidebar from the right hand menu.

This works well - but only once per page load - so if the user opens the sidebar, closes it and attempts to reopen via the right hand menu we instead get this page:

bEMcc

How instead can we have the homepageTrigger function run every time?

Here is the appsscript.json:

{
  "timeZone": "Australia/Sydney",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "Drive",
        "serviceId": "drive",
        "version": "v2"
      }
    ]
  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.scriptapp",
    "https://www.googleapis.com/auth/documents.currentonly",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/presentations",
    "https://www.googleapis.com/auth/script.container.ui",
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/drive.apps.readonly",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/script.locale",
    "https://www.googleapis.com/auth/userinfo.email"
  ],
  "urlFetchWhitelist": [
    "https://cm234-wgs-1.icognition.cloud/",
    "https://docs.google.com/feeds/download/documents/export/Export",
    "https://docs.google.com/spreadsheets/export",
    "https://docs.google.com/feeds/download/presentations/Export"
  ],
  "addOns": {
    "common": {
      "name": "Content Manager",
      "logoUrl": "https://cm234-wgs-1.icognition.cloud/CMServiceApiGoogle/TrimIcon/W64h64/trim.png",
      "homepageTrigger": {
        "runFunction": "showSidebar",
        "enabled": true
      }
    },
    "sheets": {},
    "docs": {},
    "slides": {}
  }
}

Here is the function it is calling (when I add a log line at the start of this function it is only ever called once):

function showSidebar() {
  var sidebarName = "sidebar";

  if (getRecordUri() !== 0) {
    sidebarName = "existing_record_sidebar";
  }

  var ui =
    HtmlService.createHtmlOutputFromFile(sidebarName).setTitle(
      "Content Manager",
    );
  getUi().showSidebar(ui);
}