【问题标题】:How to pass javascript variable in google scriptlet?如何在 google scriptlet 中传递 javascript 变量?
【发布时间】:2017-07-21 08:10:50
【问题描述】:

我有一个看起来像这样的代码。

<script>
function loadmainTBL(){
 <? var data = SpreadsheetApp
   .getActiveSpreadsheet().getSheetByName("Customer Logs Information Database")
   .getDataRange()
   .getValues(); ?>
 var number = 6

 <?for (var i = 12; i < data.length; i++) { ?>
 <? if (data[i][0] == "I want to put it here") { ?>
 var tableHeaderRowCount = 1;
 var table = document.getElementById('TableContainer');
 var rowCount = table.rows.length;
 for (var i = tableHeaderRowCount; i < rowCount; i++) {
    table.deleteRow(tableHeaderRowCount);
 }
 <?}?>
 <?}?>
 }
</script>

您可以看到此代码在 html 文件中,它由 javascript 和 google scriptlet 组成,我的问题是这个。我怎样才能通过这个?

var number = 6

在这个?

<? if (data[i][0] == "I want to put it here") { ?>

这里是html代码。

<? var data = SpreadsheetApp
   .getActiveSpreadsheet().getSheetByName("Customer Logs Information Database")
   .getDataRange()
   .getValues(); ?>

<table  id = "TableContainer" cellspacing="2" cellpadding="3" width ="100%" align = "center" class="hoverTable">
   <th  bgcolor = "darkgreen"><font color="white">#</font></th>
   <th  bgcolor = "darkgreen"><font color="white">Area</font></th>
   <th  bgcolor = "darkgreen"><font color="white">Customer Name</font></th>
   <th  bgcolor = "darkgreen"><font color="white">Person In Charge</font></th>
   <th  bgcolor = "darkgreen"><font color="white">Remarks</font></th>
   <th  bgcolor = "darkgreen"><font color="white">Status</font></th>
   <th  bgcolor = "darkgreen"><font color="white">Doc. Date</font></th>
   <th  bgcolor = "darkgreen"></th>
   <? for (var i = 12; i < data.length; i++) { ?>
   <tr>
   <td  class="dataid"><?= data[i][0] ?></td>
   <td  class="area"><?= data[i][1] ?></td>
   <td  class="cusname"><?= data[i][2] ?></td>
   <td  class="cic" width = "200px"><?= data[i][3] ?></td>
   <td  class="remarks" ><?= data[i][4] ?></td>
   <td  class="status" width = "70px"><?= data[i][5] ?></td>
   <td  class="docdate"><?= data[i][6] ?></td>
   <td  ><img class="click-to-select" src="https://docs.google.com/uc?id=0By6kUPbaVMWCbUI0LTJTR2g2N3M" alt="Submit" width="13px" height="13px" title = "Edit Selected Data" data-toggle="modal" data-target="#myModal"/>
   </td>
   <? } ?>
   </tr>
</table>

tysm 以获取未来帮助。

【问题讨论】:

  • 最好在code.gs中声明number或者在code.gs中写上上面的函数,用google.script.run.withSuccessHandler(fnName).loadmainTBL()调用
  • 我知道你会这么说。我尝试了将近半天似乎对我不起作用。你能示范给我看吗?请使用我的代码
  • 如果你也想确定。我会将 html 粘贴到它将替换表格的位置。

标签: javascript jquery html google-apps-script


【解决方案1】:

在 code.gs 的任何其他内容之前,放置以下内容:

//Define this here globally so it is accessed in the HTML Template
var passedParameter= ' ';

您可以将其定义为等于 6,或者在创建页面之前根据一些代码在函数内部定义它。就我而言,我将其部署为 Web 应用程序并通过 URL 传递两个变量,然后我将其合并并保存,以便我的客户端可以访问:

/**-----------------------------------------------------------------------------------
|
|    Begin section for app interface
|
------------------------------------------------------------------------------------*/
// Script-as-app template.
function doGet(passed) {

if(passed.parameter.festival && passed.parameter.year){

Logger.log(passed.parameter);
passedParameter = passed.parameter.festival + ' ' + passed.parameter.year;
}


  var result=HtmlService.createTemplateFromFile('GridView').evaluate()
        .setTitle('Boxwood Registrations')
        .setWidth(1285)
        .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);

  return result;

}

以上2段代码是我的整个code.gs文件。当我在浏览器中运行网络应用程序时,我将其添加到网络应用程序 url 的末尾:

?festival=myfestival&year=2017

我的 HTML 文件现在可以访问一个名为 passParameter 的变量,它等于“myfestival 2017”,我在我的 HTML 中的一个函数中访问它:

<script>
function showMenuYear(menuItems) {
  var list = $('#optionListYear');
  var desiredValue = '<?!= passedParameter ?>';

//More code here to use the variable desiredValue
}
</script>

如果我需要在用户输入后从服务器端检索更多信息,例如读取电子表格中的值,我会在 html 文件的函数中使用它:

<script>
function setFormList() {

  var replacement = document.getElementById("OverallGrid");
  replacement.innerHTML = "Retrieving Data...";

  if ($('#optionListYear').val() === "-1") {
    replacement.innerHTML = "Select a Festival/Year above.";
    return;
  }


 //Run the function getValidRegistrations() which is in a .gs file (server side), passing the value of the element optionListYear.
  google.script.run.withSuccessHandler(showRegistrationsTable).withFailureHandler(loadFailed).getValidRegistrations($('#optionListYear').val());

}

//If the server side ran succesfully, this is run
function showRegistrationsTable(returnedItem){
  //My code to work with the returned item goes here
}

//If the server side failed for some reason an error will be returned
function loadFailed(returnedError){
  //My code to display a error message with the returned item goes here
}
</script>

【讨论】:

    猜你喜欢
    • 2019-01-12
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2023-03-27
    • 2015-04-12
    • 2014-11-11
    相关资源
    最近更新 更多