簡體   English   中英

將 html 模板中的 js 變量傳遞給 google 腳本

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

我有一個 .gs 文件,它創建了一個 html 頁面:

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

doGet();
console.log(myvariable);

在那個 index.html 文件中,我將在標簽中創建一個變量,我想將其傳遞給 my.gs 文件,以便進一步將其放入谷歌電子表格中。

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

有什么方法可以滿足我的需要嗎?

謝謝你。

將變量傳遞給 html 模板

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

在此處輸入圖像描述

將變量從 html 傳回服務器

當您單擊對話框上的按鈕時,這會將變量傳遞回服務器腳本,您可以通過查看 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"}

客戶端到服務器通訊

模板化 html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM