【问题标题】:How can I create a PDF link to the last named version of a Google Document?如何创建指向 Google 文档最后命名版本的 PDF 链接?
【发布时间】:2019-11-30 14:53:40
【问题描述】:

我有一个可在我的网站上下载的实时文档(设备手册)。我希望能够在不让用户看到这些细微更改的情况下进行更改,因此认为使用命名版本是理想的。但我看不到链接到最新命名版本的方法。

我确信我可以使用 Google Script 创建第二个文档并在每次命名版本时复制到其中,但我认为应该有更简单的方法。

我当前使用的 URL 格式如下。

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

只要我不更改文档,此方法就可以正常工作。

【问题讨论】:

    标签: rest url google-api google-docs google-docs-api


    【解决方案1】:

    答案:

    很遗憾,无法使用 G Suite API 检索命名版本的名称。

    更多信息:

    Docs API 本身没有允许访问修订的方法,虽然 Drive API 有,但没有名称的资源表示,因此无法通过名称本身识别修订。

    解决方法:

    如果您需要的版本是最新版本,您可以使用 Drive API Revisions: list 方法检索最新版本,并从资源响应的exportLinks 属性中获取到该版本的导出链接:

    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;
    }
    

    注意:您必须使用 Advanced Drive Service v2 从 Apps 脚本中获取修订 - 激活此功能,导航至 Resources &gt; Advanced Google Services... 并单击 Drive API 旁边的开关,使其显示为 on 并变为绿色。

    参考资料:

    【讨论】:

    • 这真的很接近,但云端硬盘文件的权限似乎与文档不匹配。此链接:docs.google.com/feeds/download/documents/export/Export?id={Docs ID}&revision=13&exportFormat=pdf 不适用于外部用户,而此链接:docs.google.com/document/d{Docs ID}/export?format=pdf 只要设置了权限就可以正常工作。
    • 我尝试添加这一行: DriveApp.getFileById(fileId).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);但没有运气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多