onFormSubmit Trigger

The onFormSubmit(e) trigger fires automatically whenever a Google Form is submitted. Combined with a Google Sheet that collects form responses, it lets you process new submissions in real time — sending confirmations, updating records, notifying team members, or routing data.

Setting Up the Trigger

The simple onFormSubmit function works for spreadsheet-bound forms. For full permissions (email, Drive), use an installable trigger:

// Run once to install the trigger function createFormSubmitTrigger() { var ss = SpreadsheetApp.getActiveSpreadsheet(); ScriptApp.newTrigger("onNewDealSubmitted") .forSpreadsheet(ss) .onFormSubmit() .create(); Logger.log("Form submit trigger installed."); }

The Event Object

When a form is submitted, e contains:

function onNewDealSubmitted(e) { Logger.log("Named values: " + JSON.stringify(e.namedValues)); Logger.log("Range: " + e.range.getA1Notation()); // e.namedValues maps question titles to arrays of answers }

Processing a New Lead Submission

Suppose Acme Corp uses a Google Form to log new leads. The form has fields: Customer Name, Email, Product, Amount, Region. When submitted, this trigger adds the entry to the Sales Tracker and sends a notification:

function onNewDealSubmitted(e) { var values = e.namedValues; var customerName = values["Customer Name"][0]; var email = values["Email"][0]; var product = values["Product"][0]; var amount = parseFloat(values["Amount"][0]) || 0; var region = values["Region"][0]; // Assign a sales rep based on region var repAssignments = { "North": "Alex Martinez", "East": "Jordan Lee", "South": "Sam Rivera", "West": "Sam Rivera" }; var salesRep = repAssignments[region] || "Unassigned"; // Add to Sales Tracker sheet var sheet = SpreadsheetApp .getActiveSpreadsheet() .getSheetByName("Sales Tracker"); sheet.appendRow([ customerName, email, product, amount, region, salesRep, "In Progress", new Date() ]); // Notify the assigned sales rep var repEmails = { "Alex Martinez": "[email protected]", "Jordan Lee": "[email protected]", "Sam Rivera": "[email protected]" }; var repEmail = repEmails[salesRep]; if (repEmail) { GmailApp.sendEmail( repEmail, "New Lead Assigned: " + customerName, "Hi " + salesRep + ",\n\nA new lead has been assigned to you:\n\n" + "Customer: " + customerName + "\n" + "Email: " + email + "\n" + "Product Interest: " + product + "\n" + "Region: " + region + "\n\n" + "Log in to the Sales Tracker to follow up." ); Logger.log("Lead assigned to " + salesRep); } }

Sending a Confirmation to the Customer

After the new lead is logged, send an automatic acknowledgment to the customer:

function sendConfirmationEmail(customerName, email, product) { var firstName = customerName.split(" ")[0]; var htmlBody = ` <p>Hi ${firstName},</p> <p>Thank you for your interest in the <strong>${product}</strong> from Acme Corp.</p> <p>One of our team members will be in touch within 24 hours.</p> <br> <p>Best regards,<br>The Acme Corp Sales Team</p> `; GmailApp.sendEmail( email, "We received your inquiry – Acme Corp", "Thank you for your interest in " + product + ". We'll be in touch shortly.", { htmlBody: htmlBody } ); }

Call this from within onNewDealSubmitted():

sendConfirmationEmail(customerName, email, product);

Accessing Submitted Data by Column Index

If you prefer column positions over named values:

function onNewDealSubmitted(e) { var row = e.range.getValues()[0]; // Column order matches your form response sheet var timestamp = row[0]; var customerName = row[1]; var email = row[2]; var product = row[3]; Logger.log("New submission: " + customerName + " – " + product); }