首页 > 解决方案 > Google Doc 评论 - 使用 Appscript 更新

问题描述

我希望每个人都身体健康。我是AppScript的新手,喜欢使用它自动化 Google Docs 的概念。

我尝试了什么;
我最近遇到了一个问题,我需要使用 AppScript 根据其在 Google Docs 中的评论来更新文本。
这是演示在此处输入图像描述 ,但我知道使用 AppScript 是不可能的,所以我必须使用Google Drive API并且我尝试了它,但我仍然没有成功。当我尝试时,它似乎确实发生了变化,console.log(comments.items[i].context.value);但我的 Google Doc 中的一切都是一样的,似乎没有任何改变。

这是我的代码

function listComments() {
  let comments = Drive.Comments.list(DocumentApp.getActiveDocument().getId());

  if (comments.items && comments.items.length > 0) {
    for (let i = 1; i < comments.items.length; i++) {
      let comment = comments.items[i].content;
      let text = comments.items[i].context.value;
      comments.items[i].context.setValue(`[${text}](${comment})`);
      console.log(comments.items[i].context.value);
    }
  } else {
    Logger.log('No comment found.');
  }
}

我在任何地方都找不到更新AppScript 中的评论上下文值的文档,这也将反映在我的 Google Doc 中。
我希望你能解决我的问题。提前致谢!

标签: google-apps-scriptgoogle-drive-apigoogle-docs

解决方案


检查 Drive API 后,我没有找到任何方法来更新评论的 context.value 属性。更新评论时,您需要使用Comments.update()。但是,此方法仅允许您修改/更新评论的内容。如果您尝试根据评论内容替换 Google Docs 中的特定字符串,您需要使用Apps Script中的Document Service更新文档


更新现有评论时,您需要提供以下参数:

commentId - 评论的 ID。

fileId - 文件的 ID。

commentId 在 Comments.list 响应中可用

样本:

 {
   "kind": "drive#comment",
   "commentId": "sampleCommentId",
   ....
   },
   "htmlContent": "Test Name",
   "content": "Test Name",
   "deleted": false,
   "status": "resolved",
   "context": {
    "type": "text/html",
    "value": "name"
   },

例子:

在此处输入图像描述

内容 = "测试名称"

context.value = "名称"

如果我将使用带有以下请求正文的Comments.update()更新此评论:

{
  "content": "My Name",
  "context": {
    "value": "name"
  }
}

输出:

在此处输入图像描述


推荐阅读