Nadja
Nadja

Reputation: 11

Send an email from a google script - trigger is not working

I have at custom html form, sending all data into my google sheet. With this code - its working.

var sheetName = 'Ark1'
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()
  }

 }

But now i want to send some data of the form to myself in an Email. For this i created an additional script like this:

function sendEmail(e){

 var sheet = SpreadsheetApp.getActiveSheet();
 var dataRange = sheet.getRange(sheet.getLastRow(), 1, 1, 16);
 var data = dataRange.getValues();

 var row = data[0];

      var myEmail = "[email protected]";       
      var virksomhed = row[1];
      var email = row[3];
      [...]
      var yderlig = row[15];

      var subject = "Formular: " + virksomhed;
      var message = "Yderlig:" + "\t" + yderlig; 

   MailApp.sendEmail (myEmail, email, subject, message);
   }

On running the script, it is working. But it is not working setting up a trigger.

Does someone know what I am doing wrong? Is it because of its a doPost function to get the data from my form? I also tried to use the MailApp.sendEmail inside the doPost function following that advise: https://support.google.com/docs/thread/26659691?hl=en But that isnt working as well - at least the way i tried to set up the code.

Here is an example of the code inside the doPost.

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

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

    var virksomhed = String(newRow[1]);
    var myEmail = '[email protected]';
    var subject = 'Forform';
    var body = 'Indsendt formular fra' + virksomhed;
    MailApp.sendEmail(myEmail, subject, body);

  }

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

  finally {
    lock.releaseLock()
  }

 }

It seems like MailApp.sendEmail is just getting ignored when its inside a doPost – can that be? And if yes, is there no chance at all of getting a email notification when there is added a new row to spreadsheet?!

Upvotes: 1

Views: 1125

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11268

You should publish a new version of the web app for it to use your modified code with the doPost callback. Inside the script editor, go to Resources, Publish Web app and publish a new version.

Upvotes: 0

Related Questions