I have written a simple sidebar for Google Slides that makes a REST call and fetches some images. The user can then Copy-Paste some of these images to their document if they wish. However, my code doesn't depend on or use any Google Slides-specific functionality, and I want to also make it available to Google Docs and Sheets.
Is there any easy way of doing this, without creating a separate app for each Editor program?
I was looking at this Reference page hoping to find a parent class to all DocumentApp, SlidesApp and SheetsApp, but couldn't. Google does define "Utility Services" here, which are not supposed to be tied to any specific App, but I cannot use it because my code needs the "SlidesApp" object to create the Sidebar. Is there a way to decouple Sidebar creation from the specific Google Editor app?
// https://developers.google.com/apps-script/guides/dialogs#custom_sidebars
function onOpen() {
SlidesApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Image Search')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setTitle('Image Search');
SlidesApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
In addition to the solutions provided in the related post mentioned by @TheMaster, you can also implement a simple try...catch statement:
function onOpen() {
try{
SlidesApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Image Search')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
catch(e){
try{
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Image Search')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
catch(e){
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Image Search')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
}
}