So, I recently have been working on a project with google sheets, where I am getting some data and creating files with that data to email as attachment. I am
- copying my template file
- setting a name to that template copy file
- opening it with the new copy id
- doing my replacements of tags (changing values of the copy template file)
- sending it as an email
function sendEmail(){
for (var row in rangeData) {
var varify = false;
var fileName = new Date().toString();
Logger.log("template " + templateId);
var fileId = DriveApp.getFileById(templateId).makeCopy().getId();
DriveApp.getFileById(fileId).setName(fileName);
var body = DocumentApp.openById(fileId).getBody();
Logger.log('current row ' + rangeData[row]);
for (var column in rangeData[row]) {
var target = firstRowMapped.indexOf(columnNames[column]);
if (target != -1) {
Logger.log('found Key');
try {
body.replaceText(firstRowMapped[target].toString(), rangeData[row]
[target].toString());
varify = true;
} catch (err) {
Logger.log(err);
}
}
}
if (varify == true) {
var toSend = DocumentApp.openById(fileId);
// send mail
try {
MailApp.sendEmail(rangeData[row][emailColumn], 'Test Email', rangeData[row][0], {
name: 'Emailer Script from DOER',
cc: rangeData[row][ccColumn],
attachments: toSend.getAs(MimeType.PDF)
});
Logger.log('sent');
} catch (err) {
Logger.log(err);
}
}
}
}
I am getting my expected files on google drive. The Id of those files match that of toSend
yet when I am getting my email, I get the fileId
. I have been trying to figure this out for days, no luck. Does anyone know why this might be happening? Any Ideas? I personally have send countless attachments through this process (not with a template though) and never had this problem.