首页 > 解决方案 > 如何使用电子表格中的值创建下拉菜单?

问题描述

我正在尝试创建一个下拉菜单,其中的值位于电子表格的特定列中。

我尝试用 foo 制作单元格,但我不知道如何将它们调用到我的 html 文件中。这有效率吗?或者你能告诉我另一种方式来调用它们到我的 html 文件。

尝试了此代码,但不知道如何将其返回到 html。

function email_dropdown(divname) 
{
    var open_sheet = SpreadsheetApp.openByUrl(')getSheetByName');
    SpreadsheetApp.setActiveSpreadsheet(open_sheet);
    var active_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('*********');
    active_sheet.activate();


    var dropdown = "<select id = 'email_dropdown'> Email";

    var row_val = active_sheet.getRange("**").getValues();
    var row_length = row_val.length;
    var row_data = active_sheet.getRange("**");

    for (var row = 2; row <= row_length; row++)
    {
      dropdown = dropdown + row_data.getCell(**).getValue();
    }

    dropdown = dropdown + "</select>"
    Logger.log(dropdown);

}

标签: javascripthtmlcssgoogle-apps-script

解决方案


酷,你已经解决了大部分问题:) 现在你需要做类似下面的事情,因为你有数组中的值

function yourTestfunction() {
	var exampleValues = ['one', 'two', 'three'];
	// after your values have been stored in the array
	var sheetValuesEl = document.querySelector("#js_sheetValues");

	// populate select with values
	for(var i = 0; i < exampleValues.length; i++) {
		// Create the option
		var optionValue = document.createElement("option");
		// Set the option text
		optionValue.textContent = exampleValues[i];
		// Add the option to the select drop down
		sheetValuesEl.appendChild(optionValue);
	}
}

yourTestfunction()
<select id="js_sheetValues"></select>


推荐阅读