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

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

问题描述

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

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,我可以访问文档上的一系列修订并根据用户进行迭代.返回的对象不包含修订的 content ,而仅包含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,并将该内容附加到文档中.问题在于,提取应用程序会返回整个文档标记(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=###之类的查询参数一起使用.

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天全站免登陆