Skip to content

Instantly share code, notes, and snippets.

Embed
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
//https://docs.google.com/spreadsheets/d/{SpreadsheetId}/export
//FORMATS WITH NO ADDITIONAL OPTIONS
//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****************
//format=pdf
//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
//gridlines=true/false
//printtitle=true/false
//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.
// EXPORT RANGE OPTIONS FOR PDF
//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 = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
"?format=pdf&"+
"size=0&"+
"fzr=true&"+
"portrait=false&"+
"fitw=true&"+
"gridlines=false&"+
"printtitle=true&"+
"sheetnames=true&"+
"pagenum=CENTER&"+
"attachment=true";
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url, params).getBlob();
// save to drive
DriveApp.createFile(response);
//or send as email
/*
MailApp.sendEmail(email, subject, body, {
attachments: [{
fileName: "TPS REPORT" + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
}]
});};
*/
}
@anshu1987

This comment has been minimized.

Copy link

@anshu1987 anshu1987 commented May 19, 2017

Hi ,
The above code will download the file in google drive only , is there any way i can download file to my local computer drive.

Thanks
Anshu

@jaeyongjaykim

This comment has been minimized.

Copy link

@jaeyongjaykim jaeyongjaykim commented Sep 26, 2017

Thank you for the working code.
Is there a way to change name of the exported file?

Or, is it possible to grab the original file name (spreadsheet name) before exporting?
Thank you.

@adambrother

This comment has been minimized.

Copy link

@adambrother adambrother commented Mar 15, 2018

Hi,

this script is fantastic!

I'm beginner to scripting and trying to modify it in order to export a specific Named Range.

But don't know how to do that.

Do you have a variant of your script doing this?

Thanks in advance

@cwlind

This comment has been minimized.

Copy link

@cwlind cwlind commented Jun 18, 2018

Still works great after a few years of use. After some brute-force trial and error, I found another pdf export option. "Printnotes" is true by default, so if you don't want to export the notes embedded in a sheet, you can add the option: &printnotes=false

@KKellyC

This comment has been minimized.

Copy link

@KKellyC KKellyC commented Jun 20, 2018

Another option for &range, found it supports named ranges!

&gid=sheetId&range=NamedRange using your actual named range

@MrLando1

This comment has been minimized.

Copy link

@MrLando1 MrLando1 commented Jun 29, 2018

Hi, apologies in advance but I'm a noob to scripting...

Great bit of code.... I can get it to work and have started tweaking to my requirements..

How do:
a). Save the pdf with a specific name (rather than export.pdf which is what currently occurs?
b). Save to a specific google drive folder?

thanks in advance

UPDATE:
Found out how to set the name... amend the line:

var response = UrlFetchApp.fetch(url, params).getBlob()

...by appending this to the end: .setname("myName")

e,g, var response = UrlFetchApp.fetch(url, params).getBlob().setName("myName")

@JohnSpilker

This comment has been minimized.

Copy link

@JohnSpilker JohnSpilker commented Jul 9, 2018

Thank you. This is fantastic.

@RenaudHoule

This comment has been minimized.

Copy link

@RenaudHoule RenaudHoule commented Aug 8, 2018

You can also set the margins using

&top_margin=0.3&left_margin=0.3&right_margin=0.3&bottom_margin=0.3

you need to put them all four in order to works!

@v-python

This comment has been minimized.

Copy link

@v-python v-python commented Aug 13, 2018

I'm confused about the environment this script is intended to be used in: Does it run on the web and export to Google Drive? Or does it run on a local browser, and download to the computer and upload back to Google Drive (double network traffic)?

What is the ScriptApp.getOauthToken call? Does the script have to be authorized somehow?

Where would I put this script to use it? In the spreadsheet, or separate from the spreadsheet? If in the spreadsheet, wouldn't there be a way to get the ssID without hard-coding it? If separate from the spreadsheet, where? And do you need a separate copy of the script for each spreadsheet you want to export, because of the hard-coded ssID?

@nadampski

This comment has been minimized.

