RossV
RossV

Reputation: 195

Google Script - Creating PDF doesn't reflect changes in Document

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?

Global Variables

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

Code:

//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

function savePDF(doc) {

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

}

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.

Upvotes: 0

Views: 279

Answers (1)

Serge insas
Serge insas

Reputation: 46794

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

In your code :

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

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

Upvotes: 3

Related Questions