Google Slide modalDialog HTML javascript 运行 服务器端代码可以吗?环境应用程序脚本
Can Google Slide modalDialog HTML javascript run server-side code? Environment Apps Script
https://developers.google.com/apps-script/guides/html/reference/run
https://developers.google.com/apps-script/guides/html/communication
不要列出幻灯片。我的代码中的警报显示 javascript 运行ning 而不是服务器端代码。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
...为简单起见删除了CSS
</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>
注释掉的警报开始和结束 goodJob 和调用 flipDice 甚至在它们被注释掉显示的对话框时阻止了对话框的显示。
我尝试在 try catch 中包装对服务器的调用。这允许对话框显示,但 Okay!按钮无法关闭对话框,因此导致调用服务器。当然javascript涉及'not quite'可以关闭对话框。
我对服务器端关闭的调用有问题吗?成功中的警报和失败处理程序均未显示。
Console.log 并且 flipDice 中的警报未显示。这个功能需要一点时间。我不认为这是时间问题,因为第一行显示 'begin flipDice' 没有显示。
幻灯片可以调用服务器函数吗?
从菜单调用服务器端代码 运行 没问题。它包括 console.log 和在按钮触发的 html 中从 javascript 调用时不显示的警报。
关闭 V8 的 onOpen 函数。当我在 运行 菜单中关闭 V8 时,第一行被系统注释掉了。出现错误“Missing ; before statement。(第 18 行,文件“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
}
在我的一个警报中发现缺少引号,现在服务器端脚本成功运行。谢谢大家。很抱歉成为这样的新人
它仍然得到授权错误:
Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets
从驱动器打开或刷新时,即使该项目在清单中。尽管有这个错误,它仍然有效。
{
"timeZone": "America/Mexico_City",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Slides",
"serviceId": "slides",
"version": "v1"
}, {
"userSymbol": "Drive",
"serviceId": "drive",
"version": "v2"
}]
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/presentations",
"https://www.googleapis.com/auth/script.container.ui"],
"runtimeVersion": "V8"
}
调用服务器函数 flipDice
最终工作 HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
CSS removed 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' );
try {
google.script.run
.withSuccessHandler( succeed )
.withFailureHandler( fail )
.flipDice();
} catch (e) {
alert(" google.script.run flipDice caught error: " + e);
}
// alert("end goodJob ");
}
function succeed () {
google.script.host.close();
alert('Success from serverside flipDice');
}
function fail (err) {
alert('Handler fail - err: ' + err
+ ' received from serverside flipDice');
}
</script>
</body>
</html>
https://developers.google.com/apps-script/guides/html/reference/run https://developers.google.com/apps-script/guides/html/communication 不要列出幻灯片。我的代码中的警报显示 javascript 运行ning 而不是服务器端代码。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
...为简单起见删除了CSS
</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>
注释掉的警报开始和结束 goodJob 和调用 flipDice 甚至在它们被注释掉显示的对话框时阻止了对话框的显示。
我尝试在 try catch 中包装对服务器的调用。这允许对话框显示,但 Okay!按钮无法关闭对话框,因此导致调用服务器。当然javascript涉及'not quite'可以关闭对话框。
我对服务器端关闭的调用有问题吗?成功中的警报和失败处理程序均未显示。
Console.log 并且 flipDice 中的警报未显示。这个功能需要一点时间。我不认为这是时间问题,因为第一行显示 'begin flipDice' 没有显示。
幻灯片可以调用服务器函数吗?
从菜单调用服务器端代码 运行 没问题。它包括 console.log 和在按钮触发的 html 中从 javascript 调用时不显示的警报。
关闭 V8 的 onOpen 函数。当我在 运行 菜单中关闭 V8 时,第一行被系统注释掉了。出现错误“Missing ; before statement。(第 18 行,文件“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
}
在我的一个警报中发现缺少引号,现在服务器端脚本成功运行。谢谢大家。很抱歉成为这样的新人
它仍然得到授权错误:
Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets
从驱动器打开或刷新时,即使该项目在清单中。尽管有这个错误,它仍然有效。
{
"timeZone": "America/Mexico_City",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Slides",
"serviceId": "slides",
"version": "v1"
}, {
"userSymbol": "Drive",
"serviceId": "drive",
"version": "v2"
}]
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/presentations",
"https://www.googleapis.com/auth/script.container.ui"],
"runtimeVersion": "V8"
}
调用服务器函数 flipDice
最终工作 HTML<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
CSS removed 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' );
try {
google.script.run
.withSuccessHandler( succeed )
.withFailureHandler( fail )
.flipDice();
} catch (e) {
alert(" google.script.run flipDice caught error: " + e);
}
// alert("end goodJob ");
}
function succeed () {
google.script.host.close();
alert('Success from serverside flipDice');
}
function fail (err) {
alert('Handler fail - err: ' + err
+ ' received from serverside flipDice');
}
</script>
</body>
</html>