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
formDataobject to use the exact input names from your Wix form. For example, if you have a "phoneNumber" field, addphoneNumber: e.parameter.phoneNumber || "No number provided". - First run setup: Save the script, then run
handleResponseonce manually (you can pass a dummyeparameter if prompted) to store your spreadsheet ID automatically.
Step 2: Deploy the Google Web App
- In the Google Apps Script editor, click Deploy > New deployment.
- Click the gear icon and select Web app.
- 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)
- 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
- Open your Wix site in the Editor, select your form, then click Events > After Save in the right-hand panel.
- 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
#formDatasetwith your form's actual dataset ID, and adjust#successAlert/#errorAlertif you add those UI elements. - Field consistency: Double-check that the keys in
formData.append()exactly match the parameter names in your Google Script'sformDataobject.
Test the Flow
- Fill out your Wix form and submit it.
- Check your Google Sheet—you should see a new row with the form data and a timestamp.
- 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
相关产品推荐
相关产品推荐