Copy link

@nadampski nadampski commented Oct 11, 2018

MrLando1 - you can specify the output folder like this:
// save to drive
var dir = DriveApp.getFolderById("your_id_here");
var file = dir.createFile(response);

@Caltor

This comment has been minimized.

Copy link

@Caltor Caltor commented Jan 2, 2019

Is there a query parameter to NOT print Notes?

@rmOrtiz

This comment has been minimized.

Copy link

@rmOrtiz rmOrtiz commented Jan 17, 2019

Is there a query parameter to NOT print Notes?

There is a comment above from 'cwlind'. says that &printnotes=false would help since is true by deafault. I hope that helps.

@ricl0250

This comment has been minimized.

Copy link

@ricl0250 ricl0250 commented Jan 29, 2019

is there a query parameter to centre horizontally the printed columns?

@ricl0250

This comment has been minimized.

Copy link

@ricl0250 ricl0250 commented Jan 29, 2019

found it
&horizontal_alignment=CENTER // Alignment - horizontal: LEFT/CENTER/RIGHT
&vertical_alignment=TOP // Alignment - vertical: TOP/MIDDLE/BOTTOM

@klexl

This comment has been minimized.

Copy link

@klexl klexl commented Mar 1, 2019

hi, does anybody know if there is a query parameter to include the settings of the custom page breaks to the url?

@Gillinghammer

This comment has been minimized.

Copy link

@Gillinghammer Gillinghammer commented Mar 13, 2019

hi, does anybody know if there is a query parameter to include the settings of the custom page breaks to the url?

This page has been extremely helpful! Agreed having the ability to change the custom page breaks is the only thing missing. Anyone know?

@brianscodery

This comment has been minimized.

Copy link

@brianscodery brianscodery commented Mar 29, 2019

Great summary, thanks!

One comment, though, I'm pretty sure line 21 should be:

//fitw=true/false fit width or actual size

@Krivozhnya

This comment has been minimized.

Copy link

@Krivozhnya Krivozhnya commented Apr 6, 2019

This is great. Is there parameter for custom size, height and width?

@dkeys222

This comment has been minimized.

Copy link

@dkeys222 dkeys222 commented Jun 5, 2019

I see there is a way to Fit to width using ( fitw=true ) but is there a way to Fit to page meaning I need to Fit to Width and Height?

@contributorpw

This comment has been minimized.

Copy link

@contributorpw contributorpw commented Jun 6, 2019

@dkeys222,

It's depended of the current page orientation.

@jerezapo

This comment has been minimized.

Copy link

@jerezapo jerezapo commented Jun 22, 2019

I'm struggling with getting this to export only a single sheet. I can do it other ways, using https://spreadsheets.google.com/feeds/download/spreadsheets/Export?... Is there way to add the gid or sheetname?
Update:
got it! Need to add the gid as such: &gid=
example: export?format=pdf&gid=1234..

@rmOrtiz

This comment has been minimized.

Copy link

@rmOrtiz rmOrtiz commented Jun 24, 2019

@jerezapo you are right! that is the correct way to specify the sheet name.

Thanks for this awesome page! it is very helpful.

Now, I have a question, does anybody knows if is possible to print in pdf but with "custom headers"?

@contributorpw

This comment has been minimized.

Copy link

@contributorpw contributorpw commented Jun 24, 2019

@luciandornescu

This comment has been minimized.

Copy link

@luciandornescu luciandornescu commented Sep 2, 2019

Hello, Is there a way to export only 2 of 5 sheets in one pdf?

@coppit

This comment has been minimized.

Copy link

@coppit coppit commented Sep 11, 2019

@oshliaer,

I think the question is if one has a lot of rows, fit by width will split the rows across multiple pages. I want to fit by height as well so that everything gets narrower/smaller, and only uses one page. There's a "Fit to Height" option in the print dialog for this. Has anyone figured out the URL equivalent?

@coppit

This comment has been minimized.

Copy link

@coppit coppit commented Sep 11, 2019

