javascript - Google Slide modalDialog HTML javascript 可以运行服务器端代码吗?环境应用脚本
问题描述
https://developers.google.com/apps-script/guides/html/reference/run https://developers.google.com/apps-script/guides/html/communication 不要列出幻灯片。我的代码中的警报显示 javascript 正在运行,但不显示服务器端代码。
<!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 中。这允许对话框显示,但好的!按钮无法关闭对话框,因此导致调用服务器。当然,涉及“不完全”的 javascript 可以关闭对话框。
我对服务器端关闭的调用有问题。成功中的警报和失败处理程序均未显示。
FlipDice 中的 Console.log 和警报未显示。这个功能需要一点时间。我认为这不是时间问题,因为第一行显示“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
}
解决方案
在我的一个警报中发现了一个缺少的报价,现在服务器端脚本成功运行。谢谢大家。很抱歉成为这样一个NewB
它仍然得到身份验证错误:
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>
推荐阅读
- javascript - 使用两个键执行自动完成 - 使用 React 的 Material UI
- c# - 使用 Jquery 进行客户端验证 - MVC C# 应用程序
- github - PhpStorm GitHub 不推送某些文件和类
- react-native - ReactNative:解码从 BLE 接收到的特征值不起作用
- javascript - 使用正则表达式进行 formik 表单验证
- javascript - 使用钩子将光标放在自定义 React 功能组件的末尾
- javascript - Expo OTA 更新在首次发布时不起作用
- python - 提高进行许多 api 调用并将所有数据存储到一个数据帧中的代码的速度
- git - 将“子”git repo 合并/移动到“父”git repo
- python - 操作两个数据帧以到达输出