Google Slide modalDialog HTML javascript 可以运行服务器端代码吗?环境应用脚本 [英] Can Google Slide modalDialog HTML javascript run server-side code? Environment Apps Script

查看:56
本文介绍了Google Slide modalDialog HTML javascript 可以运行服务器端代码吗?环境应用脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://developers.google.com/apps-script/guides/html/reference/runhttps://developers.google.com/apps-script/guides/html/沟通不要列出幻灯片.我的代码中的警报显示 javascript 正在运行,但没有显示服务器端代码.

<头><基本目标=_top"><风格>

... 为简单起见删除了 CSS

 <身体><div id="父级"><div id="孩子"><?!= nextQuestion ?><p></p><输入类型=按钮"值=好的!"id="subBut"/><输入类型=按钮"值=不太"onclick="google.script.host.close()";/>

<脚本>//- - - - - - - - 听众 - - - - - - - -document.getElementById("subBut").addEventListener("click",功能(事件){alert("开始提交 addEventListener");做得好();//event.preventDefault();//停止表单提交});//- - - - - - - - 职能 - - - - - - - -函数 goodJob() {//alert("In goodJob");google.script.host.close();//alert('调用flipDice');google.script.run.withSuccessHandler( 成功 ).withFailureHandler( 失败 ).flipDice();//警报(结束好工作");}函数成功(){alert('服务器端翻转骰子成功');}功能失败(错误){警报('处理程序失败 - 错误:' + 错误+ '从服务器端接收到flipDice');}</html>

注释掉的警报开始和结束 goodJob 和调用 flipDice 阻止了对话框甚至在被注释掉显示的对话框时显示.

我尝试在 try catch 中包装对服务器的调用.这允许对话框显示,但好的!按钮无法关闭对话框,因此导致调用服务器.当然,涉及不太"的 javascript 可以关闭对话框.

我调用服务器端关闭有什么问题吗?成功和失败处理程序中的警报均未显示.

Console.log 和 flipDice 中的警报未显示.此功能需要一点时间.我不认为这是时间问题,因为第一行显示开始翻转骰子"没有显示.

幻灯片可以调用服务器功能吗?

从菜单调用时,服务器端代码运行良好.它包括 console.log 和警报,当它从按钮触发的 html 中的 javascript 调用时不显示.

关闭 V8 的 onOpen 功能.当我在运行菜单中关闭 V8 时,系统注释掉了第一行.得到错误失踪;声明前.(第 18 行,文件onOpen")"

//@NotOnlyCurrentDoc函数 onOpen() {console.log('In onOpen' );//const diceObj = {//"1": "dice1",//"2": "dice2",//"3": "dice3",//"4": "dice4",//"5": "dice5",//"6": "dice6",//"7": "dice7",//loss_turn//"8": "dice8";//空白封面//};//PropertiesService.getScriptProperties().setProperty(//'idDice', diceObj);让压力;让幻灯片集 = [];尝试  {pres = SlidesApp.getActivePresentation();slideSet = pres.getSlides();}赶上(e){console.log('caught in onOpen e: ', e);}尝试  {SlidesApp.getUi().createMenu('问?').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('检查 1', 'checkDice1').addItem('检查 2', 'checkDice2').addItem('检查 3', 'checkDice3').addItem('检查 4', 'checkDice4').addItem('检查 5', 'checkDice5').addItem('检查 6', 'checkDice6').addItem('Check 7', 'checkDice7').addItem('Check 8', 'checkDice8')).addToUi();}赶上(e){console.log('caught in createMenu e: ', e);}console.log('创建菜单后');PropertiesService.getScriptProperties().setProperty(/* 带有问题的电子表格 */'dataSsId', '1fmZCittj4ksstmhh8_t0O0csj8IDdwi9ohDDL5ZE7VA');const dataSsId = PropertiesService.getScriptProperties().getProperty('dataSsId');//console.log('dataSsId: ', dataSsId);让 ss;尝试  {ss = SpreadsheetApp.openById(dataSsId);如果(!ss){console.log('找不到电子表格!' + dataSsId );SlidesApp.getUi().alert('电子表格未找到!');返回;}  别的    {console.log('找到电子表格!' + dataSsId );}}赶上(e){console.log(' in catch 电子表格 openBYId: ' + dataSsId );SlidesApp.getUi().alert(e);返回;}准备问题(ss);//测试内容//const ckSsId = PropertiesService.getScriptProperties().getProperty('dataSsId');//console.log('dataSsId after prepareQuestions: ', ckSsId);//此处设置任意一次游戏板}

解决方案

在我的一个警报中发现一个缺失的引用,现在服务器端脚本成功运行.谢谢大家.很抱歉成为这样的新人

它仍然得到身份验证错误:

例外:您无权调用 SpreadsheetApp.openById.所需权限:https://www.googleapis.com/auth/spreadsheets

从驱动器打开或刷新时,即使该项目在清单中.尽管有这个错误,它仍然有效.

<代码>{时区":美国/墨西哥城",依赖关系":{已启用高级服务":[{userSymbol":幻灯片",serviceId":幻灯片",版本":v1"}, {用户符号":驱动器",serviceId":驱动器",版本":v2"}]},异常日志":堆栈驱动程序",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"],运行时版本":V8"}

调用服务器函数flipDice的最终工作HTML

<头><基本目标=_top"><风格>为简单起见删除了 CSS</风格><身体><div id="父级"><div id="孩子"><?!= nextQuestion ?><p></p><输入类型=按钮"值=好的!"id="subBut"/><输入类型=按钮"值=不太"onclick="google.script.host.close()";/>

<脚本>//- - - - - - - - 听众 - - - - - - - -document.getElementById("subBut").addEventListener("click",功能(事件){//alert("开始提交 addEventListener");做得好();//event.preventDefault();//停止表单提交});//- - - - - - - - 职能 - - - - - - - -函数 goodJob() {//alert("In goodJob");//google.script.host.close();//alert('调用flipDice');尝试 {google.script.run.withSuccessHandler( 成功 ).withFailureHandler( 失败 ).flipDice();}赶上(e){警报(google.script.runflipDice 发现错误:+ e);}//警报(结束好工作");}函数成功(){google.script.host.close();alert('服务器端翻转骰子成功');}功能失败(错误){警报('处理程序失败 - 错误:' + 错误+ '从服务器端接收到flipDice');}</html>

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

}

解决方案

Found a missing quote in one of my alerts and now the server side script runs successfully. Thank you everyone. Sorry to be such aNewB

It still gets the auth error:

Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets

When opened from drive or refreshed even though the item is in the manifest. It works despite this error.

{
  "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"
}

Final working HTML with call to server function flipDice

<!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>

这篇关于Google Slide modalDialog HTML javascript 可以运行服务器端代码吗?环境应用脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