简体   繁体   English

将 html 模板中的 js 变量传递给 google 脚本

[英]Pass js variable in html template to google script

I have a.gs file which create a html page:我有一个 .gs 文件,它创建了一个 html 页面:

function doGet() {
  return HtmlService.createHtmlOutputFromFile('index');
}

doGet();
console.log(myvariable);

and in that index.html file I'll create a variable in tag, which I'd like to pass to my.gs file, for further putting it in google spreadsheet.在那个 index.html 文件中,我将在标签中创建一个变量,我想将其传递给 my.gs 文件,以便进一步将其放入谷歌电子表格中。

<script>
  var myvariable = 10;
</script>

Is there any method that can carry out my need?有什么方法可以满足我的需要吗?

Thank you.谢谢你。

Pass variable to htmltemplate将变量传递给 html 模板

function elfunko() {
  let t = HtmlService.createTemplate('<input type="text" value="<?= globalvariable ?>" />');
  t.globalvariable = "Hello World";
  SpreadsheetApp.getUi().showModelessDialog(t.evaluate(),'title');
}

在此处输入图像描述

Pass variable back to server from html将变量从 html 传回服务器

This one will pass a variable back to server scripts when you click the button on the dialog you can see the result by looking in elfunko1s execution log.当您单击对话框上的按钮时,这会将变量传递回服务器脚本,您可以通过查看 elfunko1s 执行日志来查看结果。

function elfunko() {
  let t = HtmlService.createTemplate('<input type="button" value="<?= globalvariable ?>" onclick="google.script.run.elfunko1({variable:<?= globalvariable ?>})" />');
  t.globalvariable = "Hello World";
  SpreadsheetApp.getUi().showModelessDialog(t.evaluate(),'title');
}

function elfunko1(obj) {
  console.log(JSON.stringify(obj))
}

在此处输入图像描述

Cloud logs
Feb 26, 2022, 8:38:00 PM    Debug   {"variable":"Hello World"}

client to server comm 客户端到服务器通讯

templated html 模板化 html

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

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