Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

templates - Created PDF does not reflect changes made to the original document

I have a Google Docs template which is automatically copied into a folder and replace specific values with values coming from spreadsheet cells.

The template contains values such as <<41>> which are used to be "find-and-replaced" with values coming from a spreadsheet.

template table

The find-and-replace process is fine, and the results of the document looks like this image below

enter image description here

Now, I want this document to be converted into PDF after it has been updated, so I made a function for the convertion and call it at the bottom after all the codes has been executed.

Here's the function:

//convert to PDF
function convertPDF(FileID,newName) {
  Utilities.sleep(120000);
  docblob = DocumentApp.openById(FileID).getAs('application/pdf');
  /* Add the PDF extension */
  docblob.setName(newName + ".pdf");
  var file = DriveApp.createFile(docblob);
}

The convertion works fine, but the converted document isn't updated. Rather, it is like it was the one freshly copied from the template, before the values were changed.

If you may notice, I have added a "sleep" timer before in the conversion function so as to delay the conversion and give time for the changes to be saved, I've tried 1 and 2 minutes sleep but still it doesn't work.

What can I do to make sure the PDF is created from the updated template?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The function I provided above works fine, we just need to forced the script to save the changes by calling the saveAndClose() method before calling the function that converts the doc into PDF.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...