Email a Custom Sheets as PDF in Attachment
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://gist.github.com/chemiadel/bee4cb03481b580275d5296a3e7adc0d | |
function EmailPDF() { | |
//Get Active Spreadsheet | |
var spreadSheet=SpreadsheetApp.getActiveSpreadsheet(); | |
//Get All Sheets of the spreadsheets | |
var sheets=spreadSheet.getSheets(); | |
//Sheet wanted to export on PDF | |
var wantedSheets=['Sheet1','Sheet2']; | |
//Save defaults hidden sheets | |
var arr=new Array(); | |
for(var i in sheets){ | |
if(sheets[i].isSheetHidden()){ | |
arr.push(sheets[i].getSheetName()); | |
} | |
} | |
//Hide All sheets except one from wantedsheets you can't hide all sheets | |
for(var j in sheets){ | |
if(!sheets[j].isSheetHidden() && sheets[j].getSheetName()!=wantedSheets[0]){ | |
sheets[j].hideSheet(); | |
} | |
} | |
//Show Wanted sheets | |
for(var k in wantedSheets){ | |
spreadSheet.getSheetByName(wantedSheets[k]).showSheet(); | |
} | |
//Export URL with Parameters | |
var spreadSheetId = spreadSheet.getId(); | |
var URL = "https://docs.google.com/spreadsheets/d/"+spreadSheetId+"/export"+ | |
"?format=pdf&"+ | |
"size=0&"+ | |
"fzr=true&"+ | |
"portrait=true&"+ | |
"fitw=true&"+ | |
"gridlines=true&"+ | |
"printtitle=true&"+ | |
"sheetnames=true&"+ | |
"pagenum=CENTER&"+ | |
"attachment=true"; | |
//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()}}; | |
//Return the data inside this object as a blob. | |
var response=UrlFetchApp.fetch(URL,params).getBlob(); | |
//Email it | |
MailApp.sendEmail('To', 'Title', 'Body', { | |
attachments: [{ | |
fileName: "FileTitle" + ".pdf", | |
content: response.getBytes(), | |
mimeType: "application/pdf" | |
}] | |
}); | |
//Reshow all sheets | |
for(var x in sheets){ | |
if(sheets[x].isSheetHidden()){ | |
sheets[x].showSheet(); | |
} | |
} | |
//Rehide default sheets | |
for(var y in arr){ | |
spreadSheet.getSheetByName(arr[y]).hideSheet(); | |
} | |
} |
how can i send Pivot table on mail body and sheet-1 as attachment?
ReplyDeleteThe Above Question Is same here....
DeleteHave you got an answer from anyone????
Great Code! Super easy to follow!
ReplyDeleteWas the best code.
ReplyDeleteADEL! You the MAN! Thank you, works perfect. A few question... how can you set page margins and page height/width?
ReplyDeleteHi, I think this solution is a little simpler: (But thank you for your kode)
ReplyDelete//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()}};
its a great code!
ReplyDeleteCan anyone help, I'm fairly new to script but have fumble my way this far.
ReplyDeleteI 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. 👍
I want to email specific range.
ReplyDeleteme also want to send specific range by range name option or by writing specific range
ReplyDeleteHey Can i export a particular range of cells the same way? If yes what code do i have to add and where please??
ReplyDeletei want to change page setting to legal how can i do that in thisapp script
ReplyDeletecan i use it for commercial purpose
ReplyDeleteyes
Delete