javascript - 在 JavaScript 和 JSON 中传递 Google 脚本值

标签 javascript jquery json google-apps-script google-sheets

我有一个如下所示的代码。

代码.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/

相关文章:

javascript - Void 类型不可分配给 Boolean 类型(React/Typescript)

尝试向下滚动页面时 JavascriptExecutor 错误

javascript - 在 JavaScript if 条件中比较 jQuery(this)

javascript - Cheerio 获取类属性

javascript - 语法错误 : Unexpected end of input

javascript - 如何获取没有对象编号的 json 对象?

javascript - 如何在js代码中获取json值?

javascript - ajax中的Json解析

javascript - 简单的ajax请求

ios - 使用 Alamofire 使用来自 JSON 的 URL 加载图像