如何在 Google 脚本中获取选定的选项?

How to get selected option in Google script?

我想访问 Google 脚本中 selected 选项的值。

我收到了这张 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脚本

  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 中的 select 选项吗?我试图将 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 中使用断点进行调试?

我认为您的问题的原因是 name 属性未在 <select id="user"> 处使用。所以请修改如下。

发件人:

<select id="user">

收件人:

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

注:

  • 当您使用脚本作为Web Apps时,当脚本被修改时,请重新部署Web Apps为新版本。由此,最新的脚本被反​​映到Web Apps。请注意这一点。

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

      function processForm(form){
        SpreadsheetApp.openById("###").appendRow([JSON.stringify(form)]);
        return "ok";
      }
    
    • 使用这个脚本,当提交表单时,可以确认form的值。由此可以发现,使用Spreadsheet,你当前的脚本returns {"":"###","company":"###"}的object没有key.