我有一个如下所示的代码。
代码.gs
function doGet(e) {
var t = HtmlService.createTemplateFromFile('Index');
var matgrp = e.parameter.matgrp;
t.dataFromServerTemplate = {first: matgrp};
t.data = SpreadsheetApp
.openById('1231455566776fgfgggggggg')
.getActiveSheet()
.getDataRange()
.getValues();
return t.evaluate();
}
请重点关注这部分。
var matgrp = e.parameter.matgrp;
t.dataFromServerTemplate = {first: matgrp};
这里是index.html
<!DOCTYPE html>
<script>
var data = <?!= JSON.stringify(dataFromServerTemplate) ?>; //Stores the data directly in the javascript code
function initialize() {
document.getElementById("myTitle").innerText = data.first;
}
window.onload = initialize;
</script>
<html>
<head>
<base target="_top">
<style>
table,th,td
{
border:1px solid #ddd; font-family:tahoma;font-size: 10px
}
</style>
</head>
<body>
<H2 id="myTitle"></H2>
<table cellspacing="0" cellpadding="3" height ="100%" width ="60%" align = "center">
<th>Item Code</th>
<th>Product Name</th>
<th>Main Description</th>
<th>Other Description</th>
<? for (var i = 2; i < data.length; i++) { ?>
<tr>
<? if(data[i][13] == "Wall Fan") {?>
<td><?= data[i][1] ?></td>
<td><?= data[i][2] ?></td>
<td><?= data[i][3] ?></td>
<td><?= data[i][4] ?></td>
<? }else{ ?>
<?}?>
<? } ?>
</tr>
</table>
</body>
</html>
正如您在上面的代码中看到的那样,函数初始化从 code.gs
获取数据。并将其显示在标题中。 <H2 id="myTitle"></H2>
我的问题是如何传递这行代码中的值?
<table cellspacing="0" cellpadding="3" height ="100%" width ="60%" align = "center">
<th>Item Code</th>
<th>Product Name</th>
<th>Main Description</th>
<th>Other Description</th>
<? for (var i = 2; i < data.length; i++) { ?>
<tr>
<? if(data[i][13] == "This is where I need to put the data") {?>
<td><?= data[i][1] ?></td>
<td><?= data[i][2] ?></td>
<td><?= data[i][3] ?></td>
<td><?= data[i][4] ?></td>
<? }else{ ?>
<?}?>
<? } ?>
</tr>
</table>
这是我的最后一道题,我不知道如何通过。我这样做的原因是使用该值根据条件创建一个表。
TYSM寻求帮助
最佳答案
改变
<? if(data[i][13] == "This is where I need to put the data") {?>
到
<? if(data[i][13] == dataFromServerTemplate.first) {?>
关于javascript - 在 JavaScript 和 JSON 中传递 Google 脚本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44938646/