You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Sheets Web App返回成功提示但未添加行的技术求助

Google Sheets 位置捕获Web App突然失效:返回成功但未写入数据

我开发的一款用于捕获用户位置的Google Sheets Web App今日突然失效:虽返回成功提示(successMessage),但并未向Sheet中添加新行。我使用了昨日可正常运行的完全相同的代码,且尝试了Web App和Spreadsheet的所有权限选项,问题仍未解决。

HTML脚本

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-16">
    <title>Google Sheet Form</title>
    <style>
      form {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 1rem;
      }

      label {
        font-weight: bold;
      }

      input[type="text"],
      input[type="email"] {
        padding: 0.5rem;
        border: 2px solid gray;
        border-radius: 0.5rem;
        font-size: 2rem;
        width: 100%;
      }

      /* Adjust Get Location button size */
    button[type="button"] {
      padding: 0.5rem;
      border: none;
      border-radius: 0.5rem;
      font-size: 5rem; /* Change this value to adjust the font size */
      background-color: green;
      color: white;
      cursor: pointer;
    }

      button[type="submit"] {
        padding: 0.5rem;
        border: none;
        border-radius: 0.5rem;
        font-size: 5rem;
        background-color: blue;
        color: white;
        cursor: pointer;
      }

      button[type="submit"]:hover {
        background-color: darkblue;
      }
    </style>

    <script>
    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      } else {
        alert("Geolocation is not supported by this browser.");
      }
    }

    function showPosition(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      document.getElementById("Latitude").value = latitude;
      document.getElementById("Longitude").value = longitude;
    }

    function showSuccessMessage() {
      const successMessage = document.getElementById('successMessage');
      successMessage.style.display = 'block';
    }

    function sample1(e) {
      const latitude = document.getElementById("Latitude").value;
      const longitude = document.getElementById("Longitude").value;
      if ([latitude, longitude].includes("")) {
        alert("Latitude and/or Longitude are not inputted.");
      } else {
        google.script.run.withFailureHandler(err => console.log(err))
        .withSuccessHandler(f => {
          const responseText = '{"result": "success"}';
          const response = JSON.parse(responseText);
          if (response.result === 'success') {
            showSuccessMessage();
          }
        }).sample2(e);
      }
    }
    </script>
  </head>
<body>
  <form>
    <span>Logged In: <?= email ?></span>
    <p> <input size="20" name="Email" type="email" placeholder="Email" required value="<?= email ?>" readonly> </p>
    <p> <input size="20" type="text" id="Latitude" name="Latitude" required readonly> </p>
    <p> <input size="20" type="text" id="Longitude" name="Longitude" placeholder="Longitude" required readonly> </p>
    <p> <button size="20" type="button" onclick="getLocation()">Get Location</button> </p>
    <p> <button size="20" type="submit" onclick="sample1(this.parentNode.parentNode); return false;">Sign In/Sign Out</button>
    </p>

    <!-- Add a div to display the success message -->
    <div id="successMessage" style="display: none;">Form submitted successfully!</div>

    <!-- Add a div to display the error message -->
    <div id="errorMessage" style="display: none;">An error occurred. Please try again.</div>
  </form>
</body>
</html>

GS脚本

const sheetName = 'Sheet1'
const scriptProp = PropertiesService.getScriptProperties()

function initialSetup () {
  const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
  scriptProp.setProperty('key', activeSpreadsheet.getId())
}

function doPost (e) {
  const lock = LockService.getScriptLock()
  lock.tryLock(10000)

  try {
    const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
    const sheet = doc.getSheetByName(sheetName)

    const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
    const nextRow = sheet.getLastRow() + 1

    const newRow = headers.map(function(header) {
      return header === 'Date' ? new Date() : e.parameter[header]
    })

    sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])

    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
      .setMimeType(ContentService.MimeType.JSON)
  }

  catch (e) {
    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
      .setMimeType(ContentService.MimeType.JSON)
  }

  finally {
    lock.releaseLock()
  }
}

function doGet(e) {
    const userEmail = Session.getActiveUser().getEmail();
    var htmlOutput =  HtmlService.createTemplateFromFile('Form');
    htmlOutput.email = userEmail;
    return htmlOutput.evaluate();
}

const sample2 = e => doPost({ parameter: e });

内容的提问来源于stack exchange,提问作者Pieter le Roux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 17:41:01