Reputation: 16112
I am trying to get started with a "Hello World" implementation of a menu that loads a sidebar for a Google Workspace Add-On for Google Sheets.
When I open a new Google Sheet, I expect to see a menu appear under Extensions > MyApp > Start. And when I click the item labeled "Start" in the dropdown menu, I expect to see a sidebar open on the right that reads "Hello World."
Instead of that, what I actually see is when I open a new Google Sheet, I see no MyApp item in the Extensions dropdown menu. It's as if my test deployment was never loaded.
The below code is contained in a standalone script file (as required to make a Workspace Add-on).
In addition to the code I shared below, I also followed the documentation at this link and did the following steps necessary to create a test deployment.
What am I missing or doing wrong?
https://developers.google.com/apps-script/add-ons/cats-quickstart#drive.gs
Create the Apps Script project
Copy the Cloud project number
Set the Apps Script project's Cloud project
Install a test deployment
const ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen( e ) {
ui.createAddonMenu()
.addItem( 'Start', 'showSidebar', )
.addToUi();
const menuItems = [];
menuItems.push({ name: 'Start' , functionName: 'showSidebar' });
ss.addMenu( 'MyApp', menuItems, );
}
function showSidebar() {
// code TBD
// right now, I'm just trying to get the menu to appear
}
<html>
<body>
<div>Hello World</div>
</body>
</html>
{
"timeZone":"America/Los_Angeles",
"dependencies":{
},
"exceptionLogging":"STACKDRIVER",
"runtimeVersion":"V8",
"addOns":{
"calendar":{
"createSettingsUrlFunction":"getConferenceSettingsPageUrl",
"conferenceSolution":[
{
"id":"my-video-conf",
"logoUrl":"https://lh3.googleusercontent.com/...",
"name":"My Video Conference",
"onCreateFunction":"onCreateMyVideoConference"
},
{
"id":"my-streamed-conf",
"logoUrl":"https://lh3.googleusercontent.com/...",
"name":"My Streamed Conference",
"onCreateFunction":"onCreateMyStreamedConference"
}
],
"currentEventAccess":"READ_WRITE",
"eventOpenTrigger":{
"runFunction":"onCalendarEventOpen"
},
"eventUpdateTrigger":{
"runFunction":"onCalendarEventUpdate"
},
"eventAttachmentTrigger":{
"label":"My Event Attachment",
"runFunction":"onCalendarEventAddAttachment"
},
"homepageTrigger":{
"runFunction":"onCalendarHomePageOpen",
"enabled":true
}
},
"common":{
"homepageTrigger":{
"runFunction":"onDefaultHomePageOpen",
"enabled":true
},
"layoutProperties":{
"primaryColor":"#ff392b",
"secondaryColor":"#d68617"
},
"logoUrl":"https://ssl.gstatic.com/docs/script/images/logo/script-64.png",
"name":"Demo Google Workspace Add-on",
"openLinkUrlPrefixes":[
"https://mail.google.com/",
"https://script.google.com/a/google.com/d/",
"https://drive.google.com/a/google.com/file/d/",
"https://en.wikipedia.org/wiki/",
"https://www.example.com/"
],
"universalActions":[
{
"label":"Open settings",
"runFunction":"getSettingsCard"
},
{
"label":"Open Help URL",
"openLink":"https://www.example.com/help"
}
],
"useLocaleFromApp":true
},
"drive":{
"homepageTrigger":{
"runFunction":"onDriveHomePageOpen",
"enabled":true
},
"onItemsSelectedTrigger":{
"runFunction":"onDriveItemsSelected"
}
},
"gmail":{
"composeTrigger":{
"selectActions":[
{
"text":"Add images to email",
"runFunction":"getInsertImageComposeCards"
}
],
"draftAccess":"METADATA"
},
"contextualTriggers":[
{
"unconditional":{
},
"onTriggerFunction":"onGmailMessageOpen"
}
]
},
"docs":{
"homepageTrigger":{
"runFunction":"onEditorsHomepage"
},
"onFileScopeGrantedTrigger":{
"runFunction":"onFileScopeGrantedEditors"
}
},
"sheets":{
"homepageTrigger":{
"runFunction":"onOpen"
},
"onFileScopeGrantedTrigger":{
"runFunction":"onFileScopeGrantedEditors"
}
},
"slides":{
"homepageTrigger":{
"runFunction":"onEditorsHomepage"
},
"onFileScopeGrantedTrigger":{
"runFunction":"onFileScopeGrantedEditors"
}
}
}
}
Upvotes: 1
Views: 930
Reputation: 38218
From the question:
When I open a new Google Sheet, I expect to see a menu appear under Extensions > MyApp > Start.
Strictly speaking, this is not possible with Google Workspace Add-ons. To do this, you should create an Editor Add-on.
From Add-on Types
Workspace Add-ons
Google Workspace Add-ons are the latest generation of add-ons, and provide many capabilities, including: ...
- Use standardized interfaces: Construct user interfaces from built-in widget elements provided by the Apps Script Card service. You don't need any expertise with HTML or CSS to define these interfaces.
Editor Add-ons
Editor Add-ons behave differently from Google Workspace Add-ons in the following ways:
...
- Editor Add-ons can create interfaces consisting of menu items, dialogs, and sidebars. Add-ons dialogs and sidebars are defined using standard HTML and CSS.
It's worth noting that you could have both types of add-ons in the same Apps Script project. The way to test each of them varies a bit.
When publishing the add-on, you might use a single Cloud Platform project and have a single Workspace Marketplace listing.
However, the custom menu can't be included if you switch to another runtime. The Google Workspace Add-on API could get a JSON with card design but not for the custom menus. The Google Sheets API, as well as the other editors' API, doesn't include methods to create user interfaces like a custom menu.
Resources
Related
Upvotes: 2
Reputation: 152095
It's a common misconception that with Google Workspace add-ons you can't create custom menus. Yes, you can. Here's the Google Workspace Add-on quickstart (linked by Rubén) modified to add a custom menu:
The modification was to edit the appscript.json and add a section for Sheets,
"sheets": {
"homepageTrigger": {
"runFunction": "onSheetsHomepage"
}
}
then define onSheetsHomepage
to create a custom menu:
function onSheetsHomepage(e) {
SpreadsheetApp.getUi().createAddonMenu().addItem('Yes it is possible to create custom menus for Google Workspace add-ons', 'menuFunction').addToUi();
return createCatCard('This card was created from Sheets!', true);
}
function menuFunction() {
console.log('Google Workspace add-on custom menu function was called');
}
Clicking the custom menu item does execute the function:
The misconception probably stems from the fact that you can't define custom menus using the card service. But using the card service is only required if you use alternate runtimes, not if you use Apps Script to build the Google Workspace add-on.
The inability to use alternate runtimes with custom menus is a bummer though, because they enable you to develop Google Workspace Add-ons with your preferred hosting infrastructure, development tool chain, source control system, coding language, and code libraries. Plus, you're not subjected to the numerous Apps Script limitations.
I've filed a feature request to allow specifying custom menus via the card service.
Upvotes: 1