Google脚本-创建PDF无法反映文档中的更改 [英] Google Script - Creating PDF doesn't reflect changes in Document

查看:70
本文介绍了Google脚本-创建PDF无法反映文档中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在电子表格中有一个脚本,用于接收来自表单的响应.使用这些响应和模板Google文档,我从模板创建了一个新文档,然后将占位符(例如'## CLIENT ##')替换为适当的数据.效果很好.然后,我尝试在同一文件夹中创建PDF副本.问题是PDF是原始模板,没有替换占位符... 我尝试添加一个中断以使文档更新为:Utilities.sleep(15000);这无济于事.我想我不理解如何在我的函数之间传递文档?

I have a script in a spreadsheet that receives responses from a Form. Using these responses and a template Google document, I create a new Document from the template and then replace placeholders (e.g. '##CLIENT##') with the appropriate data. Works great. I then try to create a PDF copy in the same folder. The problem is that the PDF is of the original template without the placeholders replaced... I tried adding a break to let the document update with: Utilities.sleep(15000); This doesn't help. I guess I am not understanding how the document is being passed between my functions?

全局变量

var SOURCE_TEMPLATE = "{Document ID Here}";
var TARGET_FOLDER = "{Folder ID Here}";

代码:

//Get headers from spreadsheet and create a sorted array
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];

// The variable e holds all the form values in an array.
// Assign values for each submission

if (e && e.namedValues){
var array_fields =[];

var project_manager = e.namedValues[headers[1]].toString();
var client = e.namedValues[headers[2]].toString();
var client_number = e.namedValues[headers[3]].toString();
var project_title = e.namedValues[headers[4]].toString();
var project_number = e.namedValues[headers[5]].toString();
var project_engineer = e.namedValues[headers[6]].toString();
var job_type = e.namedValues[headers[7]].toString();
var completion_date = e.namedValues[headers[14]].toString();
var project_cost = e.namedValues[headers[15]].toString();
var time = get_time();

array_fields.push(project_manager,client,client_number,project_title,pad(project_number),project_engineer,job_type,completion_date,'$'+ add_commas(project_cost),time);

var key_array  = ['##PROJECTMANAGER##','##CLIENT##','##CLIENTNUMBER##','##PROJECTTITLE##','##PROJECTNUMBER##','##PROJECTENGINEER##','##JOBTYPE##','##COMPLETIONDATE##','##PROJECTCOST##','##DATE##'];

var target = createDuplicateDocument(SOURCE_TEMPLATE, client_number + "-" + pad(project_number) +  " " + project_title + " Order Entry");

Logger.log("Created new document:" + target.getId());

for(var i=0; i<array_fields.length; i++) {
  var key = key_array[i]
  var value = array_fields[i]
  replaceParagraph(target, key, value);
}
Utilities.sleep(15000); 
savePDF(target); 

}

}

savePDf函数

function savePDF(doc) {

var docblob = doc.getAs('application/pdf');
var folder =  DocsList.getFolderById(TARGET_FOLDER);
docblob.setName(doc.getName())
folder.createFile(docblob);

}

因此,savePDF函数应创建已填充文档的PDF副本,但应在模板文档或新文档被填充之前为我提供PDF. 日志显示了来自target.getID()的正确文档ID,因此我不确定为什么target没有正确传递给savePDF.我觉得这是一个小错误,但我无法确定问题所在.

So the savePDF function should create a PDF copy of the filled document but instead gives me a PDF of the template document, or the new document before it is filled it. The Logs show the correct document ID from target.getID() So I'm not sure why target isn't passed to savePDF properly. I feel like it's some small mistake but I can't determine what the issue is.

推荐答案

您需要使用 saveAndClose()在转换文档之前先对其进行更新.

You need to use saveAndClose() to update the document before converting it.

在您的代码中:

function savePDF(doc) {
  doc.saveAndClose();
  var docblob = doc.getAs('application/pdf');
  var folder =  DocsList.getFolderById(TARGET_FOLDER);
  docblob.setName(doc.getName())
  folder.createFile(docblob);
}

您不需要睡眠延迟,可以将其删除,除非您想等待15秒;-)

You don't need the sleep delay, you can remove it unless you want to wait for 15 seconds ;-)

这篇关于Google脚本-创建PDF无法反映文档中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