Found it here: https://stackoverflow.com/a/46312255 ! scale=3 for height fit, scale=4 for page fit.

@contributorpw

This comment has been minimized.

Copy link

@contributorpw contributorpw commented Oct 1, 2019

@coppit, thank you!

@jeffkahsen

This comment has been minimized.

Copy link

@jeffkahsen jeffkahsen commented Dec 3, 2019

hi, does anybody know if there is a query parameter to include the settings of the custom page breaks to the url?

Would love to find an answer to this as well. I can't find a way to programmatically add page breaks on my export. Given they've recently made custom page breaks available in the UI, I figure it has to be available via scripts... Anyone able to figure that out yet?

@techdoneforyou

This comment has been minimized.

Copy link

@techdoneforyou techdoneforyou commented Feb 29, 2020

Hello, Is there a way to export only 2 of 5 sheets in one pdf?

The answer is not super obvious, because you don't do it using the gid, leave it out of the url altogether. Instead, in your script, prior to generating the url, .hideSheet for each sheet you don't want printed. Might want to .showSheet after printing if you want to see them again. The hidden sheets do NOT print.

@andrewroberts

This comment has been minimized.

Copy link

@andrewroberts andrewroberts commented Apr 7, 2020

I've added all the extra PDF settings in the comments in this updated gist .

@nussisworld

This comment has been minimized.

Copy link

@nussisworld nussisworld commented Apr 12, 2020

I wanted to give the exported file a filename which is defined in a cell on a specific sheet. The solution is:

`// Set the output filename
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheetname'); //replace Sheetname
var value = sheet.getRange('C1').getValue(); //cell
var pdfName = value;

DriveApp.createFile(response).setName(pdfName);`

@Buckwich

This comment has been minimized.

Copy link

@Buckwich Buckwich commented May 5, 2020

@Krivozhnya If you want to set a custom size you can set a custom width and height in inch separated by x (must be greater than 1.2):

size=4.5x2 instead of the predefined sizes

@samjco

This comment has been minimized.

Copy link

@samjco samjco commented May 17, 2020

@Spencer-Easton ... to make is more dynamic.....
Replace lines 38 thru 49:

  var ssID = "12g8-tcRwFkcL7El...XdQAzSR7v8-geIR6r-IY";
  var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                        "?format=pdf&"+
                                                        "size=0&"+
                                                        "fzr=true&"+
                                                        "portrait=false&"+
                                                        "fitw=true&"+
                                                        "gridlines=false&"+
                                                        "printtitle=true&"+
                                                        "sheetnames=true&"+
                                                        "pagenum=CENTER&"+
                                                        "attachment=true";

With

  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var ssID= ss.getId(); // Don't really need the Spreadsheet ID because we are recalling the url.
  var sID= ss.getSheetId();

//Using getUrl() so no need for spreadsheetid
  var url= ss.getUrl().replace(/edit$/, '') + 
"export?exportFormat=pdf&" +
"format=pdf&"+
"fmcmd=12&"+
"size=0&"+
"fzr=true&"+
"gid="+sID+"&"+
"sheetnames=true&"+
"pagenum=CENTER&"+
"gridlines=true&"+
"portrait=false&"+
"scale=2&"+
"fitw=true&"+
"horizontal_alignment=CENTER&"+
"top_margin=0.10&"+
"bottom_margin=0.10&"+
"left_margin=0.20&"+
"right_margin=0.20&"+
"attachment=true";

@avantiya

This comment has been minimized.

Copy link

@avantiya avantiya commented Jun 12, 2020

The custom page breaks ?

@hc974

This comment has been minimized.

Copy link

@hc974 hc974 commented Jul 7, 2020

Hey, thanks a lot for the code! It works perfectly in my private drive. But I'm having problems using it in team drive. I want to create a PDF from a spreadsheet stored in a team drive and save it to a specific folder in the same team drive.

Does anyone have an idea how to do it?

@andrewroberts

This comment has been minimized.

Copy link

@andrewroberts andrewroberts commented Sep 11, 2020

