简体   繁体   中英

I can't send an email with pdf as attachment by google script

I'm making a script that get a spreadsheet, transform it in a pdf file and gets this pdf saved in a folder and send it as attachment by email. however I'm using the only function that I found to send attachments by email but when I open the email it shows the message "[object Object]" on email. Can someone help me?

function SendEmail(){



  var sheetMail = SpreadsheetApp.getActiveSpreadsheet();
  var ecrvc = sheetMail.getSheetByName('List');
  var planilha = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('List')
  var 
  var subject = "Subject"
  var message = "message"


  var email = "person1@gmail.com"


//var ecrvc = "Sheet1";
var folderID = "folderId"; // Folder id to save in a folder.
var pdfName = "YourSpreadsheet of "+week+"-20";

var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(ecrvc);
var folder = DriveApp.getFolderById(folderID);

//Copy whole spreadsheet
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder))


var destSheet = destSpreadsheet.getSheets()[0];

//save to pdf
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var newFile = folder.createFile(theBlob);

//Delete the temporary sheet
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);




//Send the email with attachments  
var arquivo = theBlob;
 DriveApp.getFileById(theBlob); 

  MailApp.sendEmail(email, subject,{
     // htmlBody: message + "", noReply:true,{
    attachments: [file.getAs.file(arquivo.PDF)],
    name: pdfName
  });   

}

There are several problems

file is not declared and getAs syntax is wrong.

Example taken from https://developers.google.com/apps-script/reference/mail/mail-app#sendemailrecipient,-subject,-body,-options

// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
MailApp.sendEmail('mike@example.com', 'Attachment example', 'Two files are attached.', {
    name: 'Automatic Emailer Script',
    attachments: [file.getAs(MimeType.PDF), blob]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM