【问题标题】:How to create a drop down menu with values from spreadsheet?如何使用电子表格中的值创建下拉菜单?
【发布时间】:2019-07-19 04:27:18
【问题描述】:

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

我尝试使用 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);

}

【问题讨论】:

  • 在 for 循环中执行 console.log(cell) 会得到什么值?他们是正确的吗?还有什么时候调用函数测试?
  • 我更新了代码。上一个不工作。我计划在页面加载时调用此函数,因为我首先需要用户输入。
  • 现在上面的结果是什么?
  • 结果是“"。它存储在一个数组中。
  • 好的,只是为了确认一下,您想要的工作表中的值也存储在数组中?

标签: javascript html css google-apps-script


【解决方案1】:

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

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()
&lt;select id="js_sheetValues"&gt;&lt;/select&gt;

【讨论】:

  • 我收到错误“ReferenceError:文档未定义。”当我放置代码时。我该怎么办?
  • 你有html文件吗?
  • 是的。我在同一个项目中有 html 和 gs 文件。
  • 你在哪里添加javascript?在头上还是在身体里?有点google的文档没有定义。这是另一个问题。您是否在浏览器中打开 html 页面并在控制台中收到该错误?可能是因为脚本在文档加载之前正在运行。
  • 脚本在 gs 文件中。我应该把它放在html文件中吗?
猜你喜欢
  • 2018-11-23
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 2016-12-11
  • 2021-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多