-
Go to Google Sheets and create a new spreadsheet.
-
Name the sheet (e.g., Contact Form Responses).
-
Add headers to the first row to organize submissions:
Name | Email | Message | Timestamp
-
Open the Script Editor:
- Click on Extensions > Apps Script in the menu bar.
-
Delete any placeholder code inside the script editor.
-
Paste Your Script: Use the following code:
function doPost(data) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var timestamp = new Date(); var name = data.parameter.name; var email = data.parameter.email; var message = data.parameter.message; sheet.appendRow([name, email, message, timestamp]); var subject = "New Contact Form Submission from " + name; var body = "You have received a new message from your website contact form.\n\n" + "Name: " + name + "\n" + "Email: " + email + "\n" + "Message: " + message + "\n\n" + "Submitted on: " + timestamp; MailApp.sendEmail({ to: "youremail@gmail.com", cc: "yourccemail@gmail.com", // CC recipient subject: subject, body: body }); return ContentService.createTextOutput('Form submission successful!'); } // Optional test function // function testDoPost() { // var data = { // parameter: { // name: "Hari", // email: "hari@example.com", // message: "This is a test message." // } // }; // Logger.log(doPost(data)); // }
-
Click on "Deploy" > "New Deployment".
-
In the deployment window:
- Select "Web app" as the deployment type.
- Description: Add a description (e.g., "Contact Form Integration").
- Execute as: Select Me (your Google account).
- Who has access: Select Anyone or Anyone with the link to allow your web form to send data to the script.
-
Click "Deploy".
-
Authorize the Script:
- A pop-up will appear asking for permissions. Click Review Permissions.
- Select your Google account.
- Click Advanced > Go to (your project) and grant permissions.
-
Copy the Web App URL:
- After successful deployment, you’ll get a web app URL. Copy it for use in your contact form.
Replace the placeholder App Script URL with your deployed Apps Script web app URL in Script.js
as shown below:
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('contactForm').addEventListener('submit', function(event) {
event.preventDefault();
var formData = new FormData(this); // Collects form data
fetch('https://script.google.com/macros/s/your-app-script-deployment-id/exec', {
// Replace the above URL with your own deployed Apps Script web app URL
method: 'POST',
body: formData
})
.then(response => response.text())
.then(data => {
var confirmationMessage = document.getElementById('confirmationMessage');
confirmationMessage.style.display = 'block';
setTimeout(function() {
confirmationMessage.style.display = 'none'; // Hide after 10 seconds
}, 10000);
document.getElementById('contactForm').reset(); // Reset the form
})
.catch(error => console.error('Error!', error.message));
});
});
- It listens for the submission of the contact form.
- It prevents the default form submission behavior.
- It collects the form data and sends it to your Apps Script URL using a
POST
request. - It displays a confirmation message after submission and resets the form.
Make sure to replace the placeholder URL with your actual Apps Script deployment URL to ensure it works correctly.
- Go to your website and submit the contact form with sample data.
- Check Google Sheets:
- Confirm that the new row appears with the submitted data.
- Check Your Email:
- Verify that you received a notification with the submitted details.
- In the script editor, uncomment the
testDoPost
function. - Click Run > testDoPost to simulate a form submission.
- Check the logs with View > Logs to confirm the behavior.
- Make updates to the Apps Script as needed (e.g., modify email content or handle edge cases).
- Monitor your deployment and make sure that the Google Sheet is recording entries correctly.