简体   繁体   English

如何使用 gs 文件中的变量作为 Google Apps 脚本中 HTML 文件的输入值?

[英]How can I use the variable in the gs file as the input value in the HTML file in Google Apps script?

The variable fieldName for testGetFieldName function is located at Code.gs testGetFieldName function 的变量 fieldName 位于 Code.gs

I want to insert it into the input value of the index.html file.我想将它插入到 index.html 文件的输入值中。

But if I open Sidebar using testSetValue, the value comes out as undefiend.但是,如果我使用 testSetValue 打开 Sidebar,该值就会显示为 undefiend。

How can I get the variable 'Account' to come out with Value?我怎样才能让变量'Account'产生价值?

The code I wrote is as follows.我写的代码如下。

//code.gs
function testGetFieldName(){
  var fieldName = 'Account';
  return fieldName;
}

function testSetValue(){
  var html = HtmlService.createTemplateFromFile('index');
  var output = html.evaluate()
      .setTitle('MySidebar')
      .setWidth(400)
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi().showSidebar(output);
}

index.html index.html

<!DOCTYPE html>
<html>
<head>
  <base target="_top">
</head>

<body>
  <input type="text" id ='textValue' name='textValue' value=''/>
   <script>
     document.getElementById('textValue').setAttribute('value',google.script.run.testGetFieldName());
   </script>
 </body>
</html>

In your script, how about the following modification?在您的脚本中,如何进行以下修改?

Google Apps Script side: Google Apps 脚本方面:

From:从:

var html = HtmlService.createTemplateFromFile('index');
var output = html.evaluate()

To:至:

var html = HtmlService.createTemplateFromFile('index');
html.value = testGetFieldName();
var output = html.evaluate()

HTML side: HTML 侧:

From:从:

document.getElementById('textValue').setAttribute('value',google.script.run.testGetFieldName());

To:至:

document.getElementById('textValue').setAttribute('value', '<?= value ?>');

Note:笔记:

  • If you want to use google.script.run , how about the following modification?如果你想使用google.script.run ,那么下面的修改呢?

    • From

       document.getElementById('textValue').setAttribute('value',google.script.run.testGetFieldName());
    • To

       google.script.run.withSuccessHandler(e => { document.getElementById('textValue').setAttribute('value', e); }).testGetFieldName();

Reference:参考:

In general, it's a best practice to separate your HTML,CSS,JavaScript files.通常,最好将 HTML、CSS、JavaScript 文件分开。

References参考

HtmlService Best Practices HtmlService 最佳实践

暂无
暂无

声明:本站的技术帖子网页,遵循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? 使用 html 对话框中输入字段的值使用 Google Apps 脚本在 code.gs 中进行 var - use value from input field in html dialog box to var in code.gs using Google Apps Script 如何将变量从HTML脚本文件传递到Google Apps脚本中的javascript函数? - How can I pass a variable from an HTML script file to a javascript function in google apps script? 谷歌应用程序如何将数据从 html 文件发送到 code.gs 变量 - Google apps how to send data from html file to code.gs variable 将 .gs(Google Apps 脚本)中的数据共享到<script> variable in html - Share data from .gs (Google Apps Script) to <script> variable in html 在 Google Apps 脚本中将变量从 .gs 共享到 .html - Share variable from .gs into .html in Google Apps Script Google云端硬盘应用:如何获取HTML文件以读取code.gs中定义的变量? - Google drive app: How can i get my HTML file to read a varitable defined in code.gs? 如何在 HTML Google Apps 脚本中包含文件? - How do I include a file in HTML Google Apps Script? 如何在谷歌应用脚本中的 CSV 文件中添加一列(具有 static 值)? - How can I add a column (with a static value) to a CSV file in google apps script? 无法在Google脚本创建的HTML中引用.gs文件 - unable to reference to .gs file within the google script created HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM