Edit: I've confirmed the order is operating correctly - but it's still emailing an empty template rather than with the written data.
In short, this is what I'm trying to achieve:
createDoc
with an array of dataI have all the operations performing correctly & in correct order (confirmed via console.log output), however, it's emailing an empty template PDF rather than the PDF with data written. What is happening here?
var FILE_NAME = 'Initial Checklist Export';
function email(pdf){
var emailObj = {
to: Session.getActiveUser().getEmail(),
subject: FILE_NAME,
htmlBody: pdf.getUrl(),
noReply: true
};
MailApp.sendEmail(emailObj);
}
function createPDF(tmpDoc) {
var blob = tmpDoc.getAs(MimeType.PDF);
var pdfFile = DriveApp.createFile(blob);
pdfFile.setName(FILE_NAME);
email(pdfFile);
}
function writeData(tmpDoc, sheet, checklist) {
var data_pos = ['B8', 'B10', 'B9', 'B11', 'E8', 'E9', 'E10', 'E11', 'B7', 'E7', 'B15', 'B16',
'B17', 'B18', 'B20', 'B21', 'B23', 'B24', 'B26', 'B27', 'B29','B31','B32',
'B33', 'B34','B35','B36','B38','B39','B40','B41','B43', 'E14','E15','E16',
'E17','E18','E19','E20','E21','E22','E23','E25','E26','E27', 'E28','E29',
'E30','E32','E33','E34','E35','E36','E37','E38','E39','E40','E41', 'E42',
'E43','E45','E46','E47','E48','E49','E50','E51','E52', 'E53','E54', 'E55',
'E56','E57','E58','A62'];
for (var i in checklist){
var range = sheet.getRange(data_pos[i]);
range.setValue(checklist[i]);
if (i == checklist.length-1){
createPDF(tmpDoc);
}
}
}
function createDoc(checklist) {
var spreadsheet = SpreadsheetApp.openById('redacted');
var tmpDoc = spreadsheet.copy('Temp Copy of spreadsheet');
var sheet = tmpDoc.getSheetByName('Template');
writeData(tmpDoc, sheet, checklist);
}
I would suggest trying the following code to see if it fixes your remaining problem. This piece of code also should shorten your processing time some by eliminating repeated getRange() calls, which get expensive (from a time aspect) in Spreadsheet service calls. Let me know if it still doesn't work and we can see about making further changes.
var FILE_NAME = 'Initial Checklist Export';
function email(pdf){
var emailObj = {
to: Session.getActiveUser().getEmail(),
subject: FILE_NAME,
htmlBody: pdf.getUrl(),
noReply: true
};
MailApp.sendEmail(emailObj);
}
function createPDF(tmpDoc) {
var blob = tmpDoc.getAs(MimeType.PDF);
var pdfFile = DriveApp.createFile(blob);
pdfFile.setName(FILE_NAME);
email(pdfFile);
}
function createDoc(checklist) {
var spreadsheet = SpreadsheetApp.openById('redacted');
var tmpDoc = spreadsheet.copy('Temp Copy of spreadsheet');
var sheet = tmpDoc.getSheetByName('Template');
var data_pos = ['B8','B10','B9','B11','E8','E9','E10','E11','B7','E7','B15',
'B16','B17','B18','B20','B21','B23','B24','B26','B27',
'B29','B31','B32','B33','B34','B35','B36','B38','B39','B40',
'B41','B43','E14','E15','E16','E17','E18','E19','E20','E21',
'E22','E23','E25','E26','E27','E28','E29','E30','E32','E33',
'E34','E35','E36','E37','E38','E39','E40','E41','E42','E43',
'E45','E46','E47','E48','E49','E50','E51','E52','E53','E54',
'E55','E56','E57','E58','A62'];
var ranges = sheet.getRangeList(data_pos).getRanges();
for (var i in checklist){
ranges[i].setValue(checklist[i]);
}
SpreadsheetApp.flush();
createPDF(tmpDoc);
}