0

I'm trying to save a single-sheet spreadsheet to pdf. Many blogs explain how to do this using getBlob() so I ended up with this:

var theBlob = jobSpreadSheet.getBlob().getAs('application/pdf');
var newFile = getPdfFolder().createFile(theBlob.setName(fileName + ".pdf"));

To create jobSpreadSheet I have created a new spreadsheet:

var tempSpreadsheet = SpreadsheetApp.create(name);
var driveTempFile = DriveApp.getFileById(tempSpreadsheet.getId());
var jobsFolder = getJobsFolder();
var driveNewFile = driveTempFile.makeCopy(name, jobsFolder);
var jobSpreadsheet = SpreadsheetApp.open(driveNewFile);

Next I copy a template into the new spreadsheet and remove the empty first sheet:

var jobCard = jobCardTemplate.copyTo(jobSpreadSheet).setName('Job Card');
jobSpreadSheet.deleteSheet(jobSpreadSheet.getSheets()[0]);

Then I update a few cells in the jobCard and finally create the pdf. All the steps work except creating the pdf. The pdf is created, it contains the template, but not the updated values. Should I make the create pdf step somehow wait for the updates to be saved?

  • Looking over your code again there seems to be a few oddities. Would you mind posting your full code so I can see how it flows? (Can remove any specific information like spreadsheet IDs.) – New_2_Code Nov 27 '18 at 09:40

1 Answers1

0

Are you calling .saveChanges() anywhere in your script? (Before getting the blob.) Not sure if this is required with sheets, but I had a similar issue in docs because I was not saving changes before getting the blob.

Edit:

Just tested with the following code and it appears that .saveChanges is not required:

function test() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");

sheet.getRange(1, 1, 1, 1).setValue("Test");

var file = DriveApp.getFileById(ss.getId());
var blob = file.getBlob().getAs('application/pdf');

DriveApp.createFile(blob);


}
New_2_Code
  • 330
  • 2
  • 18