Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get notified by email on each form submission? #37

Open
catrawalkar opened this issue Aug 17, 2020 · 5 comments
Open

How can I get notified by email on each form submission? #37

catrawalkar opened this issue Aug 17, 2020 · 5 comments

Comments

@catrawalkar
Copy link

Any way I can get notified each time someone submits the form?

@catrawalkar
Copy link
Author

@jamiewilson Can you please help

@catrawalkar
Copy link
Author

catrawalkar commented Sep 12, 2020

var sheetName = 'Sheet1'
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 body = "";
    var newRow = headers.map(function(header) {
      body += '<h4>'+ header + '</h4><div>' + (header === 'timestamp' ? new Date() : e.parameter[header]) + '</div>';
      return header === 'timestamp' ? new Date() : e.parameter[header]
    })

    sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
    
    var email = "catrawalkar@gmail.com";
 
    var subject = "["+ SpreadsheetApp.getActiveSpreadsheet().getName() + "] New Entry";
    
    MailApp.sendEmail(email, subject, body, {
      htmlBody: body
    });

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

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

  finally {
    lock.releaseLock()
  }
}

@nathanielperales
Copy link

Have you tested this out? Does this work?

@pareshpandit
Copy link

Is there a possibility that someone might be able to help us update this script?
At this point in time, it does not seem work--it does not post the data to the sheet.

@syedbar
Copy link

syedbar commented Feb 17, 2023

i am getting the dta in sheet not getting any notification on gmail.

using below script

const sheetName = 'Sheet2022'
const scriptProp = PropertiesService.getScriptProperties()

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

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

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

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

var body = "";
var newRow = headers.map(function(header) {
body += '

'+ header + '

' + (header === 'timestamp' ? new Date() : e.parameter[header]) + '
';
return header === 'timestamp' ? new Date() : e.parameter[header]
})

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

var email = "s*******r@gmail.com";

var subject = "["+ SpreadsheetApp.getActiveSpreadsheet().getName() + "] New Entry";

MailApp.sendEmail(email, subject, body, {
  htmlBody: body
});


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()
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants