通过 Google Apps 脚本检索修订文本 [英] Retrieve revision text through Google Apps Script

查看:25
本文介绍了通过 Google Apps 脚本检索修订文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Google Apps Script 获取文档的修订历史记录,并且正在寻找有关如何以编程方式访问修订内容的建议.

I'm playing with getting the revision history of a document through Google Apps Script and I'm looking for some advice on how to programmatically access the content of the revision.

使用 Drive API,我可以访问文档上的一系列修订并根据用户进行迭代.返回的对象不包括修订的内容,只是一个 ID.但是,您可以获得各种内容类型(pdf、纯文本等)的下载 URL.

Using the Drive API, I can access an array of revisions on the document and iterate based on user. The returned object does not include the content of the revision, just an ID. But, you can get a download URL for various content types (pdf, plaintext, etc).

我想使用 UrlFetchApp 检索下载 URL 并将该内容附加到文档中.问题是 fetch 应用程序返回整个文档标记(HTML 和 CSS),而我只想要文件的内容.

I'd like to retrieve a download URL using UrlFetchApp and get that content to append to a document. The problem is that the fetch app returns the entire document markup (HTML and CSS) and I'd only like the content of the file.

脚本

function revisionHistoryLite() {
  var doc = DocumentApp.getActiveDocument();
  var eds = doc.getEditors();
  var body = doc.getBody();

  var revs = Drive.Revisions.list(doc.getId())

  var editsList = [];

  for(var i=0; i<revs.items.length; i++) {
    var revision = revs.items[i];
    editsList.push([revision.id, revision.kind, revision.modifiedDate, revision.lastModifyingUser.emailAddress]);

    if(revision.lastModifyingUser.emailAddress == "bbennett@elkhart.k12.in.us") {
      var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"];

      // revUrl returns https://docs.google.com/feeds/download/documents/export/Export?id=docIdString&revision=1&exportFormat=txt

      var revString = UrlFetchApp.fetch(revUrl, { contentType: "text/plain",  }).getContentText();
      Logger.log(revString); // Contains full HTTP markup

      // Append the body contents to a temporary document for further processing
      // var tempDoc = DocumentApp.create("Temp").getBody().appendParagraph(revString);


    }
  }
}

推荐答案

使用UrlFetchApp.fetch()从exportLinks下载文件时,需要授权.所以请修改你的脚本如下.

When it downloads files from exportLinks using UrlFetchApp.fetch(), the authorization is required. So please modify your script as follows.

var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"];
var revString = UrlFetchApp.fetch(revUrl, { contentType: "text/plain",  }).getContentText();

致:

var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"]  + "&access_token=" + ScriptApp.getOAuthToken();
var revString = UrlFetchApp.fetch(revUrl).getContentText();

由此,您可以从修订数据中下载文本数据.

By this, you can download text data from the revision data.

function revisionHistoryLite() {
  var doc = DocumentApp.getActiveDocument();
  var eds = doc.getEditors();
  var body = doc.getBody();
  var revs = Drive.Revisions.list(doc.getId())
  var editsList = [];
  for(var i=0; i<revs.items.length; i++) {
    var revision = revs.items[i];
    editsList.push([revision.id, revision.kind, revision.modifiedDate, revision.lastModifyingUser.emailAddress]);
    if(revision.lastModifyingUser.emailAddress == "### mail address ###") {
      var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"]  + "&access_token=" + ScriptApp.getOAuthToken();
      var revString = UrlFetchApp.fetch(revUrl).getContentText();
      Logger.log(revString); // Contains full HTTP markup
    }
  }
}

更新时间:2020 年 2 月 7 日

从 2020 年 1 月起,访问令牌不能与查询参数一起使用,例如 access_token=###.Ref 所以请使用请求头的访问令牌而不是查询参数.如下.

Updated: February 7, 2020

From January, 2020, the access token cannot be used with the query parameter like access_token=###. Ref So please use the access token to the request header instead of the query parameter. It's as follows.

var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});

这篇关于通过 Google Apps 脚本检索修订文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