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 triggerfunctioncreateFormSubmitTrigger(){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:
functiononNewDealSubmitted(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:
functiononNewDealSubmitted(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 regionvar repAssignments ={"North":"Alex Martinez","East":"Jordan Lee","South":"Sam Rivera","West":"Sam Rivera"};var salesRep = repAssignments[region]||"Unassigned";// Add to Sales Tracker sheetvar sheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales Tracker"); sheet.appendRow([ customerName, email, product, amount, region, salesRep,"In Progress",newDate()]);// Notify the assigned sales repvar 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:
functionsendConfirmationEmail(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 });}