简体   繁体   English

如何在Google scriptlet中传递JavaScript变量?

[英]How to pass javascript variable in google scriptlet?

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

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

as you can see this code is inside html file and it is compose of javascript and google scriptlet my question is this. 如您所见,此代码位于html文件中,并且由javascript和google scriptlet组成,我的问题是这样。 how can I pass this? 我该如何通过?

var number = 6

in this one? 在这个?

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

Here is the html code. 这是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 for future help. tysm以获得将来的帮助。

In code.gs before anything else, place the following: 在code.gs之前,放置以下内容:

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

You can define this to equal 6, or inside a function define it based on some code prior to creating the page. 您可以将其定义为等于6,或者在函数内部根据创建页面之前的一些代码对其进行定义。 In my case, I am deploying this as a web app and passing two variables via the URL, which I then merge and save so my client side has access: 就我而言,我将其部署为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;

}

The above 2 sections of code are my entire code.gs file. 以上两段代码是我的整个code.gs文件。 When I run the web app in my browser I add this to the end of web app's url: 当我在浏览器中运行Web应用程序时,将其添加到Web应用程序URL的末尾:

?festival=myfestival&year=2017

My HTML files now have access to a variable called passedParameter which is equal to "myfestival 2017" which I access in a function in my HTML with: 我的HTML文件现在可以访问一个名为passParameter的变量,该变量等于“ myfestival 2017”,我可以通过以下方式在HTML函数中访问该变量:

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

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

If I need to retrieve more information from the server side after user input, such as read a spreadsheet for a value, I use this in a function in the html file: 如果我需要在用户输入后从服务器端检索更多信息,例如读取电子表格中的值,请在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>

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

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