从DriveApp查找两个文本文件中的差异

时间:2019-04-11 13:31:13

标签: google-apps-script google-docs

我正在编写一个脚本,用于将Google文档的内容复制到文本文件中并将其存储在Google云端硬盘中。然后,用户编辑文档,完成操作后,它将复制新版本以将其驱动到单独的文件中。我已经完成了这一部分,但是现在我需要知道如何使脚本自动比较文本中的差异,然后在文档中更改这些差异的颜色。有任何想法吗?

我尝试过的唯一一件事就是问我的朋友,在编程方面我比我了解得多,哈哈。他帮不了我。

function copyQuestions() {
  var body = DocumentApp.getActiveDocument().getBody();
  var getquestions = body.getText()
  DriveApp.createFile('tempquestions.txt', getquestions);
  DocumentApp.getUi()
  .alert('Questions have been copied to a new file on Google Drive. Get to work, and be sure not to delete the file!')
}
function copyAnswers() {
  var body = DocumentApp.getActiveDocument().getBody();
  var getanswers = body.getText()
  DriveApp.createFile('tempanswers', getanswers);
  DocumentApp.getUi()
  .alert('Coloring answers...')
}

该脚本创建带有文档内容副本的文件。我现在需要知道如何比较两者之间的差异。谢谢!

1 个答案:

答案 0 :(得分:1)

听起来像您需要Diff引擎。您可以尝试利用Levenshtein DistanceDice's Coefficient之类的知名算法编写自己的算法,但是如果您不太愿意,可以尝试寻找与Apps Script兼容的现有javascript库。 / p>

进行了快速搜索,发现了一些开放源代码存储库。我不能保证它们与Apps Script的兼容性,但其中至少有一个似乎可行(JSDiff支持EcmaScript 3,而Apps Script最多支持EcmaScript 5):


JSDIFF


NODE-DELTA