Skip to content Skip to sidebar Skip to footer

Generate Pdf Of Only One Sheet Of My Spreadsheet

I need a script that creates a PDF with only one sheet of my spreadsheet. I currently have a script that generates a PDF but does it with the entire file. I can not copy the values

Solution 1:

This question has been addressed more or less here.

In short, you can modify export parameters in Url queryString to get a specific sheet. And then convert the blob to a pdf.

Here is the code for the same, base code was obtained from here

function emailSpreadsheetAsPDF() {

  // Send the PDF of the spreadsheet to this email addressvaremail="Your Email Id"; 

  // Get the currently active spreadsheet URL (link)// Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");varss= SpreadsheetApp.getActiveSpreadsheet();
  varsheet= ss.getSheetByName("<Your Sheet name>")

  // Subject of email messagevarsubject="PDF generated from sheet " + sheet.getName(); 

  // Email Body can  be HTML too with your logo image - see ctrlq.org/html-mailvarbody="Install the <a href='http://www.labnol.org/email-sheet'>Email Spreadsheet add-on</a> for one-click conversion.";

  // Base URLvarurl="https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());

  /* Specify PDF export parameters
  From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
  */varurl_ext='exportFormat=pdf&format=pdf'// export as pdf / csv / xls / xlsx
  + '&size=letter'// paper size legal / letter / A4
  + '&portrait=false'// orientation, false for landscape
  + '&fitw=true&source=labnol'// fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false'// hide optional headers and footers
  + '&pagenumbers=false&gridlines=false'// hide page numbers and gridlines
  + '&fzr=false'// do not repeat row headers (frozen rows) on each page
  + '&gid=';                             // the sheet's Idvartoken= ScriptApp.getOAuthToken();


  //make an empty array to hold your fetched blobs  var blobs;


    // Convert your specific sheet to blobvarresponse= UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

    //convert the response to a blob and store in our array
    blobs = response.getBlob().setName(sheet.getName() + '.pdf');


  // Define the scope
  Logger.log("Storage Space used: " + DriveApp.getStorageUsed());

  // If allowed to send emails, send the email with the PDF attachmentif (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments:[blobs]     
    });  
}

Edit: To combine specific sheets and convert it into and pdf.

Firstly you will have to obtain the sheet object of all the sheets you want to merge into the pdf

var sheet2 = ss.getSheetByName("<Your Sheet name>")
var sheet3 = ss.getSheetByName("<Your Sheet name>")

Then pass the sheetId of each of the sheet to the URL query delimited by %EE%B8%80, Like so

   url += url_ext + sheet.getSheetId()+ "%EE%B8%80"+sheet2.getSheetId()+ "%EE%B8%80"+sheet3.getSheetId()
   var response = UrlFetchApp.fetch(url, {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

Post a Comment for "Generate Pdf Of Only One Sheet Of My Spreadsheet"