首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将变量从.gs函数推入html文件?

如何将变量从.gs函数推入html文件?
EN

Stack Overflow用户
提问于 2020-10-22 16:36:19
回答 1查看 171关注 0票数 0

下面的代码自动获取电子表格中某个列的最后一个链接。我需要创建一个网页应用程序的按钮,每次点击它运行脚本,并自动打开一个新窗口,在列的最后一个链接。我需要它的形式的网页应用程序,因为我需要嵌入的网址到一个按钮的外部网站。

提取列中最后一个链接的.gs代码如下所示:

代码语言:javascript
复制
function obtainurl(){  
  var summarySheet = 
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Summary'); 
  var lastRow = "AA" + summarySheet.getLastRow(); 
  var new_url = summarySheet.getRange(lastRow).getValues();
  var new_url1=   [].concat.apply([], new_url)  
  return new_url1;
}

上面的代码可以工作,并返回我与logger.log检查过的正确链接。然后,我有另一个.gs函数的组合:

代码语言:javascript
复制
function doGet(){
  return HtmlService.createHtmlOutputFromFile('openUrl').setHeight(50);
}    

而对应的html代码:

代码语言:javascript
复制
<html>
  <head>
   <base target="_blank">
    <script>

      //Here is where i have the problem I don't know how to pass the url into the var above and get the correct link opened everytime.
      
      var winRef = window.open(new_url);
      winRef ? google.script.host.close() : window.alert('Allow popup to redirect you to '+url1) ;
      window.onload=function(){document.getElementById('url').href = url1;}
    </script>
  </head>
  <body>
    If the download has not started please disable your ad-blocker! Or else contact us at -----
  </body>
</html>

总之,我不知道如何将传递给html文件。我已经检查过了,如果我将完整的url硬编码到html文件中,它就能工作。但是,这需要是动态的,总是打开电子表格列中的最后一个url。

提前谢谢你,我知道我是个新手。

EN

回答 1

Stack Overflow用户

发布于 2020-10-22 22:01:41

您需要使用.gs从客户机调用google.script.run函数,并在服务器返回值后指定要执行的函数。

代码语言:javascript
复制
// Calls server function obtainurl()
google.script.run.withSuccessHandler(showUrl).obtainurl();

// Function executed after server returns value
function showUrl(url) {
  alert(url);
}

这里有更多详细信息:https://developers.google.com/apps-script/guides/html/reference/run

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64486648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档