Create PDF link to the last named version of a Google Document

落花浮王杯 提交于 2020-06-28 05:58:46

问题


I have a live document which is available for download on my website (a manual for a piece of equipment). I want to be able to make changes without these minor changes being visible to the user and so thought that using named versions would be ideal. But I can't see a way to link to the most recent named version.

I'm sure I could use Google Script to create a second document and duplicate into it every time I name the version, but I was thinking there should be an easier way.

The URL I'm currently using is in the following format.

https://docs.google.com/document/d/{Document ID}/export?format=pdf

This works well as long as I don't make changes to the document.

Any ideas?

Thanks,

Stu


回答1:


Answer:

Unfortunately, the name of a named version can't be retrieved using the G Suite APIs.

More Information:

The Docs API itself doesn't have methods that allow access to revisions, and while the Drive API does, there isn't a resource representation of the name so the revisions can't be identified by name itself.

Workaround:

If the version you need is the latest version, you can use the Drive API Revisions: list method to retrieve the latest revision and get an export link to that from the exportLinks property of the resource response:

function getExportLink() {
  var fileId = "<your-file-id>";
  var revisions = Drive.Revisions.list(fileId);
  var latestRevision = revisions.items[(revisions.items.length - 1)];
  var url = "https://docs.google.com/feeds/download/documents/export/Export?id=";
  var revision = "&revision=" + latestRevision.id;
  var format = "&exportFormat=pdf";
  
  return url + fileId + revision + format;
}

Note: you have to use the Advanced Drive Service v2 to get Revisions from within Apps Script - to activate this navigate to Resources > Advanced Google Services... and click the switch next to Drive API so that it says on and turns green.

I hope this is helpful to you!

References:

  • Google Documents API Reference
  • Download and publish file revisions - Google Drive API
  • Google Drive API Revisions Overview
  • Google Drive API Revisions: list method


来源:https://stackoverflow.com/questions/59117269/create-pdf-link-to-the-last-named-version-of-a-google-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!