Email a Custom Sheets as PDF in Attachment


Script export and send email of custom sheet on PDF in Attachment for Google Sheets with GAS (Google Apps Script) & Javascript Language, don't forget to change the variables according to your goal, read tips and watch video to understand more about how to run it even you are not a programmer



How to use it 




  • Sheets you want to print :

you can add more inside the array

var wantedSheets=[' Sheet1','Sheet2','Sheet3'.....];



  • Print Options :



  • Email parameters :

Seperate each email with comma :

MailApp.sendEmail('pace3man@gmail.com,medrx82@gmail.com', 'Title', 'Body',...




The Code

Please Comment and help us with share !

Comments

  1. Anonymous2/28/2019

    how can i send Pivot table on mail body and sheet-1 as attachment?




    ReplyDelete
    Replies
    1. The Above Question Is same here....
      Have you got an answer from anyone????

      Delete
  2. Great Code! Super easy to follow!

    ReplyDelete
  3. Was the best code.

    ReplyDelete
  4. ADEL! You the MAN! Thank you, works perfect. A few question... how can you set page margins and page height/width?

    ReplyDelete
  5. Hi, I think this solution is a little simpler: (But thank you for your kode)

    //Get Active Spreadsheet
    var spreadSheet=SpreadsheetApp.getActiveSpreadsheet();

    //Get Sheet to print of the spreadsheets
    var sheets=spreadSheet.getSheets();
    var mySheet = spreadSheet.getSheetByName("PrintSheet");
    var sheetID = mySheet.getSheetId();

    //Export URL with Parameters
    var spreadSheetId = spreadSheet.getId();
    var URL = "https://docs.google.com/spreadsheets/d/"+spreadSheetId+"/export"+
    "?format=pdf&"+
    "size=7&"+
    "fzr=false&"+
    "portrait=true&"+
    "fitw=true&"+
    "gridlines=false&"+
    "printtitle=false&"+
    "sheetnames=false&"+
    "pagenum=UNDEFINED&"+
    "attachment=true&"+
    "gid="+sheetID;
    //the HTTP method for the request: get and headers : authorization : Bearer tokens to access OAuth 2.0-protected resources
    var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

    ReplyDelete
  6. Anonymous12/11/2019

    its a great code!

    ReplyDelete
  7. Can anyone help, I'm fairly new to script but have fumble my way this far.

    I have a spreadsheet that has 9 pages as standard, occasionally an additional 3-4 get inserted depending on the use of the spreadsheet.

    I need to be able to create a PDF of all pages except page 1, and 2x hidden pages (which contain reference tables for vlookups).
    The PDF format needs to be specific for the data to sit on the page properly.

    I don't need to email it but instead just save it to another drive folder.

    Thanks for any help in advance. 👍

    ReplyDelete
  8. I want to email specific range.

    ReplyDelete
  9. me also want to send specific range by range name option or by writing specific range

    ReplyDelete
  10. Anonymous6/08/2022

    Hey Can i export a particular range of cells the same way? If yes what code do i have to add and where please??

    ReplyDelete
  11. Anonymous8/12/2022

    i want to change page setting to legal how can i do that in thisapp script

    ReplyDelete
  12. Anonymous10/30/2022

    can i use it for commercial purpose

    ReplyDelete

Post a Comment

Popular posts from this blog

Export and Email Specific Range in Google Sheets in CSV,PDF or XLSX