This worked until recently, but now it only works one time after I've made a code or GSheet change, but subsequent calls give me a server 500 error.

I've tried all combinations of the URL, down to absolute minimum and running the generated URL in the browser address bar when I get the same error.

@iAbdullah1995

This comment has been minimized.

Copy link

@iAbdullah1995 iAbdullah1995 commented Dec 5, 2020

This worked until recently, but now it only works one time after I've made a code or GSheet change, but subsequent calls give me a server 500 error.

I've tried all combinations of the URL, down to absolute minimum and running the generated URL in the browser address bar when I get the same error.

Hello, @andrewroberts
Same thing happen with me but I noticed a weird thing maybe will help us to fix this problem!
when I replace:
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

with:
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()},muteHttpExceptions: true};

the script is run BUT when I go to Google drive and try to open the file, I got warning from google said the file is not safe!

UPDATE:
Ok, I downloaded the file on my computer, It was ".html" file NOT ".pdf" !!
another thing, the content of file is:

Google Docs encountered an error. Please try reloading this page, or coming back to it in a few minutes.

To learn more about the Google Docs editors, please visit our help centre.

We're sorry for the inconvenience.

  • The Google Docs Team

Check this link please, maybe will help you:
https://gist.github.com/draqoon/9e5896f735be2cf9b3c17101ced8af8a

noitce: similar error happening with "MailApp.sendEmail".

@samjco

This comment has been minimized.

Copy link

@samjco samjco commented Dec 5, 2020

@jf-tellier

This comment has been minimized.

Copy link

@jf-tellier jf-tellier commented Jan 9, 2021

