Export Large Google Sheet to PDF

google sheetsgoogle-apps-script

I have a google sheet having 110,000 rows and 10 columns.

If I try to export using the normal File > Download > As PDF, it takes forever to make the Save button work (The ~4800 pages of the PDF keeps rendering and on a premature save, nothing happens) I have done this on Chrome, Firefox and Edge browser, just to see if changing browser would make a difference.

Then I tried using app-script, but the ones available on the WebApp StackExchange did not work.

I know the solution will be with an app script code. What is the best solution for me to able to export the 1 million cell sized sheet to a PDF easily.

EDIT:

As per the comments below I did execute the 8 line code in the newer app script console of Google,

function saveAsPDF() {
const folderName = `Cubing`;
const fileNamePrefix = `filename`;

DriveApp.getFoldersByName(folderName)
.next()
.createFile(SpreadsheetApp.getActiveSpreadsheet()
              .getBlob()
              .getAs(`application/pdf`)
              .setName(`${fileNamePrefix} - ${Utilities.formatDate(new 
 Date(), `GMT-8`, `yyyy-MM-dd`)}`));  
 }

But I got a message which is not related to the code.

The execution log said,

Exception: We're sorry, no servers are currently available. Please wait a bit and try again.
saveAsPDF   @ Code.gs:7

Best Answer

There is a ` pair-matching problem:

1
|
v
`${fileNamePrefix} - ${Utilities.formatDate(new 
 Date(), `GMT-8`, `yyyy-MM-dd`)}`
         ^     ^  ^          ^  ^
         |     |  |          |  |
         1     2  2          3  3    

While there are multiple ways to fix your script, what about this (use the + to concatenate values and regular strings instead of template literals):

fileNamePrefix + ' - ' + Utilities.formatDate(new Date(), 'GMT-8', 'yyyy-MM-dd')

Resources