【问题标题】:How to get selected option in Google script?如何在谷歌脚本中获得选定的选项?
【发布时间】:2021-04-04 19:52:12
【问题描述】:

我想访问 Google Script 中选定选项的值。

我得到了这个 HTML 表单。有一个选项。

  <form id="invoice" _lpchecked="1">
    <label for="company">company:</label><br>
    <input type="text" id="company" name="company" value=""><br>
    <br>
    <label for="user">created:</label>
    <select id="user">
       <option value="Radek">Radek</option>
       <option value="Lucka">Lucka</option>
     </select>         
    <input type="submit" value="send" onclick="event.preventDefault(); sendForm(this);">
  </form>

表单被发送到 Google Script

  function sendForm(theForm){
    //var invoice = document.getElementById("invoice");
    //invoice.user = document.getElementById("user").value;
    
    google.script.run.withSuccessHandler(formSent)
                     .processForm(document.getElementById("invoice"));
  }

然后在 GAS 中我可以访问表单数据。通过记录表单数据,我发现该选项没有密钥。在form 对象中存在正确的值,但键为空。可以帮助获得 GAS 中的选择选项吗?我尝试将user 键添加到表单数据,但没有成功。

function processForm(form){
  var result = "OK";
  var data = [(new Date()).toLocaleString("cs-CZ"),
             form.company,
             form.user]
  for (var key in form) {
    Logger.log('the key is: ' + key);
    Logger.log('the value is: ' + form[key]);
  };
  saveInvoiceData(data); // saves data to Google Sheets

  return result;
}

有没有办法在 IDE 中使用断点进行调试?

【问题讨论】:

    标签: google-apps-script html-select


    【解决方案1】:

    我认为您的问题的原因是name 属性未在&lt;select id="user"&gt; 中使用。所以请修改如下。

    发件人:

    <select id="user">
    

    收件人:

    <select id="user" name="user">
    

    注意:

    • 当您将脚本用作 Web Apps 时,当脚本被修改时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。

    • 关于这种情况下的调试,很遗憾,在这种情况下,我认为不能直接使用脚本编辑器的调试。那么例如,使用以下脚本怎么样?

        function processForm(form){
          SpreadsheetApp.openById("###").appendRow([JSON.stringify(form)]);
          return "ok";
        }
      
      • 使用该脚本,当提交表单时,可以确认form的值。由此可以发现,使用电子表格,您当前的脚本返回的对象为{"":"###","company":"###"},没有key。

    【讨论】:

    • 你是对的,它是缺少的name。关于调试......当然,我将表单数据保存到电子表格中,但我在调试数据之后将其发送到网络并在保存之前。我使用了 Logger,但想知道是否还有其他方法。
    • @Radek 感谢您的回复。例如,当您使用 Web 应用程序时,这些信息对您的情况有用吗? github.com/tanaikech/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多