问题
Does createMenu work differently for slides than it does for spreadsheets?
Is this a bug or is something wrong with my code? Should it be reported?
SIMPLE TEST - - - - - - - DOES NOT CREATE MENU ON OPEN FROM DRIVE OR REFRESH
function onOpen() {
console.log('In onOpen' );
const pres = SlidesApp.getActivePresentation();
const slideSet = pres.getSlides();
let ui;
try {
ui = SlidesApp.getUi();
console.log('getUi succeeded' );
} catch (e) {
console.log('caught in getUi: ', e );
}
try {
ui.createMenu( 'Ask ?')
.addItem('BE1', 'BE1')
.addToUi();
} catch (e) {
console.log('caught in createMenu: ', e );
}
console.log(' after create menu');
// let result = ui.alert('sample title', 'Please click "ask?" to begin.', ui.ButtonSet.OK);
}
For this one there is no AUTH problem as there is almost no code.
Manifest
{
"timeZone": "America/Mexico_City",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
If you are curious the function called from the menu - BE1
/**
* Create a new square textbox, using the supplied element ID.
*/
function BE1 () {
let pres = SlidesApp.getActivePresentation();
let slideSet = pres.getSlides();
let shape = slideSet[0].insertShape(SlidesApp.ShapeType.TEXT_BOX, 200, 300, 60, 60);
let textRange = shape.getText();
textRange.setText('Hello world!');
}
It really looks like create menu does not work upon slide presentation open or refresh despite Google documentation. Is this a bug? Can someone who knows how please report it?
= = = = = = = = = = = = = = = = = = =
Presentation That does open window.
function onOpen() {
let ui = SlidesApp.getUi();
let pres;
let slideSet = [];
try {
pres = SlidesApp.getActivePresentation();
slideSet = pres.getSlides();
} catch (err) {
console.log('get active presentation err: ', err);
}
SlidesApp.getUi()
.createMenu( 'Ask ?')
.addItem('BE1','BE1')
.addToUi();
}
Manifests:
{
"timeZone": "America/Mexico_City",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
Error when loaded from drive: get active presentation err: { [Exception: Authorization is required to perform that action.] name: 'Exception' }
If you stop the program because of the error, the menu is not created. If you run from the script editor you do not get the authorization error.
来源:https://stackoverflow.com/questions/65615455/google-apps-script-custom-menu-createmenu-not-displaying-in-slide-when-present