编辑:我已经确认订单运行正常-但它仍在通过电子邮件发送空模板,而不是书面数据。
简而言之,这就是我要实现的目标:
createDoc
我的所有操作都正确且正确地执行(通过console.log输出确认),但是,它通过电子邮件发送空的模板PDF,而不是通过电子邮件发送包含数据的PDF。这是怎么回事
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);
}
答案 0 :(得分:1)
我建议尝试以下代码,看看它是否可以解决您的剩余问题。这段代码还可以消除重复的getRange()调用,从而缩短您的处理时间,而在电子表格服务调用中,重复的getRange()调用(从时间方面而言)会变得昂贵。让我知道它是否仍然无法正常工作,我们可以进行进一步的更改。
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);
}