Send email to form submitter using google web app

144 views
Skip to first unread message

Jessica Anderson

unread,
Apr 4, 2021, 6:10:07 PM4/4/21
to Google Apps Script Community
Hello,
I am new to google app script.
I have a google web app and trying to use it with my project, to send web form info to google sheet when form is submitted.
I am using a custom form not google form.

The web app works fine, on form submit, name and email will be added to google sheet.

 however I want to send email notification/ autoresponder  as  thank your for submission, after form submission to the form submitter.

I have a code snippet that is available online, it used to work before with google app script, however I am not sure why it does not work in my web app.

Here is the snippet:

function onFormSubmit(e){

var date = e.values [0];
var userEmail = e.values[1];
var userName = e.values[2];

var subject = "Form Submitted";
var message = "Thank you, " + userName + " for signup " + date;
MailApp.sendEmail(userEmail, subject, message);
}

I have checked the userEmail and Name values [1] and [2] are correct and same as in google sheet.

Here is the web app that works, except the last part for sending email,  I have setup a trigger on form submit as well.
appreciate your help.



var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("signups");

function startTime() {
  ss.getRange(ss.getLastRow() + 1, 1).setValue(new Date());
}

var sheetName = 'signups'
var scriptProp = PropertiesService.getScriptProperties()

function intialSetup () {
  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
  scriptProp.setProperty('key', activeSpreadsheet.getId())
}

function doPost (e) {
  var lock = LockService.getScriptLock()
  lock.tryLock(10000)

  try {
    var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
    var sheet = doc.getSheetByName(sheetName)

    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
    var nextRow = sheet.getLastRow() + 1

    var newRow = headers.map(function(header) {
      return header === 'timestamp' ? new Date() : e.parameter[header]
    })

    sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])

    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
      .setMimeType(ContentService.MimeType.JSON)
  }

  catch (e) {
    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
      .setMimeType(ContentService.MimeType.JSON)
  }
  finally {
    lock.releaseLock()
  }

}

// This is the added part for sending email to form submitter that does not work
function onFormSubmit(e){

var date = e.values [0];
var userEmail = e.values[1];
var userName = e.values[2];

var subject = "Form Submitted";
var message = "Thank you, " + userName + " for signup " + date;
MailApp.sendEmail(userEmail, subject, message);
}

Alan Wells

unread,
Apr 4, 2021, 7:10:21 PM4/4/21
to Google Apps Script Community
Unless a Google Form is used, then an installed trigger for the the "On Form Submit" event will not run.
Your Web App can use the google.script.run.myFunction() client side API to run a server side function.
If you are not familiar with google.script.run, then please take a look at the documentation:

Also, you may want to look at the troubleshooting information:
If you still have questions, please post back.

cbmserv...@gmail.com

unread,
Apr 5, 2021, 12:07:29 AM4/5/21
to google-apps-sc...@googlegroups.com

I think using your webapp to send an email is fine. It should work without having to go to a Google Form.

 

However, there are 2 suggestions I would recommend:

 

  1. The onFormSubmit function needs to be called from your doPost function for it to run. (unless you are using an on form submit trigger as Alan is indicating where you would need to use Google Forms and a trigger to call it.
  2. The e variable being used is really whatever parameters your form is sending. So make sure you are using the correct parameters and not e.values[1[ and [2]. The variable needs to match what you are passing in the webapp call.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/8006aa11-f0a8-49d4-9f3c-c6f005bd4b9dn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages