2015-07-21 109 views
0

我想從一個基於這裏發佈的問題Previous questioe posted by @Kron011的問題的電子表格中填充一個列表,我正在掙扎。我添加了這些行我.GS文件:Google html從電子表格創建列表

function getMenuListOne(){ 
return SpreadsheetApp.openbyId('spreadsheet_key').getSheetByName('sheet1') 
.getRange(row, column, numRows, numColumns).getValues(); 
} 

,我添加了這行我的HTML文件:

<select id="menu"> 
    <option></option> 
    <option>Google Chrome</option> 
    <option>Firefox</option> 
    </select> 


<script 
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> 
</script> 
<script> 
// The code in this function runs when the page is loaded. 
$(function() { 
    google.script.run.withSuccessHandler(showThings) 
     .getMenuListFromSheet(); 
    google.script.run.withSuccessHandler(showMenu) 
     .getMenuListFromSheet(); 
}); 

function showThings(things) { 
    var list = $('#things'); 
    list.empty(); 
    for (var i = 0; i < things.length; i++) { 
    list.append('<li>' + things[i] + '</li>'); 
    } 
} 

function showMenu(menuItems) { 
    var list = $('#menu'); 
    list.find('option').remove(); // remove existing contents 

    for (var i = 0; i < menuItems.length; i++) { 
    list.append('<option>' + menuItems[i] + '</option>'); 
    } 
} 

</script> 

一如以往我的痛苦有限的經驗是阻礙我的努力。我可以得到一個新的菜單框出現並顯示我想要的正確結果,但我無法使現有的框顯示相同的列表。現有的箱碼目前:

<input type="text" name="site" list="depotslist" id="site" class="form-control" placeholder="Select depot/site" required> 
        <datalist id="depotslist"> 
        <option value="one"> 
        <option value="two"> 
        </datalist> 

但有人請點我在正確的方向這我現有的菜單框的部分,我需要改變,以獲得兩位溝通?

UPDATE 7月23日

添加以下代碼來獲取另一份清單,以從另一個源操作:

$(function() { 
    google.script.run.withSuccessHandler(showThings2) 
     .getMenuListSources(); 
    google.script.run.withSuccessHandler(showMenu2) 
     .getMenuListSources(); 
}); 

function showThings2(things2) { 
    var list2 = $('#things2'); 
    list.empty(); 
    for (var i = 0; i < things2.length; i++) { 
    list2.append('<li>' + things2[i] + '</li>'); 
    } 
} 

function showMenu2(menuItems2) { 
    var list2 = $('#menu2'); 
    var box2 = $('#sourcelist'); 
    list2.find('option').remove(); // remove existing contents 

    for (var i = 0; i < menuItems2.length; i++) { 
    list2.append('<option>' + menuItems2[i] + '</option>'); 
    box2.append('<option>' + menuItems2[i] + '</option>'); 
    } 
} 

這些線路中的.GS文件:

var Bvals = SpreadsheetApp.openById(ssKey).getSheetByName('SourceCodes').getRange("C3:C").getValues(); 
var Blast = Avals.filter(String).length; 
return SpreadsheetApp.openById(ssKey).getSheetByName('SourceCodes').getRange(3,3,Blast,1).getValues(); 

回答

1

這與使用元素「菜單」所做的相似。與jQuery的你可以選擇包含選項的框中的元素,然後附加新的值。在這種情況下,它的ID是:depotslist。

function showMenu(menuItems) { 
    var list = $('#menu'); 
    var box = $('#depotslist'); 
    list.find('option').remove(); // remove existing contents 

    for (var i = 0; i < menuItems.length; i++) { 
    list.append('<option>' + menuItems[i] + '</option>'); 
    box.append('<option>' + menuItems[i] + '</option>'); 

    } 

作爲一個不同的元素「菜單」在這種情況下,內容將保留。這意味着,在框中,你會看到,無論您添加的,因爲我們不喜歡這一行刪除內容選項「一,二」

list.find('option').remove(); 

我不知道這是否是你想要的到底是什麼要做,讓我知道如果這沒有幫助。

+0

謝謝傑拉爾多,這正是我之後的事情。 – witham

+0

赫拉爾多,如果我想從一張紙上創建另一個列表,我需要做些什麼才能做出獨特的或改變來允許這個?我嘗試添加完全相同的代碼並更改了行'var box = $('#');'但其他列表未顯示我期望的條目。 – witham

+0

這些新列表顯示了什麼?那些是同樣的列表嗎?在選擇器中使用#時,是指示要選擇的元素的ID。所以請確保元素具有唯一的ID。 – Gerardo