Word.CommentCollection class
包含 Word 集合。批注对象。
注解
使用方
- Word.正文:getComments
- Word.ContentControl: getComments
- Word.文档: 批注
- Word.段落:getComments
- Word.范围: getComments
- Word.按选定内容: 注释
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-comments.yaml
// Replies to the first active comment in the selected content.
await Word.run(async (context) => {
const text = (document.getElementById("reply-text") as HTMLInputElement).value;
const comments: Word.CommentCollection = context.document.getSelection().getComments();
comments.load("items");
await context.sync();
const firstActiveComment: Word.Comment = comments.items.find((item) => item.resolved !== true);
if (firstActiveComment) {
const reply: Word.CommentReply = firstActiveComment.reply(text);
console.log("Reply added.");
} else {
console.warn("No active comment was found in the selection, so couldn't reply.");
}
});
方法
| get |
获取集合中的第一条注释。 如果此集合为空,则引发 |
| get |
获取集合中的第一条注释。 如果集合为空,则此方法将返回其 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| toJSON() | 覆盖 JavaScript |
| track() | 根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 |
| untrack() | 释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 |
属性详细信息
context
items
获取此集合中已加载的子项。
readonly items: Word.Comment[];
属性值
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-comments.yaml
// Replies to the first active comment in the selected content.
await Word.run(async (context) => {
const text = (document.getElementById("reply-text") as HTMLInputElement).value;
const comments: Word.CommentCollection = context.document.getSelection().getComments();
comments.load("items");
await context.sync();
const firstActiveComment: Word.Comment = comments.items.find((item) => item.resolved !== true);
if (firstActiveComment) {
const reply: Word.CommentReply = firstActiveComment.reply(text);
console.log("Reply added.");
} else {
console.warn("No active comment was found in the selection, so couldn't reply.");
}
});
方法详细信息
getFirst()
获取集合中的第一条注释。 如果此集合为空,则引发 ItemNotFound 错误。
getFirst(): Word.Comment;
返回
注解
getFirstOrNullObject()
获取集合中的第一条注释。 如果集合为空,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性。
getFirstOrNullObject(): Word.Comment;
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-comments.yaml
// Gets the range of the first comment in the selected content.
await Word.run(async (context) => {
const comment: Word.Comment = context.document.getSelection().getComments().getFirstOrNullObject();
comment.load("contentRange");
const range: Word.Range = comment.getRange();
range.load("text");
await context.sync();
if (comment.isNullObject) {
console.warn("No comments in the selection, so no range to get.");
return;
}
console.log(`Comment location: ${range.text}`);
const contentRange: Word.CommentContentRange = comment.contentRange;
console.log("Comment content range:", contentRange);
});
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: Word.Interfaces.CommentCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions): Word.CommentCollection;
参数
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): Word.CommentCollection;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Word.CommentCollection;
参数
- propertyNamesAndPaths
- OfficeExtension.LoadOption
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.CommentCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.CommentCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。
toJSON(): Word.Interfaces.CommentCollectionData;
返回
track()
根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。
track(): Word.CommentCollection;
返回
untrack()
释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync() 。
untrack(): Word.CommentCollection;