after a few hour i figure out how to use the PDF Range option, the gid MUST be the gid of the sheet URL (ex
: hxxps://docs.google.com/spreadsheets/d/1BSMr_FSUlII7S32aOWkri9gtFa8wncjt9_cRs7LzzPM/edit#gid=804538520 )

`
function generatePdf() {

//All requests must include id in the path and a format parameter
//https://docs.google.com/spreadsheets/d/{SpreadsheetId}/export

//FORMATS WITH NO ADDITIONAL OPTIONS
//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****************
//format=pdf
//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
//fzc=true/false repeat column headers
//portrait=true/false false = landscape
//fitw=true/false fit window or actual size
//gridlines=true/false
//printtitle=true/false
//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.
//printnotes=false Set to false if you don't want to export the notes embedded in a sheet
//top_margin=[number] Margins - you need to put all four in order fir it to works, and they have to be to
//left_margin=[number] 2DP. So 0.00 for zero margin.
//right_margin=[number]
//bottom_margin=[number]
//horizontal_alignment=CENTER Horizontal Alignment: LEFT/CENTER/RIGHT
//vertical_alignment=TOP Vertical Alignment: TOP/MIDDLE/BOTTOM
//scale=1/2/3/4 1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
//pageorder=1/2 1= Down, then over / 2= Over, then down
//sheetnames=true/false
//range=[NamedRange] Named ranges supported - see below

// EXPORT RANGE OPTIONS FOR PDF
//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 = "1BSMr_FSUlII7S34aOWkri8gtFa8wncjt9_cXs7LzzPM";
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
"?format=pdf&"+
"size=0&"+
"fzr=true&"+
"portrait=true&"+
"fitw=true&"+
"gridlines=false&"+
"printtitle=false&"+
"sheetnames=false&"+
"pagenum=UNDEFINED&"+
"gid=804528520&"+
"ir=false&"+
"ic=false&"+
"r1=0&"+
"c1=0&"+
"r2=44&"+
"c2=7&"+
"attachment=true";

var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

var response = UrlFetchApp.fetch(url, params).getBlob();
// save to drive
DriveApp.createFile(response);

//or send as email
/*
MailApp.sendEmail(email, subject, body, {
attachments: [{
fileName: "TPS REPORT" + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
}]
});};
*/

}
`

@stretchymorris

This comment has been minimized.

Copy link

@stretchymorris stretchymorris commented Jan 23, 2021

Hi everyone.

this has all been very helpful.

does anyone know if i can apply protection to the PDF (i.e. copy/edit protection) as you can in adobe??

any help would be greatly received.

@luc-ass

This comment has been minimized.

Copy link

@luc-ass luc-ass commented Mar 3, 2021

Has anyone found out how to enable Filename, Sheetname, Date and Time in the header/footer? I've not been able to find a file in the source code that shows the options...

@nussisworld

This comment has been minimized.

Copy link

@nussisworld nussisworld commented Mar 3, 2021

Has anyone found out how to enable Filename, Sheetname, Date and Time in the header/footer? I've not been able to find a file in the source code that shows the options...

I have created a header in my sheet to be printed. There you will find the "PDF NAME" field. To show the current date there I use "= today ()". So that the PDF gets this file name, I use the following code:

// Set the output filename var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName('Bestellformular'); var value = sheet.getRange('C1').getValue(); var pdfName = value;

@luc-ass

This comment has been minimized.

Copy link

@luc-ass luc-ass commented Mar 3, 2021

That's certainly one way to do it and pointed me to naming my file the right way. Perhaps to be a little more precise: I am willing to do some reverse engineering to find missing options to this api. The thing I am missing is a starting point. Any ideas which file to start with?

@nussisworld

This comment has been minimized.

Copy link

@nussisworld nussisworld commented Mar 3, 2021

That's certainly one way to do it and pointed me to naming my file the right way. Perhaps to be a little more precise: I am willing to do some reverse engineering to find missing options to this api. The thing I am missing is a starting point. Any ideas which file to start with?

Do you need a working demo script?

@luc-ass

This comment has been minimized.

Copy link

@luc-ass luc-ass commented Mar 3, 2021

I have a working demo, I am just trying to find more options like these

"fitw=true&"+
"gridlines=false&"+
"printtitle=false&"+
"sheetnames=false&"+
@itsazzad

This comment has been minimized.

Copy link

@itsazzad itsazzad commented Mar 5, 2021

What are the export range options for XLSX?
The PDF export range options do not seem to be working for XLSX.

@JerryGarcia

This comment has been minimized.

Copy link

@JerryGarcia JerryGarcia commented Apr 15, 2021

I'm just curious how you learned how to do this originally.

@lyehengfoo

This comment has been minimized.

Copy link

@lyehengfoo lyehengfoo commented Apr 16, 2021

This post/article and the additional alignment parameters in @samjco reply, has helped me a great deal to export a Google sheet into PDF. Thank you for this great sharing!

@kguiste95

This comment has been minimized.

Copy link

@kguiste95 kguiste95 commented Apr 16, 2021

How does one learn about these about the different elements and options within the export url?
For example, 'format=pdf'... Is there some documentation for learning the components of a export url?

@andrewroberts

This comment has been minimized.

Copy link

@andrewroberts andrewroberts commented Apr 16, 2021

@kguiste95

This comment has been minimized.

Copy link

@kguiste95 kguiste95 commented Apr 16, 2021

This is curious, I'm relatively new to programming, I'm focusing on the google services. Google has a vast amount of resources and documentation, however it's obviously not exhaustive.

Surely there must be some rationale around structuring the urls. They work because the systems have been developed for them to work right? So, how they work should be documented somewhere?

@mrddt-tech

This comment has been minimized.

Copy link

@mrddt-tech mrddt-tech commented May 2, 2021

Could anyone give me a link to full documentation of this google API (that exports Sheet as PDF)? Thank you!

@plajko

This comment has been minimized.

Copy link

@plajko plajko commented May 4, 2021

I used this endpoint for exporting sheets as pdf, but it stopped working for me with error:

BAD_REQUEST: 400. That’s an error. The server cannot process the request because it is malformed. It should not be retried. That’s all we know.

Any idea?

@jonahmolinski

This comment has been minimized.

Copy link

@jonahmolinski jonahmolinski commented May 4, 2021

@plajko Looks like there is now a redirect happening. Make sure your redirected URL request has the access token header and it will start working again.

@plajko

This comment has been minimized.

Copy link

@plajko plajko commented May 4, 2021

@jonahmolinski thanks, I saw that redirect when opened in browser, but with the access token in Authorization header it fails before getting the http 3xx.

@plajko

This comment has been minimized.

Copy link

@plajko plajko commented May 4, 2021

@jonahmolinski Sorry, my bad :) the http client I was using was following redirects automatically (without forwarding the headers), now works like a charm! Thanks!

