Skip to content

Instantly share code, notes, and snippets.

Last active July 27, 2023 07:14
Star You must be signed in to star a gist
What would you like to do?
Example on how to export a Google sheet to various formats, includes most PDF options
function exportSpreadsheet() {
//All requests must include id in the path and a format parameter
//format=xlsx //excel
//format=ods //Open Document Spreadsheet
//format=zip //html zipped
//CSV,TSV OPTIONS***********
//format=csv // comma seperated values
// tsv // tab seperated values
//gid=sheetId // the sheetID you want to export, The first sheet will be 0. others will have a uniqe ID
// PDF OPTIONS****************
//size=0,1,2..10 paper size. 0=letter, 1=tabloid, 2=Legal, 3=statement, 4=executive, 5=folio, 6=A3, 7=A4, 8=A5, 9=B4, 10=B5
//fzr=true/false repeat row headers
//portrait=true/false false = landscape
//fitw=true/false fit window or actual size
//pagenum=CENTER/UNDEFINED CENTER = show page numbers / UNDEFINED = do not show
//attachment = true/false dunno? Leave this as true
//gid=sheetId Sheet Id if you want a specific sheet. The first sheet will be 0. others will have a uniqe ID.
// Leave this off for all sheets.
//need all the below to export a range
//gid=sheetId must be included. The first sheet will be 0. others will have a uniqe ID
//ir=false seems to be always false
//ic=false same as ir
//r1=Start Row number - 1 row 1 would be 0 , row 15 wold be 14
//c1=Start Column number - 1 column 1 would be 0, column 8 would be 7
//r2=End Row number
//c2=End Column number
var ssID = "12g8-tcRwFkcL7El...XdQAzSR7v8-geIR6r-IY";
var url = ""+ssID+"/export"+
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url, params).getBlob();
// save to drive
//or send as email
MailApp.sendEmail(email, subject, body, {
attachments: [{
fileName: "TPS REPORT" + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
Copy link

Can anyone help point out where I can manage the PDF file name? Ideally, I'd like to incorporate the value of a cell into the file name. Don't seem to be able to find where through some trial and error. This may not be the right place. Any help is appreciated!

Copy link

basetoad commented May 2, 2023

I have not found one. In case it helps, it seems as though the filename is a combination of Spreadsheet (Book) name, and Sheet name that is bring printed so conceptually "Spreadsheet - Sheet.pdf"

Copy link

techdoneforyou commented May 3, 2023 via email

Copy link

@techdoneforyou - Thank you very much! That's what I needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment