简体   繁体   English

在JavaScript和JSON中传递Google脚本值

[英]Passing Google Script Value in JavaScript and JSON

I have a code that looks like this. 我有一个看起来像这样的代码。

code.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();

}

Please focus on this part. 请专注于这一部分。

var matgrp = e.parameter.matgrp; t.dataFromServerTemplate = {first: matgrp};

and here is index.html 这是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>

as what you see on the code above the function initialize gets the data from code.gs and display it in header. 正如您在函数上方的代码中看到的那样,initialize从code.gs获取数据并将其显示在标头中。 the <H2 id="myTitle"></H2> my question is how can i pass the value in this line of code? <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>

this is my last problem and I dont know how to pass. 这是我的最后一个问题,我不知道如何通过。 the reason why im doing this is to use that value to create a table based on condition. 我这样做的原因是使用该值基于条件创建表。

TYSM for help TYSM寻求帮助

Change 更改

<? if(data[i][13] == "This is where I need to put the data") {?> 

to

<? if(data[i][13] == dataFromServerTemplate.first) {?>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM