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. 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

Post a Comment