【问题标题】:Get Variable Value from inside code.gs Google App Script to Javascript从 code.gs Google App Script 内部获取变量值到 Javascript
【发布时间】:2021-12-06 09:33:27
【问题描述】:

我有一个显示按钮的简单 web 应用程序,当用户单击时,它将打开一个显示网站的新窗口。我想要做的是当用户单击按钮时,它将从我的谷歌表中获取值并根据该值打开一个新窗口。然后,webapp 将使用新值更新 google sheet 中的值。

例如: 如果我的 google sheet 中的值为 "Google",它将打开一个窗口到 "www.google.com",然后将 google sheet 值更新为 “其他网站”

每当用户单击按钮时,我已经成功地创建了用于更新 google 工作表上的值的功能,但我无法从 code.gs/google 工作表中获取值到我的 Javascript。 请帮忙。

这是我的代码。gs:

var url = "url"; //mygooglesheet url
var web = "";

function doGet(e) {
  let tmp = HtmlService.createTemplateFromFile("index");
  return tmp.evaluate();

}

function setWebsite () {

  var ss = SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("website");
  web = ws.getRange(1,1,1,1).getValue();
  
  if (web === "Google") {
    ws.getRange(1,1,1,1).setValue("Youtube");
  } 
    else if (web === "Youtube") {
    ws.getRange(1,1,1,1).setValue("Facebook");
  }
    else {
    ws.getRange(1,1,1,1).setValue("Google");
  } 
}

function getWebsite() {  
  
  var ss = SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("website");
  var web = ws.getRange(1,1,1,1).getValue();
    return web;
    }

我的 index.html:

<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>Please Click Below</h1>
    <!-- <h2><?=web?>:</h2> -->
    <button id = "btn" type="submit" >Open Window</button>
              
  <script>
  document.getElementById("btn").addEventListener("click", doStuff);
  var web = "";
  
  function doStuff() {

  google.script.run.getWebsite(); //dont know for sure if this is needed or not
  google.script.run.setWebsite();
  web = <?=web?>; //dont know for sure if this is needed or not
  if (web === "Google") {
      window.open("https://www.google.com/");
    } 
      else if (web === "Youtube") {
      window.open("https://www.youtube.com/");
    }
     else {
      window.open("https://www.facebook.com/");
    } 
  }
</script>

  </body>
</html>

现在,我的 webapp 只打开一个新窗口到 "facebook.com" 并更新到代码中的下一个值。我尝试了 "google.script.run.withSuccessHandler(onSuccess).getWebsite()" 但未能成功从 code.gs 中获取变量值,请帮忙。

谢谢。

【问题讨论】:

    标签: javascript html google-apps-script


    【解决方案1】:

    问题:

    您希望在单击按钮时使用getWebsite() 返回的 URL 打开一个新选项卡。

    解决办法:

    为了处理由google.script.run 调用的服务器端函数返回的数据,请使用success handler。此处理程序调用的函数将作为参数传递服务器端函数(getWebsite())返回的值。

    代码示例:

    <html>
      <head>
        <base target="_top">
      </head>
      <body>
        <h1>Please Click Below</h1>
        <button id = "btn" type="submit" >Open Window</button>
        <script>
          document.getElementById("btn").addEventListener("click", doStuff); 
       
          function doStuff() {
            google.script.run.withSuccessHandler(openWebsite).getWebsite();
          }
    
          function openWebsite(web) {
            if (web === "Google") {
              window.open("https://www.google.com/");
            } else if (web === "Youtube") {
              window.open("https://www.youtube.com/");
            } else {
              window.open("https://www.facebook.com/");
            }
          }
        </script>
      </body>
    </html>
    

    注意:

    如果getWebsite() 返回的数据应该在页面首次加载和单击按钮之间保持静态,您也可以使用Mike Steelson 提到的方法,使用template scriplets

    【讨论】:

    • 感谢您的解决方案!这个更好,我在找什么。我意识到当我第一次加载页面并单击按钮时,它使用@Mike Steelson 回答正确运行。但是当我第二次点击时,它没有从谷歌表中获得新的更新值。使用您的解决方案,它将从谷歌表中获取新的更新值。非常感谢,我学到了很多。 =)
    【解决方案2】:

    在html中,在脚本部分,写

    <? var web = getWebsite(); ?>
    

    并删除google.script.run.getWebsite();

    【讨论】:

    • 非常感谢您提供解决方案。有效!我花了两天时间试图找到解决方案,但没有奏效。对不起,我还是个学习者。
    猜你喜欢
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多