通过Apps Script从Google Doc模板创建的PDF具有旧值

时间:2018-08-20 18:34:38

标签: google-apps-script google-docs

基本上,这是正在发生的事情。用户提交Google表单(此后将触发将其设置为关闭状态),然后通过电子邮件将模板收据发送给用户。我目前遇到的问题是复制的模板将更新值,但是 pdf没有更新的值

很抱歉造成混乱,计划在以后创建数组。

// to do : refactor or w/e 
// make it so it emails the person a pdf of the doc
var docTemplateId = 'templateID';
var headerPosition = 'A1:E1';
var dataRange = 'A2:E2'; // 
var sheetId = 'dataSoureID';
var headers = Sheets.Spreadsheets.Values.get(sheetId, headerPosition);
var rowData = Sheets.Spreadsheets.Values.get(sheetId, dataRange);

var email = rowData.values[0][0];
var name = rowData.values[0][1];
var grade = rowData.values[0][2];
var subject = rowData.values[0][3];
var initalMeeting = rowData.values[0][4];
var techCurrentlyUsed = rowData.values[0][5];
var documentName = 'Good Teacher Planning '+ name;
var documentId = DriveApp.getFileById(docTemplateId).makeCopy().getId();
DriveApp.getFileById(documentId).setName(documentName);
var body = DocumentApp.openById(documentId).getBody();

// to do write get email function
function writeToTemplate() 
{ 
    body.replaceText('##email##', email);
    body.replaceText('##name##', name);
    body.replaceText('##grade##', grade);
    body.replaceText('##subject##', subject);
    body.replaceText('##initalMeeting##', initalMeeting); 
    body.replaceText('##techCurrentlyUsed##', techCurrentlyUsed);
}

function emailPDF()
{
  var file = DriveApp.getFileById(documentId);
  var blob = file.getBlob().getAs('application/pdf').setName(documentName);
  MailApp.sendEmail(email, 'hello', 'world', {attachments: blob});
  Logger.log('end email')
}

function main()
{
  writeToTemplate();
  emailPDF();
}

编辑:已用saveAndClose();

修复

1 个答案:

答案 0 :(得分:0)

您的Blob源高级参数必须为附件数组:

attachments: [blob1,blob2,blob3]

即使只有一个附件,您仍然需要使用数组

attachments: [blob]