https://developers.google.com/apps-script/guides/html/reference/run https://developers.google.com/apps-script/guides/html/communication Do not list slides. Alerts in my code show the javascript running but not the server side code.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
... removed CSS for simplicity
</style>
</head>
<body>
<div id="parent">
<div id="child">
<?!= nextQuestion ?>
<p></p>
<input type="button" value="Okay!" id="subBut" />
<input type="button" value="Not quite" onclick="google.script.host.close()" />
</div>
</div>
<script>
// - - - - - - - - LISTENERS - - - - - - - -
document.getElementById("subBut").addEventListener("click",
function(event) {
alert("Begin submit addEventListener");
goodJob();
// event.preventDefault(); //stop form from submitting
} );
// - - - - - - - - FUNCTIONS - - - - - - - -
function goodJob() {
// alert("In goodJob ");
google.script.host.close();
// alert('Call flipDice' );
google.script.run
.withSuccessHandler( succeed )
.withFailureHandler( fail )
.flipDice();
// alert("end goodJob ");
}
function succeed () {
alert('Success from serverside flipDice');
}
function fail (err) {
alert('Handler fail - err: ' + err
+ ' received from serverside flipDice');
}
</script>
</body>
</html>
The commented out alerts start and end goodJob and call flipDice prevented the dialog from even being displayed when they were commented out the dialog displayed.
I tried wrapping the call to the server in a try catch. This allows the dialog to display but the Okay! button could not close the dialog and therefore cause the call to the server. Of course the javascript involved 'not quite' could close the dialog.
Is something wrong with my call to server-side close. Neither the alert in the success nor the failure handlers displayed.
Console.log and alerts in flipDice did not display. This function takes a little time. I don't think it is a timing issue as the first line displays 'begin flipDice' did not display.
Can a slide call a server function?
The server side code runs fine when invoked from the menu. It includes console.log and alerts which are not displayed when it is called from the javascript in the html triggered by the button.
onOpen function with V8 turned off. The first line was commented out by the system when I turned V8 off in the run menu. Getting error "Missing ; before statement. (line 18, file "onOpen")"
//@NotOnlyCurrentDoc
function onOpen() {
console.log('In onOpen' );
// const diceObj = {
// "1": "dice1",
// "2": "dice2",
// "3": "dice3",
// "4": "dice4",
// "5": "dice5",
// "6": "dice6",
// "7": "dice7", // lose_turn
// "8": "dice8" // blank cover
// };
// PropertiesService.getScriptProperties().setProperty(
// 'idDice', diceObj);
let pres;
let slideSet = [];
try {
pres = SlidesApp.getActivePresentation();
slideSet = pres.getSlides();
} catch (e) {
console.log('caught in onOpen e: ', e);
}
try {
SlidesApp.getUi()
.createMenu( 'Ask ?')
.addItem('Roll', 'flipDice')
.addItem('BE1','BE1')
.addItem('BE2','BE2')
.addItem('BE3','BE3')
.addItem('BE4','BE4')
.addItem('Restack','rePositionDice')
.addSeparator()
.addSubMenu(SlidesApp.getUi().createMenu('Check Dice')
.addItem('Check 1', 'checkDice1')
.addItem('Check 2', 'checkDice2')
.addItem('Check 3', 'checkDice3')
.addItem('Check 4', 'checkDice4')
.addItem('Check 5', 'checkDice5')
.addItem('Check 6', 'checkDice6')
.addItem('Check 7', 'checkDice7')
.addItem('Check 8', 'checkDice8'))
.addToUi();
} catch (e) {
console.log('caught in createMenu e: ', e);
}
console.log(' after create menu');
PropertiesService.getScriptProperties().setProperty( /* spreadsheet with the questions */
'dataSsId', '1fmZCittj4ksstmhh8_t0O0csj8IDdwi9ohDDL5ZE7VA');
const dataSsId = PropertiesService.getScriptProperties().getProperty('dataSsId');
//console.log('dataSsId: ', dataSsId);
let ss;
try {
ss = SpreadsheetApp.openById(dataSsId);
if (!ss) {
console.log('Spreadsheet not found! ' + dataSsId );
SlidesApp.getUi().alert('Spreadsheet not found!');
return;
} else {
console.log('Spreadsheet found! ' + dataSsId );
}
} catch(e) {
console.log(' in catch spreadsheet openBYId: ' + dataSsId );
SlidesApp.getUi().alert(e);
return;
}
prepareQuestions(ss);
// testing stuff
// const ckSsId = PropertiesService.getScriptProperties().getProperty('dataSsId');
// console.log('dataSsId after prepareQuestions: ', ckSsId);
// any one time gameboard setup here
}
<buttonwithtype="submit"is rather unusual I'd make it<input type="button"since you don't actually have a<form>- Coopergoogle.script.host.close()in thewithSuccessHandler()- Cooper<input type="button" value="Okay!" />has not ID ofsubBut. By this, when the button ofOkay!is clicked, no action occurs. How about this? By the way, can I ask you about the relationship between your this question and the question in your previous comment? - Tanaike