简体   繁体   English

如何将变量从 Code.gs 传递到包含的脚本片段

[英]How to pass variable from Code.gs to included script snippet

So I understand that to put a variable in my HTML template I need to do所以我知道要在我的 HTML 模板中放置一个变量,我需要做

var html = HtmlService.createTemplateFromFile('Index')    
html.data = data
html = html.evaluate();

and then call it in the HTML with然后在 HTML 中调用它

<?= data ?>

But then if I include a script tag snippet with (in the Code.gs file)但是,如果我包含一个脚本标记片段(在 Code.gs 文件中)

function include(filename) {
   return HtmlService.createHtmlOutputFromFile(filename)
   .getContent();
}

and then include my script snippet in the HTML with然后在 HTML 中包含我的脚本片段

<?!= include('DataHandler') ?>

How do I call that data variable in the DataHandler script?如何在 DataHandler 脚本中调用该data变量?

Change the include function to evaluate data:更改include function 以评估数据:

function include(filename, ...otherData) {
  var html = HtmlService.createTemplateFromFile('Index')    
  otherData.forEach(obj => html[obj.key] = obj.value)
  return html.evaluate();
}

Then use include like:然后使用include

<?!= include('DataHandler', {key:data, value: [1,2,3]}) ?>

I don't really use templates much but this worked for me:我并没有真正使用模板,但这对我有用:

HTML: HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div id="lnk"><?= data?></div>
    <script>
      var gdata=<?= data ?>
      window.onload=function(){
        google.script.run.writeData(gdata);
      }
      console.log("My Code");
    </script>
  </body>
</html>

GS: GS:

function launchmydialog() {
  let ui=HtmlService.createTemplateFromFile('ah1');
  ui.data="Hello Work";
  SpreadsheetApp.getUi().showModelessDialog(ui.evaluate(),'Title');
}

function writeData(data) {
  SpreadsheetApp.getUi().alert(data);
}

暂无
暂无

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

相关问题 如何将元素值变量从 Javascript html 文件传递到 google apps 脚本中的 code.gs 文件? - How to pass an element-value variable from Javascript html file to code.gs file in google apps script? 将数组从Code.gs传递到Javascript Google App脚本 - Pass an Array from Code.gs to Javascript Google App Script 在 Google App Script 中将变量从 Code.gs 传递到 html - Passing variable from Code.gs to html in Google App Script 如何将 code.gs 中的变量添加到 Audio src? - How to Add variable from code.gs to Audio src? Google Apps脚本,将两个(变量)2个“变量”或“数组”从code.gs传递给index.html / Javascript - Google apps script, pass (two) 2 “variables” or “array” from code.gs to index.html / Javascript 从 code.gs Google App Script 内部获取变量值到 Javascript - Get Variable Value from inside code.gs Google App Script to Javascript Google 脚本从 code.gs 返回到 index.html - Google script return from code.gs to index.html 如何将另一个页面的值更新到 Code.gs 数组列表中以传递到电子表格 - How to update value from another page into Code.gs array list to be pass into spread sheet 谷歌应用程序如何将数据从 html 文件发送到 code.gs 变量 - Google apps how to send data from html file to code.gs variable 使用Google Apps脚本将对话框中输入字段的值返回到code.gs中的var - return value from input field in dialog box to var in code.gs using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM