Google Apps 脚本 - 调用“google.script.run.function();”时,.HTML 模态菜单不执行 .gs 函数

问题描述 投票:0回答:1

我遇到了一个奇怪的 GAS 问题,每当我生成 MODAL 菜单并提交它时,有时 google.script.run 可以工作,有时它根本不执行我想要的功能。

有人遇到过 google.script.run.insertDesiredFunctionHere() 只能偶尔工作的问题吗?

这是我制作的 HTML 模式菜单,每当按下提交按钮时,google.script.run 似乎并不想可靠地执行我想要的功能。在这种情况下,特别是“google.script.run.processForm1(form)”:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
      body {
        background-color: Gray;
        font-size: 18px;
      }
      input, select, button {
        display: block;
        margin-bottom: 20px;
        font-size: 18px;
      }
      input {
        width: 95%;
        font-size: 16px;
        color: Black;
        font-family: Arial, sans-serif;
        box-sizing: border-box;
        border: groove;
      }
      input::placeholder {
        color: DimGrey;
      }
      label {
        font-family: Arial, sans-serif;
        font-size: 22px;
        color: White;
      }
      select {
        width: 95%;
        font-size: 16px;
        color: Black;
        font-family: Arial, sans-serif;
        box-sizing: border-box;
        border: groove;
      }
      select::value {
        color: DimGrey;
      }
      button {
        background-color: blue;
        color: white;
        font-size: 24px;
        padding: 10px 20px;
        border: none;
        cursor: pointer;
        border-radius: 5px;
      }
    </style>
  </head>
  <body>
    <form id="form1" onsubmit="submitForm1(); return false;">

      <br>

      <label for="warehouse_number">Warehouse Number?</label>
      <input type="text" id="warehouse_number" name="warehouse_number" placeholder="Example: 110" oninput="updateOutput()" required>

      <label for="signal_time">What time was the signal received?</label>
      <input type="text" id="signal_time" name="signal_time" placeholder="Example: 13:31" required><br>

      <label for="zone_number">Zone Number?</label>
      <input type="text" id="zone_number" name="zone_number" placeholder="Example: 123" required><br>

      <label for="zone_description">Zone Description?</label>
      <input type="text" id="zone_description" name="zone_description" placeholder="Example: Receiving Man Door" required><br>

      <label for="camera_check">Camera Check</label>
      <select id="camera_check" name="camera_check" required>
        <option value="" selected>Were camera views available?</option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>
      </select><br>

      <label for="misc_notes">Misc Findings/Notes?</label>
      <input type="text" id="misc_notes" name="misc_notes" placeholder="Example: 'A bird was seen stuck in the tire center.'"><br>

      <button type="submit">Submit</button>
    </form>
    <script>
      function submitForm1() {
        var form = document.getElementById('form1');
        try {
        google.script.run.processForm1(form);
        } catch(err){
          SpreadsheetApp.getUi().alert("Something went wrong!! Please screenshot this and send it to REDACTED - Error Code: " + err);
        }
        google.script.host.close();
      }
    </script>
  </body>
</html>

在下面的屏幕截图中,您将看到我运行该函数来打开模态菜单(该模态菜单的 html 位于上面),为其提供一些随机数据并提交。第一次没有任何反应时,它甚至不会尝试调用我的函数 (processForm1) - 但当我立即再次尝试时,您可以看到(以黄色突出显示)processForm1 函数被调用并按预期工作。我正在用头撞墙,试图找出为什么 google.script.run 不可靠......任何见解将不胜感激!

Image of the GAS execution log showing 2 modal menus ran, but only 1 executing the function on submit

尝试在多个帐户上运行脚本,google.script.run 无法可靠运行。执行日志显示没有尝试运行函数。

javascript html modal-dialog
1个回答
0
投票

添加了成功处理程序,在收到成功后关闭模式。这解决了这个问题。 `

  function submitForm1(){
    var submitBtn = document.getElementById('submit');
    submitBtn.disabled = true;
    var form = document.getElementById('form1');
    google.script.run
      .withSuccessHandler(onSuccess)
      .processForm1(form);
      submitBtn.textContent = 'Please wait!';
      return true;
  }

  function onSuccess(){
    google.script.host.close();
  }

</script>`
© www.soinside.com 2019 - 2024. All rights reserved.