Skip to content

批注(Comments)

Comment 对象的集合,这些对象代表指定选定内容、范围或文档中的批注

属性列表

属性说明
Count获取批注个数

方法列表

方法返回类型说明
Add()添加批注
DeleteComment()删除单个批注
DeleteAll()删除所有批注
GetComments()Comment 对象的集合全部书签
Item()Comment单个批注对象
ModifyComment()修改批注内容
ReplyComment()回复批注
GoToComment()跳转到批注位置
SetSolved()设置批注解决状态

Count

获取批注个数

语法

表达式.ActiveDocument.Comments.Count

返回值

返回 Number 代表文档内容控件的数量。

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 添加批注
  await app.ActiveDocument.Comments.Add({
    text: "WebOffice",
    range: {
      start: 1,
      end: 10,
    },
  });

  // 获取批注数量
  const count = await app.ActiveDocument.Comments.Count;
  console.log(count);
}

Add()

添加批注

语法

表达式.ActiveDocument.Comments.Add({text, useSelection, range})

参数

属性类型默认值必填说明
textString批注内容。
useSelectionBoolean否,与 range 必选其一是否使用当前选区范围
rangeRange{}否,与 useSelection 必选其一批注的文本起止区域。
range.startNumber区域开始位置
range.endNumber区域结束位置

IMPORTANT

无论使用当前选区还是指定 range ,如果范围内没有有效文本,则批注不会添加成功

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 使用指定范围添加批注1
  await app.ActiveDocument.Comments.Add({
    text: "批注1",
    range: {
      start: 1,
      end: 10,
    },
  });

  // 使用当前选区添加批注2
  await app.ActiveDocument.Comments.Add({
    text: "批注2",
    useSelection: true
  });
}

DeleteComment()

删除单条批注

语法

表达式.ActiveDocument.Comments.DeleteComment({commentId})

参数

属性类型默认值必填说明
commentIdString批注id

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 删除批注
  await app.ActiveDocument.Comments.DeleteComment({commentId: '123'});
}

DeleteAll()

删除所有批注

语法

表达式.ActiveDocument.Comments.DeleteAll()

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 删除全部批注
  await app.ActiveDocument.Comments.DeleteAll();
}

GetComments()

全量批注集合

语法

表达式.ActiveDocument.Comments.GetComments()

返回值

返回 Comment 对象集合,单个批注属性见 Item()

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 添加批注
  await app.ActiveDocument.comments.Add({
    text: "WebOffice",
    range: {
      start: 1,
      end: 10,
    },
  });

  // 获取全量批注
  const comments = await app.ActiveDocument.Comments.GetComments();

  console.log(comments); 
}

Item()

批注集合的单个批注

语法

表达式.ActiveDocument.Comments.Item(key)

参数

属性类型默认值必填说明
keyNumber 或 String如果是数字,则为索引值;如果是字符,则为批注id

返回值

返回 Comment 对象,拥有以下属性和方法

属性列表

属性类型说明
IdString批注id
UserNameString作者名
UserIdString作者id
TextString批注内容
QuoteTextString批注引用的文本
DateNumber批注添加的时间戳

方法列表

方法说明参数
SetSolved(bSolved)设置批注的解决状态bSolved: Boolean,必须,默认值无。true 为解决,false 为未解决设置批注的解决状态

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 添加批注
  await app.ActiveDocument.comments.Add({
    text: "WebOffice",
    range: {
      start: 1,
      end: 10,
    },
  });

  // 单个批注对象
  const firstComment = await app.ActiveDocument.comments.Item(1);

  console.log(await firstComment.Text); // 批注内容
  console.log(await firstComment.UserName); // 批注作者
  await firstComment.SetSolved(true); // 设置为已解决
}

ModifyComment()

修改单条批注

语法

表达式.ActiveDocument.Comments.ModifyComment({commentId, text})

参数

属性类型默认值必填说明
commentIdString批注id
textString批注的新内容

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 添加批注
  await app.ActiveDocument.comments.Add({
    text: "WebOffice",
    range: {
      start: 1,
      end: 10,
    },
  });

  // 单个批注对象
  const firstComment = await app.ActiveDocument.comments.Item(1);

  // 修改批注
  const commentId = firstComment.Id;
  await app.ActiveDocument.Comments.ModifyComment({commentId, text: '新的批注内容'});
}

ReplyComment()

回复单条批注

语法

表达式.ActiveDocument.Comments.ReplyComment({commentId, text})

参数

属性类型默认值必填说明
commentIdString批注id
textString批注的回复内容

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 添加批注
  await app.ActiveDocument.comments.Add({
    text: "WebOffice",
    range: {
      start: 1,
      end: 10,
    },
  });

  // 单个批注对象
  const firstComment = await app.ActiveDocument.comments.Item(1);

  // 回复批注
  const commentId = firstComment.Id;
  await app.ActiveDocument.Comments.ReplyComment({commentId, text: '批注的回复内容'});
}

GoToComment()

跳转到某条批注

语法

表达式.ActiveDocument.Comments.GoToComment({commentId})

参数

属性类型默认值必填说明
commentIdString批注id

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 添加批注
  await app.ActiveDocument.comments.Add({
    text: "WebOffice",
    range: {
      start: 1,
      end: 10,
    },
  });

  // 单个批注对象
  const firstComment = await app.ActiveDocument.comments.Item(1);

  // 跳转到批注
  const commentId = firstComment.Id;
  await app.ActiveDocument.Comments.GoToComment({commentId});
}

SetSolved()

修改某条批注的解决状态

语法

表达式.ActiveDocument.Comments.SetSolved({commentId, bSolved})

参数

属性类型默认值必填说明
commentIdString批注id
bSolvedBoolean解决状态

示例

js
async function example() {
  await jssdk.ready();

  const app = jssdk.Application;

  // 添加批注
  await app.ActiveDocument.comments.Add({
    text: "WebOffice",
    range: {
      start: 1,
      end: 10,
    },
  });

  // 单个批注对象
  const firstComment = await app.ActiveDocument.comments.Item(1);

  // 将第一条批注设置为已解决
  const commentId = firstComment.Id;
  await app.ActiveDocument.Comments.SetSolved({commentId, bSolved: true});
}