Home > database >  Google Apps Script send attachment
Google Apps Script send attachment

Time:10-04

currently I can send pdf from google spreadsheet to email with this script with attachment, but the attachment fail to send

this is my current script :

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HELPER").hideSheet();
  var startRow = 2; // First row of data to process
  var getLastRow = 4000; // Number of rows to process
  var dataRange = sheet.getRange(startRow, 4, getLastRow, 7);
  var data = dataRange.getValues();
  for (var i = 0; i < data.length;   i) {
    var row = data[i];
    var trigger = row[0];
    var emailAddress = row[2]; // Email column
    if(emailAddress != "" && emailAddress != " "){ 
      var message = "Hi,\n\nSilahkan download dan print attachment ini.\n\nTerima Kasih,\nSistemHBR"; // Content column
      var subject = "FORM CUTI";
      var status_email = row[6];
      if ((trigger != 'FALSE') && (status_email != 'EMAIL_SENT')); // Prevents sending duplicates
      attachments = [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("FORM CUTI")]; { 
        MailApp.sendEmail(emailAddress, subject, message);
        sheet.getRange(startRow   i, 7).setValue('EMAIL_SENT');
        // Make sure the cell is updated right away in case the script is interrupted
        SpreadsheetApp.flush();
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HELPER").activate();
        
  }
  //MailApp.sendEmail(message);
  //SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HELPER").activate();
   }
  }
}

What's wrong with my code above?

Thank You

CodePudding user response:

In your script, I thought that when if ((trigger != 'FALSE') && (status_email != 'EMAIL_SENT')) is true, you might have wanted to send the email. So, how about the following modification?

From:

if ((trigger != 'FALSE') && (status_email != 'EMAIL_SENT')); // Prevents sending duplicates
 attachments = [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("FORM CUTI")]; { 
   MailApp.sendEmail(emailAddress, subject, message);
   sheet.getRange(startRow   i, 7).setValue('EMAIL_SENT');
   // Make sure the cell is updated right away in case the script is interrupted
   SpreadsheetApp.flush();
   SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HELPER").activate();

}

To:

if ((trigger != 'FALSE') && (status_email != 'EMAIL_SENT')) {
  attachments = [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("FORM CUTI")];
  MailApp.sendEmail(emailAddress, subject, message, {attachments: attachments});
  sheet.getRange(startRow   i, 7).setValue('EMAIL_SENT');
  // Make sure the cell is updated right away in case the script is interrupted
  SpreadsheetApp.flush();
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HELPER").activate();
}
  • In this case, when (trigger != 'FALSE') && (status_email != 'EMAIL_SENT') is true, an email is sent by including the attachment file.

References:

  • Related