@ricketyca

This comment has been minimized.

Copy link

@ricketyca ricketyca commented May 18, 2021

Some time ago I had used this helpful information above to generate html code to download a PDF file from a sheet. I plugged the appropriate codes into an href which I displayed on a web page on a google site. I could not get the range feature to work. I discovered that when I appended the href html, the "&ic" was being converted to "%E2%81%A3" and therefore any attempt to activate the href (I was using a button) failed. Things were a bit rushed so I did not bother as it was not critical.

I have now discovered a simple solution. If instead of "&ic" I instead use the coded form, (following the & with "amp;" and then the "ic") and it now works OK in my situation.

The same 'conversion' was happening to '&range' and inserting the 'amp;' solved it there too. For interest's sake this only applies to the href download and not when you use it in a GET

@ricketyca

This comment has been minimized.

Copy link

@ricketyca ricketyca commented May 19, 2021

Hi ,
The above code will download the file in google drive only , is there any way i can download file to my local computer drive.

Thanks
Anshu

You may find this can work for you. Instead of a script using the GET I have a page with a button where I put the link. Then it downloads. The script sample is getting the blob of the PDF and explicitly loading it into the Drive folder. I realise this may not be suitable for you but wanted you to know a way. hth

@bihaequipinc

This comment has been minimized.

Copy link

@bihaequipinc bihaequipinc commented Jul 8, 2021

Hi,

The code is work for me.
But how to export a few sheets?
Meaning a few gid ?

@samjco

This comment has been minimized.

Copy link

@samjco samjco commented Jul 8, 2021

So Google doesn't have a full-blown API of how to do everything with Gsheets and PDF. And I think it kinda strange that we are left to figuring things out... So I am now looking into phpOffice, wondering if data could be passed into this environment and then convert to pdf
https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/#pdf
examples:
https://stackoverflow.com/a/55356683
https://stackoverflow.com/a/39355051

OR

Something like this will work using: http://fpdf.org/

So the idea is to make a button in Gsheets that will produce an output of a dynamic CSV link. Then add FPDF to your server to ingest that data and convert it on the fly and produce a PDF that will show in the browser. Here is a sample code:
http://fpdf.org/en/script/script102.php

example output:
http://fpdf.org/en/script/ex102.pdf

@bihaequipinc

This comment has been minimized.

Copy link

@bihaequipinc bihaequipinc commented Jul 9, 2021

I have my own template with google sheet. Like impossible to pass my template to phpOffice. Hmm do you have any ideas on how to export few sheets or few tab using appsscript ?

@bihaequipinc

This comment has been minimized.

Copy link

@bihaequipinc bihaequipinc commented Jul 9, 2021

So Google doesn't have a full-blown API of how to do everything with Gsheets and PDF. And I think it kinda strange that we are left to figuring things out... So I am now looking into phpOffice, wondering if data could be passed into this environment and then convert to pdf
https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/#pdf
examples:
https://stackoverflow.com/a/55356683
https://stackoverflow.com/a/39355051

OR

Something like this will work using: http://fpdf.org/

So the idea is to make a button in Gsheets that will produce an output of a dynamic CSV link. Then add FPDF to your server to ingest that data and convert it on the fly and produce a PDF that will show in the browser. Here is a sample code:
http://fpdf.org/en/script/script102.php

example output:
http://fpdf.org/en/script/ex102.pdf

I have my own template with google sheet. Like impossible to pass my template to phpOffice. Hmm do you have any ideas on how to export few sheets or few tab using appsscript ?
Untitled

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