Skip to content

Instantly share code, notes, and snippets.

Created November 29, 2012 14:57
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save anonymous/4169590 to your computer and use it in GitHub Desktop.
Save anonymous/4169590 to your computer and use it in GitHub Desktop.
Get Google Spreadsheet as PDF with customizations
function spreadsheetToPDF(key) {
var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets");
var scope = "https://spreadsheets.google.com/feeds"
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
var requestData = {
"oAuthServiceName": "spreadsheets",
"oAuthUseToken": "always",
};
var name = DocsList.getFileById(key).getName()+".pdf";
var pdf = UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+key+"&exportFormat=pdf&gid=1&gridlines=0&printtitle=0&size=7&fzr=true&portrait=1&fitw=1", requestData).getBlob().setName(name);
return pdf;
}
@mikeydee77
Copy link

I had been looking for a solution to this problem for a while. Thanks this worked for me.

@brashScript
Copy link

Hi thanks, this looks like a really useful code. When I try to run this code, it requires some kind of authorization. What do I have to do for that?

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