如何在google scriptlet中传递javascript变量?

时间:2017-07-21 08:10:50

标签: javascript jquery html google-apps-script

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

<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以备将来帮助。

1 个答案:

答案 0 :(得分:0)

在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;

}

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

?festival=myfestival&year=2017

我的HTML文件现在可以访问名为passedParameter的变量,该变量等于&#34; myfestival 2017&#34;我在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>