Sending HTML form data directly to Google Sheets using Google Apps Script

Need help connecting web form to Google Sheets

I’m trying to build a simple contact form on my website that automatically saves user responses to a Google Sheets document. I want to use Google Apps Script to handle this process.

My HTML form looks like this:

<form method='post' action='https://script.google.com/macros/s/your-script-id...'>
  Your Name: <input type='text' name='userName' />
  Email Address: <input type='text' name='userEmail' />
  <input type='submit' value='Send' />
</form>

Expected result in my spreadsheet:

| userName | userEmail |
   John     [email protected]

Can Google Apps Script handle this type of form submission, or should I look into other Google APIs instead?

EDIT: Found a working solution!

var SHEET_ID = '1Abc2Def3Ghi4Jkl5Mno6Pqr7Stu8Vwx9Yz0...';

function doPost(request) {
  var ui = UiApp.getActiveApplication();
  
  SpreadsheetApp.openById(SHEET_ID).getSheetByName('Contacts').appendRow([request.parameter.userName, request.parameter.userEmail]);
  
  ui.add(ui.createLabel("Thanks! We received your info: " + request.parameter.userName));
  
  return ui;
}

function buildForm(){
   var ui = UiApp.createApplication();
   var formPanel = ui.createFormPanel();
   var container = ui.createVerticalPanel();
   
   container.add(ui.createLabel('Sign up for our newsletter'));
   container.add(ui.createTextBox().setName('userName').setId('userName'));
   container.add(ui.createTextBox().setName('userEmail').setId('userEmail'));
   container.add(ui.createSubmitButton("Subscribe"));
   
   formPanel.add(container);
   ui.add(formPanel);
   
   return ui;
}

You’re on the right track, but there’s an easier way. Skip building the form in Apps Script entirely. Since you’ve got HTML on your site already, just use doPost() to handle the data and send back a redirect or JSON response. I’ve done this tons of times - ContentService.createTextOutput() works way better for cross-domain stuff. Don’t forget CORS headers if your form’s on a different domain than script.google.com. Deploy your script with ‘Anyone’ access and ‘User accessing the web app’ permissions, or you’ll hit auth errors when someone submits.

hey, just a heads up - UiApp’s been deprecated for years n doesn’t work anymore. You’ll need HtmlService for modern GAS projects. Plus, deploy your script as a web app with the right permissions or form submissions will fail silently.

Apps Script handles form submissions fine, but your approach has problems. HTML forms expect redirects, so if you’re using ContentService to return JSON or text, browsers just show that raw response instead of staying put. I’d use fetch() or XMLHttpRequest instead. Keep your HTML form but catch the submit event with JavaScript, then send the data via fetch() to your Apps Script. You’ll get better error handling and can show success messages without the page jumping around. One more thing - validate everything server-side in your doPost function before writing to sheets. Once deployed, anyone can hit your endpoint and submit whatever they want.