Save a Sheet as a PDF Uploading/Replacing an existing drive hosted PDF

243 views
Skip to first unread message

Robert Alder

unread,
Sep 26, 2023, 9:11:38 PM9/26/23
to Google Apps Script Community

I have no idea why Google has made a simple operation so damned difficult.  As near as I can tell, a complicated Google Script is needed.  So, I’m begging for a solution.  Either a full script or an Add-On (if one exists) to automate the following;

I have a Google Sheet containing some contact info. This is where the info is maintained. Easy to maintain. OK so far.

But there is a need to then create a PDF of that sheet after editing it. This is also easy, albeit a manual task, and the resulting PDF is just “Downloaded” to my local download folder. BUT THAT’s NOT what is needed.

There is already an existing PDF version of the file on the Google Drive.   That file is shared with anyone with its URL link. Perfect.  So far, so good.

But to “post” a new version of the PDF file I have to go thru this terrible routine:

·       First go thru the several steps to first download the sheet as a new PDF which gets sent to my local Download folder.  Easy enough, but far from desired end result.

  • ·       So, I go back to the Google drive,
  • ·       Locate the Pre-edited PDF version on the google drive.
  • ·       Then Right click on it.
  • ·       Then Choose “File Info”
  • ·       Then choose “Manage versions”
  • ·       Then “Upload New Version”
  • ·       And then point to my local downloaded folder to find/select the recently updated PDF.

Done.  Who knew?   It works in only 10 very complicated steps (which only took me hours and hours of Google searching to figure how to even do it that round about way.  

The above will process will, indeed, save the PDF on the drive with the same URL and same sharing qualities. Why shouldn’t this be a built in one-step choice within Google drive?  You know – a simple “Save and upload/replace an existing PDF file version,”  But, alas, it isn’t.  Apparently the above manual steps are required. Or maybe a complicated Google script is needed after first editing a Google sheet (or Doc) to run and do all the above steps to save and upload  the resulting new PDF. The Upload/replace is necessary so all outstanding links to the PDF (on websites, bookmarks, links in prior emails, etc.) will then still work and be linked to the current updated PDF version of the “source” sheet or doc.  

I’m open to an Add-On (If there is one) that could be loaded and used to automate this.  

My idea would be to put a button on the source sheet or doc (or a custom ribbon addition in the Google Sheets application) to simply run the script or Add-On which will upload/replace the existing PDF.  This rather than the above COMPLICATED manual steps. In a perfect world the script or Add-On would be smart enough to always save/upload the resulting PDF in the same Drive directory as it the source sheet or .doc.  

Anyone have the solution?  No, I’m not a proficient Google script writer so I can’t write this stuff from scratch. But I know a script (or maybe an Add-On utility) can do it.

HELP.  Why Mr. Google doesn’t provide this simple file maintenance utility within the Google  account is beyond me.

Bob Alder

Terri C

unread,
Oct 6, 2023, 10:21:39 AM10/6/23
to Google Apps Script Community
This might help you. I have a process where I replace a PFD file in a folder that is shared with users who access the pdf that exists in a specified Google folder.

SpreadsheetId is the Google sheet source of the data, pdfName is the name of the PDF file that will go in the folder, and sharedfolderID is the ID of the folder where I'm putting the PDF. This was written a while ago and is in a process that still runs every morning.

function convertSpreadsheetToPdf(spreadsheetId, pdfName) {
  var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = spreadsheetId ? spreadsheetId : spreadsheet.getId();
  var pdfName = pdfName ? pdfName : spreadsheet.getName();
  var parents = DriveApp.getFileById(spreadsheetId).getParents();
  var folder = parents.hasNext() ? parents.next() : DriveApp.getRootFolder();
  var url_base = spreadsheet.getUrl().replace(/edit$/,'');
  var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
    + '&id=' + spreadsheetId
    + '&size=A4'      // paper size
    + '&portrait=true'    // orientation, false for landscape
    + '&fitw=true'        // fit to width, false for actual size
    + '&sheetnames=false&printtitle=false&pagenumbers=false'  //hide optional headers and footers
    + '&gridlines=false'  // hide gridlines
    + '&fzr=false';       // do not repeat row headers (frozen rows) on each page
  var options = {headers: {'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()}}
  var response = UrlFetchApp.fetch(url_base + url_ext, options);
  var blob = response.getBlob().setName(pdfName + '.pdf');
  var sharedfolder = DriveApp.getFolderById(sharedfolderID);
  sharedfolder.createFile(blob);

teamterrificracing

unread,
Oct 6, 2023, 11:12:08 AM10/6/23
to 'Terri C' via Google Apps Script Community
Terri,
Just wanted to thank you for your response. I'm out of town until mid to late next week and unable to attend to really looking at what you sent. It's  most appreciated! I'll keep you posted. 
Bob Alder



On Oct 6, 2023 at 9:21 AM, 'Terri C' via Google Apps Script Community <google-apps-sc...@googlegroups.com> wrote:

This might help you. I have a process where I replace a PFD file in a folder that is shared with users who access the pdf that exists in a specified Google folder.

SpreadsheetId is the Google sheet source of the data, pdfName is the name of the PDF file that will go in the folder, and sharedfolderID is the ID of the folder where I'm putting the PDF. This was written a while ago and is in a process that still runs every morning.

function convertSpreadsheetToPdf(spreadsheetId, pdfName) {
  var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = spreadsheetId ? spreadsheetId : spreadsheet.getId();
  var pdfName = pdfName ? pdfName : spreadsheet.getName();
  var parents = DriveApp.getFileById(spreadsheetId).getParents();
  var folder = parents.hasNext() ? parents.next() : DriveApp.getRootFolder();
  var url_base = spreadsheet.getUrl().replace(/edit$/,'');
  var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
    + '&id=' + spreadsheetId
    + '&size=A4'      // paper size
    + '&portrait=true'    // orientation, false for landscape
    + '&fitw=true'        // fit to width, false for actual size
    + '&sheetnames=false&printtitle=false&pagenumbers=false'  //hide optional headers and footers
    + '&gridlines=false'  // hide gridlines
    + '&fzr=false';       // do not repeat row headers (frozen rows) on each page
  var options = {headers: {'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()}}
  var response = UrlFetchApp.fetch(url_base + url_ext, options);
  var blob = response.getBlob().setName(pdfName + '.pdf');
  var sharedfolder = DriveApp.getFolderById(sharedfolderID);
  sharedfolder.createFile(blob);

On Tuesday, September 26, 2023 at 9:11:38 PM UTC-4 Robert Alder wrote:

NOTICE: This message is from the Greece Central School District. This message and any attachments may be confidential and/or privileged and are intended only for the individual(s) or group(s) identified as the addressee. If the message addressee is in error, or you are not authorized to read, copy, or distribute this message or attachments; please delete this message and attachments and notify the sender by return email at the address listed above.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/DA0lPahD5lI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/c6a3c8ae-5e45-4cf9-b1d8-81b403019470n%40googlegroups.com.

Terri C

unread,
Oct 6, 2023, 12:56:09 PM10/6/23
to Google Apps Script Community
I did notice I left the closing } off the function I pasted in my response. Just a note in the bigger script I use to call this function. I delete the existing PDF file in the drive before re-creating it with this function with the same name. This way I just have the "current" PDF file that the others who have access to the folder see.
Reply all
Reply to author
Forward
0 new messages