Home > database >  Trying to make Google Apps Script to Automate Invoice email. (Error: Exception: Invalid email: [obje
Trying to make Google Apps Script to Automate Invoice email. (Error: Exception: Invalid email: [obje

Time:12-30

Currently trying to make a script that automates invoice email. The script takes info from a Google Spreadsheet and populates a Google Form. The Form is then converted to PDF and emailed. I keep running into the same error.

My knowledge of programming is very basic, and the script itself is taken entirely from video sources.

const TEMPLATE_FILE_ID = '1R-EX_iu418HrG8dTpS0MU04okzHa4-qQOd3b5ggvZKY';
const DESTINATION_FOLDER_ID = '1D4DlsrLEClE4wfHHu0FK3tQP84ZLirl4';
const CURRENCY_SIGN = 'Php.';
const PDF_FOLDER_ID = DriveApp.getFolderById("1q1DvS9-48oGaY8j5bWy2sgiR4SzRclEa");

function toCurrency(num) {
    var fmt = Number(num).toFixed(2);
    return `${CURRENCY_SIGN}${fmt}`;
}

function toDateFmt(dt_string) {
  var millis = Date.parse(dt_string);
  var date = new Date(millis);
  var year = date.getFullYear();
  var month = ("0"   (date.getMonth()   1)).slice(-2);
  var day = ("0"   date.getDate()).slice(-2);

  return `${year}-${month}-${day}`;
}

function parseFormData(values, header) {
    var subtotal = 0;
    var discount = 0;
    var response_data = {};

    for (var i = 0; i < values.length; i  ) {
      var key = header[i];
      var value = values[i];

    
      if (key.toLowerCase().includes("price")) {
        subtotal  = value;
        value = toCurrency(value);

      
      } else if (key.toLowerCase().includes("discount")) {
        discount  = value;
        value = toCurrency(value);
      
      } else if (key.toLowerCase().includes("date")) {
        value = toDateFmt(value);
      }

      response_data[key] = value;
    }

    response_data["sub_total"] = toCurrency(subtotal);
    response_data["total"] = toCurrency(subtotal - discount);

    return response_data;
}


function populateTemplate(document, response_data) {

    var document_header = document.getHeader();
    var document_body = document.getBody();

    for (var key in response_data) {
      var match_text = `{{${key}}}`;
      var value = response_data[key];

      
      document_header.replaceText(match_text, value);
      document_body.replaceText(match_text, value);
    }

}


function createPDF(response_data) {

  
  var sheet = SpreadsheetApp.getActiveSheet();
  var last_row = sheet.getLastRow() - 1;

  
  var range = sheet.getDataRange();
 
  
  var data = range.getValues()[last_row];
  
  
  var headers = range.getValues()[0];

  
  var response_data = parseFormData(data, headers);

  
  const template_file = DriveApp.getFileById(TEMPLATE_FILE_ID);
  const target_folder = DriveApp.getFolderById(DESTINATION_FOLDER_ID);

 
  var filename = `${response_data["Invoice Date"]}_${response_data["Company Name"]}_${response_data["Invoice Number"]}`;
  const newTempDoc = template_file.makeCopy(filename, target_folder);

  
  var document = DocumentApp.openById(newTempDoc.getId());

  
  populateTemplate(document, response_data);
  document.saveAndClose();
  
  const blobPDF = newTempDoc.getAs(MimeType.PDF);
  const pdfFile = PDF_FOLDER_ID.createFile(blobPDF).setName(response_data["Company Name"]  "_Invoice");
  target_folder.removeFile(newTempDoc);

  GmailApp.sendEmail(toString(response_data["Email"]), "My Company Invoice", "Attached is your invoice. Have a good day!", {attachments: pdfFile, name: '"My Company Name"'});
}

My problem is apparently on the last line

GmailApp.sendEmail(toString(response_data["Email"]), "My Company Invoice", "Attached is your invoice. Have a good day!", {attachments: pdfFile, name: '"My Company Name"'});

I tried to convert the email into a string but that resulted in the error shown in the title. At this point, I'd welcome any advice.

CodePudding user response:

Considering response_data["Email"] is returning a valid email.

Try replacing this:

GmailApp.sendEmail(toString(response_data["Email"]), "My Company Invoice", "Attached is your invoice. Have a good day!", {attachments: pdfFile, name: '"My Company Name"'});

with this :

GmailApp.sendEmail(response_data["Email"].toString(), "My Company Invoice", "Attached is your invoice. Have a good day!", {attachments: pdfFile, name: '"My Company Name"'});

Reference:

sendEmail

  • Related