I’m trying to create a process to produce invoices. The invoice data is in a spreadsheet and the code takes data from that spreadsheet, makes a copy of the master invoice (a template spreadsheet with some data and graphics), adds the relevant data into the copy of the master and saves it as a google sheet. All this works correctly.
The problem comes when I try and create a PDF version of that spreadsheet – what I get is a blank copy of the spreadsheet (i.e. it has the correct name, has all the information and graphics the master spreadsheet has, but none of the values I’ve added via the setValue statements.)
This is the relevant part of the code
var newID = DriveApp.getFileById(masterInvoiceID).makeCopy().setName(saveName).getId(); var newInvoice = SpreadsheetApp.openById(newID);
newInvoice.getRange(‘J7’).setValue(formattedDate); newInvoice.getRange("J9").setValue(invNumber);
newInvoice.getRange(‘D15’).setValue(recName); newInvoice.getRange('D17').setValue(address);
newInvoice.getRange(‘D19’).setValue(companyName);
SpreadsheetApp.flush Utilities.sleep(500); var outputFolder = DriveApp.getFolderById(outputFolderID); var theBlob = newInvoice.getBlob();
outputFolder.createFile(theBlob).setName(saveName);
I’ve tried various permutations of the newInvoice.getBlob() such as newInvoice.getAs(‘application/pdf’)
I’ve added the flush command and the sleep command in case the code is getting to the getBlob before the file is written but nothing I try is working.
I converted an old (fully populated) excel invoice to google sheets and ran just the last 2 lines of the code above against it and it produced a fully populated PDF version, so I think the last 2 lines should do the job, but I’ve got no idea why it doesn’t work in the code above.
Paul James is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.