【问题标题】:Highlight text and insert comment into specific Google Doc text突出显示文本并将评论插入特定的 Google Doc 文本
【发布时间】:2014-02-26 23:25:20
【问题描述】:

我想知道是否可以通过某些 Google API 突出显示特定单词或文本正文并插入评论。我意识到驱动 API 有一个插入注释方法,但是,生成的注释是针对整个文档的,而不仅仅是针对文本的特定部分。

非常感谢。

【问题讨论】:

  • 你知道你要找什么文字吗?
  • 我正在寻找说文档中某个位置的名称,然后突出显示该词并创建评论。

标签: google-apps-script


【解决方案1】:

到目前为止,我已经能够匹配并突出显示文档中文本“Earth”的所有实例(搜索目标字符串),但我仍在努力添加评论。这是执行此操作的代码:

function myFunction() {
  var fileId = 'Id of your Document goes here';
  var background = background || '#FFFF00';  // highlight yellow.
  var target = 'Earth';
  var doc = DocumentApp.openById(fileId);
  var bodyElement = doc.getBody();
  var searchResult = bodyElement.findText(target);

  while (searchResult !== null) {
    var thisElement = searchResult.getElement();
    var thisElementText = thisElement.asText();

    //Logger.log(url);
    thisElementText.setBackgroundColor(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive(),background);

    // search for next match
    searchResult = bodyElement.findText(target, searchResult);
  }
}

这是从这里改编/复制的:Can I color certain words in Google Document using Google Apps Script?(从技术上讲,我没有适应太多,因为太晚了,而且我很懒)。仍在努力添加评论。由于这样做的关键是隔离与目标匹配的“元素”,因此我怀疑只需找到一种方法将 cmets 添加到元素并将其标记到背景着色步骤的末尾即可。

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 2013-01-23
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多