Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Apps Script to save a single sheet from a spreadsheet as pdf in a specific folder

I am using a Google spreadsheet to prepare invoices and was looking for a simple script that saves a sheet, where the invoice is in, in a "invoices" folder to build an archive. I "borrowed" code from numerous contributors on Stackoverflow and youtube and came up with a code that works. I had to copy the invoice to a newly created spreadsheet, because it seems to be impossible to create a pdf from one single sheet in type spreadsheet. I also had to use a piece of code to move the pdf from the root to an "invoices" folder The only thing I am not able to solve is that the spreadsheet created in line 6 consistes of 2 sheets. An empty one and a correctly copied one. The created pdf thus alsa has 2 sheets, one empty and one correct sheet. Anyone got a clue how to solve this ? By the way sometimes it takes some minutes before the pdf shows in the folders.

Below is the code

function generatePdf(){
    //Create a temporary spreadsheet, to store the desired sheet from the spreadsheet in. 
    var originalSpreadsheet = SpreadsheetApp.getActive();
    originalSpreadsheet.setActiveSheet(originalSpreadsheet.getSheets()[4]);
    var name = "Testname"
    var newSpreadsheet = SpreadsheetApp.create(name);
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    sheet = originalSpreadsheet.getActiveSheet();
    sheet.copyTo(newSpreadsheet);
    //Save the desired sheet as pdf
    var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf');
    var saveCopy = DriveApp.createFile(pdf);
    //Delete temporary spreadsheet
    DriveApp.getFilesByName(name).next().setTrashed(true);
    //Move the pdf file from the rootfolder to the folder where invoices are to be stored.
    var files = DriveApp.getRootFolder().getFiles();
    while (files.hasNext()) {
        var file = files.next();
        var destination = DriveApp.getFolderById("0B3ok04PZOVbgLXA2dy14MVlLRXM");
        destination.addFile(file);
        var pull = DriveApp.getRootFolder();
        pull.removeFile(file);    
    }
}
like image 465
John Avatar asked Sep 25 '16 18:09

John


People also ask

How do I save a selected area as a PDF in Google Sheets?

If you want to export a selection, click and drag to select a range of cells. Select Download > PDF document from the File menu in the Google Sheet.

Can you save a Google Sheet as a PDF?

To download the Google Sheet as a PDF document, click “File” in the top-left corner, then hover over “Download” and select the “PDF document” option. Click on “File” in the top-left corner, then hover over “Download” and select “PDF document” from the list. On the next page, there are a number of configuration options.


1 Answers

I'm not sure if you have encountered this code but you can try this:

function checkSheet() {
var sheetName = "Sheet1";
var folderID = "FOLDER_ID"; // Folder id to save in a folder.
var pdfName = "Invoice "+Date();

var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);

//Copy whole spreadsheet
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder))

//delete redundant sheets
var sheets = destSpreadsheet.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName){
destSpreadsheet.deleteSheet(sheets[i]);
}
}

var destSheet = destSpreadsheet.getSheets()[0];
//repace cell values with text (to avoid broken references) 
var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);

//save to pdf
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var newFile = folder.createFile(theBlob);

//Delete the temporary sheet
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
}

Note: While testing this code, it creates the pdf in an instant, but it may depend on the invoice template.

References:

  • Simple Google Apps Script to export a single sheet to PDF and email it to a contact list
  • Sample Template - Professional Invoice Template (for Testing Purpose)

Hope this helps!

UPDATE

enter image description here enter image description here

like image 181
Mr.Rebot Avatar answered Nov 24 '22 05:11

Mr.Rebot