[英]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?在您的脚本中,如何进行以下修改?
var html = HtmlService.createTemplateFromFile('index');
var output = html.evaluate()
var html = HtmlService.createTemplateFromFile('index');
html.value = testGetFieldName();
var output = html.evaluate()
document.getElementById('textValue').setAttribute('value',google.script.run.testGetFieldName());
document.getElementById('textValue').setAttribute('value', '<?= value ?>');
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();
In general, it's a best practice to separate your HTML,CSS,JavaScript files.通常,最好将 HTML、CSS、JavaScript 文件分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.