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

Wix表单AfterSave数据同步至Google Sheets遇问题,求解决方案

Got it, let's get your Wix form data flowing into Google Sheets without a hitch. You've already started on the Google Apps Script side—let's finish that up and tie it to your Wix form's AfterSave event. Here's the complete solution:

Step 1: Complete the Google Apps Script Web App Code

Replace your partial script with this full, tested version. It handles both GET/POST requests, prevents concurrent write conflicts, and writes data to your Sheet1 reliably:

function doGet(e) {
  return handleResponse(e);
}

function doPost(e) {
  return handleResponse(e);
}

var SHEET_NAME = "Sheet1";
var SCRIPT_PROP = PropertiesService.getScriptProperties();

function handleResponse(e) {
  // Lock the script to avoid overlapping writes
  var lock = LockService.getPublicLock();
  lock.waitLock(30000); // Wait up to 30 seconds for access

  try {
    // Auto-set spreadsheet ID on first run (no hardcoding needed)
    var docId = SCRIPT_PROP.getProperty("docId");
    if (!docId) {
      var doc = SpreadsheetApp.getActiveSpreadsheet();
      SCRIPT_PROP.setProperty("docId", doc.getId());
      docId = doc.getId();
    }

    var doc = SpreadsheetApp.openById(docId);
    var sheet = doc.getSheetByName(SHEET_NAME);

    // Map Wix form fields to sheet columns (adjust these to match your form!)
    var formData = {
      fullName: e.parameter.fullName || "No name provided",
      email: e.parameter.email || "No email provided",
      subject: e.parameter.subject || "No subject provided",
      message: e.parameter.message || "No message provided"
      // Add more fields here that correspond to your Wix form inputs
    };

    // Append data as a new row (includes timestamp for reference)
    var newRow = [new Date(), formData.fullName, formData.email, formData.subject, formData.message];
    sheet.appendRow(newRow);

    // Return a success response to Wix
    return ContentService.createTextOutput(JSON.stringify({status: "success", message: "Data saved to Sheets"}))
      .setMimeType(ContentService.MimeType.JSON);
  } catch (error) {
    // Return error details for debugging
    return ContentService.createTextOutput(JSON.stringify({status: "error", message: error.toString()}))
      .setMimeType(ContentService.MimeType.JSON);
  } finally {
    lock.releaseLock();
  }
}

Quick script notes:

  • Match your form fields: Update the formData object to use the exact input names from your Wix form. For example, if you have a "phoneNumber" field, add phoneNumber: e.parameter.phoneNumber || "No number provided".
  • First run setup: Save the script, then run handleResponse once manually (you can pass a dummy e parameter if prompted) to store your spreadsheet ID automatically.
Step 2: Deploy the Google Web App
  1. In the Google Apps Script editor, click Deploy > New deployment.
  2. Click the gear icon and select Web app.
  3. Set these critical options:
    • Execute as: Your Google account (the owner of the spreadsheet)
    • Who has access: Anyone, even anonymous (Wix sends unauthenticated requests, so this is non-negotiable)
  4. Click Deploy, authorize the script when prompted, then copy the generated Web app URL—you'll need this for Wix.
Step 3: Hook Up Wix Form's AfterSave Event
  1. Open your Wix site in the Editor, select your form, then click Events > After Save in the right-hand panel.
  2. Replace the default code with this (paste your Web app URL where indicated):
export function formDataset_afterSave() {
  // Pull the submitted data from your form's dataset
  const submittedItem = $w("#formDataset").getCurrentItem();
  
  // Prepare data to send (match field names to your Google Script!)
  const formData = new URLSearchParams();
  formData.append("fullName", submittedItem.fullName);
  formData.append("email", submittedItem.email);
  formData.append("subject", submittedItem.subject);
  formData.append("message", submittedItem.message);
  // Add all your form fields here

  // Send data to Google Sheets
  fetch("YOUR_GOOGLE_WEB_APP_URL_HERE", {
    method: "POST",
    body: formData
  })
  .then(response => response.json())
  .then(result => {
    console.log("Sheets response:", result);
    // Optional: Show a success message to users
    $w("#successAlert").show();
  })
  .catch(err => {
    console.error("Error sending data:", err);
    // Optional: Show an error message to users
    $w("#errorAlert").show();
  });
}

Wix code notes:

  • Update IDs: Replace #formDataset with your form's actual dataset ID, and adjust #successAlert/#errorAlert if you add those UI elements.
  • Field consistency: Double-check that the keys in formData.append() exactly match the parameter names in your Google Script's formData object.
Test the Flow
  1. Fill out your Wix form and submit it.
  2. Check your Google Sheet—you should see a new row with the form data and a timestamp.
  3. If it fails, check the Wix Editor's console (F12) for errors, and confirm your Web App is deployed with "Anyone, even anonymous" access.

内容的提问来源于stack exchange,提问作者Shantanu Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:28:00