Reputation:
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 = "[email protected]"
//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
});
}
Upvotes: 0
Views: 1693
Reputation: 38131
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('[email protected]', 'Attachment example', 'Two files are attached.', {
name: 'Automatic Emailer Script',
attachments: [file.getAs(MimeType.PDF), blob]
});
Upvotes: 3