2017-07-21 23 views
2

我有一個看起來像這樣的代碼。如何在google scriptlet中傳遞javascript變量?

<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和谷歌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爲未來的幫助。

+0

最好在code.gs中聲明數字,或者在code.gs中寫上述函數,並用google.script.run.withSuccessHandler(fnName).loadmainTBL() – Ritz

+0

來調用它。我知道你會這麼說。我試了幾乎半天似乎不適合我。你能向我證明嗎?如果你想要的話,請使用我的代碼 –

+0

。我將粘貼HTML代替表格。 –

回答

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; 

} 

以上2個的代碼段是我的全部代碼.gs文件。當我運行在瀏覽器中的Web應用程序我補充這Web應用程序的URL的末尾:

?festival=myfestival&year=2017 

我的HTML文件,現在已經進入到一個名爲passedParameter變量,它等於「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>