I've just started kicking around some code ideas for a new project in Google slides and noticed that the onOpen trigger is not working, unless it is run from the code editor. When the bulk of the code is stripped out and applied to a dummy file the trigger works again - but I can't seem to isolate what the problem is by trial and error.
Have you any ideas what might be blocking it or how to avoid this problem in the future?
As an example, the onoPen() trigger works with the code below - but only when the last ten lines are commented out. I understand the last ten lines will need permission from the user when they are run, but surely this should not prevent the onOpen() trigger from installing the menu which could call the code.
/**
* @OnlyCurrentDoc
*/
function onOpen() {
var ui = SlidesApp.getUi();
ui.createMenu('✔️ Check Answer & 🎨 Update Diagram')
.addItem('check answer', 'questionSubmition')
.addSeparator()
.addItem('Another Page & answer test', 'targetTest2')
.addToUi();
};
function questionSubmition() {
SlidesApp.getUi().alert("Test message", "......", SlidesApp.getUi().ButtonSet.OK);
};
var pres = SlidesApp.getActivePresentation();
var presSelection = SlidesApp.getActivePresentation().getSelection();
var currentSlide = presSelection.getCurrentPage().getObjectId();
var specialSlide = SlidesApp.getActivePresentation().getSlides()[1].getObjectId();
const slide2 = 'g2ae25d2687c_0_0';
//const masterSlideLayouts = pres.getLayouts();
//const masterSlide = masterSlideLayouts[1];
//const studentSlide = pres.getSlideById('p21');
//const shapesArray = masterSlide.getShapes();
//const shapeGroupsArray = masterSlide.getGroups();
About As an example, the onoPen() trigger works with the code below - but only when the last ten lines are commented out.
, if you are using the global variables as shown in your script, I guessed that the reason for your current issue is due to that SlidesApp.getActivePresentation()
is put as a global variable.
When you open Google Slide, the simple trigger of OnOpen is executed. At that time, the global variables are run first. At this time, an error related to permission occurs at SlidesApp.getActivePresentation()
.
If the installable OnOpen trigger can be used, your issue will be resolved by it. But, unfortunately, in the current stage, Google Slide cannot use the installable OnOpen trigger. Google Slide can use only the OnOpen trigger of the simple trigger. In the case of the simple trigger, an error occurs at SlidesApp.getActivePresentation()
.
Although, unfortunately, from your question, I do not know your actual script, in your script, how about moving those variables in a function as follows?
/**
* @OnlyCurrentDoc
*/
function onOpen() {
var ui = SlidesApp.getUi();
ui.createMenu('✔️ Check Answer & 🎨 Update Diagram')
.addItem('check answer', 'questionSubmition')
.addSeparator()
.addItem('Another Page & answer test', 'targetTest2')
.addToUi();
};
function questionSubmition() {
SlidesApp.getUi().alert("Test message", "......", SlidesApp.getUi().ButtonSet.OK);
};
function variables_() {
var pres = SlidesApp.getActivePresentation();
var presSelection = SlidesApp.getActivePresentation().getSelection();
var currentSlide = presSelection.getCurrentPage().getObjectId();
var specialSlide = SlidesApp.getActivePresentation().getSlides()[1].getObjectId();
const slide2 = 'g2ae25d2687c_0_0';
//const masterSlideLayouts = pres.getLayouts();
//const masterSlide = masterSlideLayouts[1];
//const studentSlide = pres.getSlideById('p21');
//const shapesArray = masterSlide.getShapes();
//const shapeGroupsArray = masterSlide.getGroups();
return { pres, presSelection, currentSlide, specialSlide, slide2 };
// or return { pres, presSelection, currentSlide, specialSlide, slide2, masterSlideLayouts, masterSlide, studentSlide, shapesArray, shapeGroupsArray };
}
// This is your function.
// You can use the above variables in the function as follows.
function sample() {
const { pres, presSelection, currentSlide, specialSlide, slide2 } = variables_();
// or const { pres, presSelection, currentSlide, specialSlide, slide2, masterSlideLayouts, masterSlide, studentSlide, shapesArray, shapeGroupsArray } = variables_();
// do something.
// You can use the variables of pres, presSelection, currentSlide, specialSlide, slide2 in this function.
}
variables_()
is not run. So, no error occurs. And, when you want to use the variables of pres, presSelection, currentSlide, specialSlide, slide2
, you can use them as shown in the function sample()
.