Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
工作测试的javascript不';从Google Sheets html模板运行时不起作用_Javascript_Arrays_Google Sheets_Dropdown - Fatal编程技术网

工作测试的javascript不';从Google Sheets html模板运行时不起作用

工作测试的javascript不';从Google Sheets html模板运行时不起作用,javascript,arrays,google-sheets,dropdown,Javascript,Arrays,Google Sheets,Dropdown,一般来说,我对脚本编写非常陌生,所以请耐心听我说 我正在google sheets上测试一些脚本,运行一个HTML模板 我的部分脚本需要调用数组数据。我正在测试运行此脚本: 您将看到脚本按需要运行 但是,当HTML模板从GoogleSheets启动时,即使使用相同的代码,也不会填充数字 在Google Sheets脚本中,以下函数用于运行HTML模板: var htmlDlg = HtmlService.createHtmlOutputFromFile('index') .set

一般来说,我对脚本编写非常陌生,所以请耐心听我说

我正在google sheets上测试一些脚本,运行一个HTML模板

我的部分脚本需要调用数组数据。我正在测试运行此脚本:

您将看到脚本按需要运行

但是,当HTML模板从GoogleSheets启动时,即使使用相同的代码,也不会填充数字

在Google Sheets脚本中,以下函数用于运行HTML模板:

  var htmlDlg = HtmlService.createHtmlOutputFromFile('index')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setWidth(500)
      .setHeight(150);
  SpreadsheetApp.getUi()
  .showModalDialog(htmlDlg, 'Select An Existing Client');
}
我还有一个doGet函数,如下所示:

function doGet() {
 var htmlOutput = template.evaluate()
                   .setSandboxMode(HtmlService.SandboxMode.NATIVE);
  return htmlOutput;
  return HtmlService.createHtmlOutputFromFile('index');
}

<!DOCTYPE html>
<html>
  <head>
     <script>
    // Get dropdown element from DOM
var dropdown = document.getElementById("selectNumber");

// Loop through the array
for (var i = 0; i < myArray.length; ++i) {
    // Append the element to the end of Array list
    dropdown[dropdown.length] = new Option(myArray[i], myArray[i]);
}

    </script>
  </head>
  <body>
<form id="myForm">
  <select id="selectNumber">
    <option>Choose a number</option>
  </select>
</form>
  </body>
</html>
然后,我的HTML表单中的代码如下所示:

function doGet() {
 var htmlOutput = template.evaluate()
                   .setSandboxMode(HtmlService.SandboxMode.NATIVE);
  return htmlOutput;
  return HtmlService.createHtmlOutputFromFile('index');
}

<!DOCTYPE html>
<html>
  <head>
     <script>
    // Get dropdown element from DOM
var dropdown = document.getElementById("selectNumber");

// Loop through the array
for (var i = 0; i < myArray.length; ++i) {
    // Append the element to the end of Array list
    dropdown[dropdown.length] = new Option(myArray[i], myArray[i]);
}

    </script>
  </head>
  <body>
<form id="myForm">
  <select id="selectNumber">
    <option>Choose a number</option>
  </select>
</form>
  </body>
</html>

//从DOM获取下拉列表元素
var dropdown=document.getElementById(“selectNumber”);
//在数组中循环
对于(变量i=0;i

任何帮助都将不胜感激。谢谢

在应用程序脚本中,您可以通过使用 Scriptlet的优点是,您还可以将电子表格中的数据或
code.gs
中的变量合并到选项中

样本:

index.html

更新:

如果您更喜欢使用脚本,则必须在完成其余html内容后,将脚本从头移到身。


这允许您在使用Javascript修改此HTML内容之前呈现HTML内容(基本上创建一个id为“selectNumber”的元素),请参阅。

感谢您的帮助。你能澄清一下code.gs是否是Google Sheets的脚本编写区域吗?就像我说的,我是一个新手:如果有人能解释为什么我现有的代码没有Scriptlet就不能工作,那将是非常棒的:)我更新了我的答案来解释为什么你的脚本不能工作-基本上是关于你插入Javascript的位置。非常感谢你的帮助:)