简体   繁体   English

Google脚本-在html文件中使用JS变量

[英]Google Script - Using a JS variable in html file

I want to include a variable that I normally use in a .gs file in my html file. 我想在html文件中包含一个通常在.gs文件中使用的变量。 For example, the following code would make variable 'value' be the content of whatever is in cell A1: 例如,以下代码将使变量“值”成为单元格A1中内容的内容:

var value = SpreadsheetApp.getActiveSheet().getRange("A1").getValue();

How can I include this variable in my .html file, so that typing 'value' in the html file will return the contents of cell A1? 如何在我的.html文件中包含此变量,以便在html文件中键入'value'将返回单元格A1的内容?

EDIT 编辑

What I'm trying to achieve is build a mail action button that automatically submits a google form, filled with the data in the spreadsheet. 我要实现的目标是建立一个邮件操作按钮,该按钮会自动提交一个Google表单,并用电子表格中的数据填充。 The script I have for the .html file is as follows: 我为.html文件准备的脚本如下:

<div>
<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "EmailMessage",
  "action": {
    "@type": "ConfirmAction",
    "name": "Accept",
    "handler": {
      "@type": "HttpActionHandler",
      "url": "https://docs.google.com/forms/d/139j-0POMG9BVqttXnY8l-6YMI7M2DReREIuJJd_waJE/formResponse?ifq&entry.1766440389=" + value + "&entry.1329637906&submit=Submit"
    }
  },
  "description": ""
}

</script>

</div>

The button works perfectly fine, but I need to prefill the form by adding the variable in the middle of the url (under 'handler' where I typed " + value + "), containing the contents of the cell. 该按钮可以正常工作,但是我需要通过在包含单元格内容的url中间(在“ handler”下,我键入“ + value +”的位置)下添加变量来预填写表单。 Only problem is that I can't figure out how to integrate that variable in the .html file 唯一的问题是我不知道如何在.html文件中集成该变量

You have a text formula: 您有一个文本公式:

"url": "https://docs.google.com/forms/d/139j-0POMG9BVqttXnY8l-6YMI7M2DReREIuJJd_waJE/formResponse?ifq&entry.1766440389=" + value + "&entry.1329637906&submit=Submit"

And you are adding a variable to a URL search string. 并且您正在向URL搜索字符串添加变量。 You want the value to be looked up from the spreadsheet, and put into the URL. 您希望从电子表格中查找值,然后将其放入URL。

So, I guess you have a button that starts the process? 那么,我想您有一个启动过程的按钮? When the button is clicked, it should run google.script.run . 单击该按钮后,它应运行google.script.run Then, when the server side .gs function is done running, it will return a value, and you will need an withSuccessHandler(function) to start the function that will take the returned value, and put it into the URL. 然后,当服务器端.gs函数完成运行时,它将返回一个值,并且您将需要一个withSuccessHandler(function)来启动将接受返回值并将其放入URL的函数。

Start with reading the documentation for google.script.run . 首先阅读google.script.run的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM