使用谷歌应用程序脚本将反应表单数据发送到谷歌工作表时如何避免重定向?

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

目前,我正在尝试使用一个简单的表单将其数据发送到谷歌表格,以便网站所有者更容易看到他们的回复。我尝试按照在线教程使用谷歌应用程序脚本,但是一旦提交数据,我就会被重定向到成功的 JSON 响应。

离开另一篇文章:

如何避免在 google-sheet 应用程序脚本中重定向

另一位用户说避免使用内容服务,但我找不到替代方案。我在下面附上了我的谷歌脚本代码和处理我的表单逻辑的组件。

表单组件:

import React from "react"

import { useState } from "react";
export default function Form(props) {
  const [data, setData] = useState({
    name: " ",
    company: "",
    service: "",
    contact: "",
    technician: "",
  });
  const { name, company, service, contact, technician } = data;

  const handleChange = (e) => {
    setData({ ...data, [e.target.name]: e.target.value });
  };

  return (
    <div className="form-container">
      <form 
      action="<my google scripts api endpoint"
      method="POST"
      >
        <div className="form-group">
          <h3 className="form-header">Service Inquiry</h3>
          <label className="input-label">Name:</label>
          <textarea
            type="text"
            name="name"
            value={name}
            onChange={handleChange}
            required
            className="input"
            rows="1"
          ></textarea>
        </div>

        <div className="form-group">
          <label className="input-label">Company Name:</label>
          <textarea
            type="text"
            name="company"
            value={company}
            onChange={handleChange}
            required
            className="input"
            rows="1"
          ></textarea>
        </div>
        <div className="form-group">
          <label className="input-label">What can we do for you?</label>

          <textarea
            type="text"
            name="service"
            value={service}
            onChange={handleChange}
            required
            className="input"
            rows="5"
            cols="50"
          ></textarea>
        </div>
        <div className="form-group">
          <label className="input-label">
            Please leave a way for us to contact you:
          </label>
          <textarea
            type="text"
            name="contact"
            value={contact}
            onChange={handleChange}
            placeholder="Email or Phone"
            className="input"
            rows="1"
          ></textarea>
        </div>
        <div className="form-group">
          <label className="input-label">Request Specific Technician:</label>
          <textarea
            type="text"
            name="technician"
            value={technician}
            onChange={handleChange}
            className="input"
            rows="1"
            placeholder="*optional"
          ></textarea>

          <p className="footnote">
            *if you would like a specific technician, please specify
          </p>
          <input type="submit" value="Send" className="submit-button" />
        </div>
      </form>
    </div>
  );
}

Google 应用程序脚本:

// Original code from https://github.com/jamiewilson/form-to-google-sheets
// Updated for 2021 and ES6 standards

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()
  }
}

提交表格后我收到的信息:

我只是想避免重定向到 JSON。

javascript reactjs forms google-apps-script web-applications
1个回答
0
投票

我仍然面临同样的问题,并且从文档中不清楚 PreventDefault() 方法应该在上面的应用程序脚本中放在哪里。有什么建议吗?

© www.soinside.com 2019 - 2024. All rights reserved.