I created an html form to allow users to submit their emails to spreadsheet tab "Emails"
In the spreadsheet I created another tab "Sort Emails" to sort the results of "Emails" tab in a reverse order so while people submit their emails, I have the new submitted email in Sort Emails B2.
The problem is: Now I am trying to automatically send an email to new submitted email on-Submit, i.e once a new user submits the form, the script sends an email to the user's email that is supposed to be located in Sort Emails B2. Any help please. Thanks in advance!
Please note when I run the function SendEmail manually from the sheet script editor, it does the job but my question is about running the function automatically on form submit
I'm not much experienced but I think we need a trigger to run the SendEmail function automatically whenever a new row is added to the sheet and of course I tried the current project's triggers in the script editor (both onEdit and onChange) but no luck (I don't understand why??)
This is my html file:
<!DOCTYPE html>
<html lang="en">
<body>
<form name="Subscribe" id="Subscribe" action="https://script.google.com/macros/s/ScriptKey/exec" method="POST" onsubmit="myFunction()">
Form Inputs ..
</form>
<script>
function myFunction() {
google.script.run.SendEmail();
}
</script>
</body>
</html>
And this is my gs file:
function doGet() {
return HtmlService.createTemplateFromFile('Form.html')
.evaluate() // evaluate MUST come before setting the Sandbox mode
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
var sheetName = 'Emails'
var scriptProp = PropertiesService.getScriptProperties()
function doPost (e) {
var lock = LockService.getScriptLock()
lock.tryLock(10000)
try {
var doc = SpreadsheetApp.getActive();
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])
var y = sheet.getLastRow();
var x = sheet.getRange(y,2).getValues();
return ContentService
.createTextOutput(JSON.stringify({ 'Central Says': 'Successfully subscribed', 'Email': x }))
.setMimeType(ContentService.MimeType.JSON)
}
catch (e) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
.setMimeType(ContentService.MimeType.JSON)
}
finally {
lock.releaseLock()
}
}
function SendEmail() {
var e = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sort Emails").getRange("B2").getValues();
var m = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange("A1").getValues();
var subject = 'Welcome ..';
MailApp.sendEmail(e, subject, m);
}