Excel.CommentCollection class

表示属于工作簿的注释对象的集合。

Extends

注解

[ API 集:ExcelApi 1.10 ]

属性

context

与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。

items

获取此集合中已加载的子项。

方法

add(cellAddress, content, contentType)

使用给定单元格上的给定内容创建新批注。 InvalidArgument如果提供的范围大于一个单元格,则会引发错误。

add(cellAddress, content, contentTypeString)

使用给定单元格上的给定内容创建新批注。 InvalidArgument如果提供的范围大于一个单元格,则会引发错误。

getCount()

获取集合中的批注数量。

getItem(commentId)

根据其 ID 从集合中获取批注。

getItemAt(index)

根据其位置从集合中获取批注。

getItemByCell(cellAddress)

从指定单元格获取的批注。 如果单元格中没有注释,则会引发错误。

getItemByReplyId(replyId)

获取给定答复所连接到的注释。

getItemOrNullObject(commentId)

根据其 ID 从集合中获取批注。 如果注释对象不存在,则此方法返回一个对象,其 isNullObject 属性设置为 true。 有关详细信息,请参阅 *OrNullObject 方法和属性

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,反过来,调用toJSON传递给它的 对象的 方法。) 虽然原始Excel.CommentCollection对象是 API 对象,toJSON但该方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.CommentCollectionData) ,其中包含一个“items”数组,其中包含集合项中任何已加载属性的浅表副本。

事件

onAdded

添加注释时发生。

onChanged

在批注集合中的批注或答复发生更改时发生,包括删除答复时。

onDeleted

在批注集合中删除批注时发生。

属性详细信息

context

与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

items

获取此集合中已加载的子项。

readonly items: Excel.Comment[];

属性值

方法详细信息

add(cellAddress, content, contentType)

使用给定单元格上的给定内容创建新批注。 InvalidArgument如果提供的范围大于一个单元格,则会引发错误。

add(cellAddress: Range | string, content: CommentRichContent | string, contentType?: Excel.ContentType): Excel.Comment;

参数

cellAddress

Excel.Range | string

要向其添加注释的单元格。 这可以是 Range 对象或字符串。 如果是字符串,则必须包含完整地址,包括工作表名称。 InvalidArgument如果提供的范围大于一个单元格,则会引发错误。

content

Excel.CommentRichContent | string

批注的内容。 这可以是字符串或 CommentRichContent 对象。 字符串用于纯文本。 CommentRichContent 对象允许使用其他注释功能,例如提及。 [Api 集:ExcelApi 1.10 字符串,1.11 用于 CommentRichContent 对象]

contentType
Excel.ContentType

可选。 批注中包含的内容类型。 默认值为 enum ContentType.Plain。 [Api 集:ExcelApi 1.10 for Enum ContentType.Plain,1.11 for Enum ContentType.Mention]

返回

注解

[ API 集:ExcelApi 1.10 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/12-comment/comment-basics.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Comments");

    // Note that an InvalidArgument error will be thrown if multiple cells passed to `comment.add`.
    sheet.comments.add("A2", "TODO: add data.");
    await context.sync();
});

add(cellAddress, content, contentTypeString)

使用给定单元格上的给定内容创建新批注。 InvalidArgument如果提供的范围大于一个单元格,则会引发错误。

add(cellAddress: Range | string, content: CommentRichContent | string, contentTypeString?: "Plain" | "Mention"): Excel.Comment;

参数

cellAddress

Excel.Range | string

要向其添加注释的单元格。 这可以是 Range 对象或字符串。 如果是字符串,则必须包含完整地址,包括工作表名称。 InvalidArgument如果提供的范围大于一个单元格,则会引发错误。

content

Excel.CommentRichContent | string

批注的内容。 这可以是字符串或 CommentRichContent 对象。 字符串用于纯文本。 CommentRichContent 对象允许使用其他注释功能,例如提及。 [Api 集:ExcelApi 1.10 字符串,1.11 用于 CommentRichContent 对象]

contentTypeString

"Plain" | "Mention"

可选。 批注中包含的内容类型。 默认值为 enum ContentType.Plain。 [Api 集:ExcelApi 1.10 for Enum ContentType.Plain,1.11 for Enum ContentType.Mention]

返回

注解

[ API 集:ExcelApi 1.10 ]

getCount()

获取集合中的批注数量。

getCount(): OfficeExtension.ClientResult<number>;

返回

注解

[ API 集:ExcelApi 1.10 ]

getItem(commentId)

根据其 ID 从集合中获取批注。

getItem(commentId: string): Excel.Comment;

参数

commentId

string

注释的标识符。

返回

注解

[ API 集:ExcelApi 1.10 ]

getItemAt(index)

根据其位置从集合中获取批注。

getItemAt(index: number): Excel.Comment;

参数

index

number

要检索的对象的索引值。 从零开始编制索引。

返回

注解

[ API 集:ExcelApi 1.10 ]

getItemByCell(cellAddress)

从指定单元格获取的批注。 如果单元格中没有注释,则会引发错误。

getItemByCell(cellAddress: Range | string): Excel.Comment;

参数

cellAddress

Excel.Range | string

注释所在的单元格。 这可以是 Range 对象或字符串。 如果是字符串,则必须包含完整地址,包括工作表名称。 InvalidArgument如果提供的范围大于一个单元格,则会引发错误。

返回

注解

[ API 集:ExcelApi 1.10 ]

getItemByReplyId(replyId)

获取给定答复所连接到的注释。

getItemByReplyId(replyId: string): Excel.Comment;

参数

replyId

string

批注回复的标识符。

返回

注解

[ API 集:ExcelApi 1.10 ]

getItemOrNullObject(commentId)

根据其 ID 从集合中获取批注。 如果注释对象不存在,则此方法返回一个对象,其 isNullObject 属性设置为 true。 有关详细信息,请参阅 *OrNullObject 方法和属性

getItemOrNullObject(commentId: string): Excel.Comment;

参数

commentId

string

注释的标识符。

返回

注解

[ API 集:ExcelApi 1.14 ]

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(options?: Excel.Interfaces.CommentCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.CommentCollection;

参数

options

Excel.Interfaces.CommentCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

提供要加载对象的属性的选项。

返回

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames?: string | string[]): Excel.CommentCollection;

参数

propertyNames

string | string[]

逗号分隔的字符串或指定要加载的属性的字符串数组。

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Excel.CommentCollection;

参数

propertyNamesAndPaths
OfficeExtension.LoadOption

propertyNamesAndPaths.select 是一个逗号分隔的字符串,指定要加载的属性,是 propertyNamesAndPaths.expand 一个逗号分隔的字符串,指定要加载的导航属性。

返回

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,反过来,调用toJSON传递给它的 对象的 方法。) 虽然原始Excel.CommentCollection对象是 API 对象,toJSON但该方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.CommentCollectionData) ,其中包含一个“items”数组,其中包含集合项中任何已加载属性的浅表副本。

toJSON(): Excel.Interfaces.CommentCollectionData;

返回

事件详细信息

onAdded

添加注释时发生。

readonly onAdded: OfficeExtension.EventHandlers<Excel.CommentAddedEventArgs>;

事件类型

注解

[ API 集:ExcelApi 1.12 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/events-comment-event-handler.yaml

await Excel.run(async (context) => {
    const comments = context.workbook.worksheets.getActiveWorksheet().comments;

    // Register the onAdded, onChanged, and onDeleted comment event handlers.
    comments.onAdded.add(commentAdded);
    comments.onChanged.add(commentChanged);
    comments.onDeleted.add(commentDeleted);

    await context.sync();

    console.log("Added event handlers for when comments are added, changed, or deleted.");
});

...

async function commentAdded(event: Excel.CommentAddedEventArgs) {
    // Retrieve the added comment using the comment ID.
    // Note: This function assumes only a single comment is added at a time. 
    await Excel.run(async (context) => {
        const addedComment = context.workbook.comments.getItem(event.commentDetails[0].commentId);

        // Load the added comment's data.
        addedComment.load(["content", "authorName", "creationDate"]);

        await context.sync();

        // Print out the added comment's data.
        console.log(`A comment was added:`);
        console.log(`    ID: ${event.commentDetails[0].commentId}`);
        console.log(`    Comment content:${addedComment.content}`);
        console.log(`    Comment author:${addedComment.authorName}`);
        console.log(`    Creation date:${addedComment.creationDate}`);
    });
}

onChanged

在批注集合中的批注或答复发生更改时发生,包括删除答复时。

readonly onChanged: OfficeExtension.EventHandlers<Excel.CommentChangedEventArgs>;

事件类型

注解

[ API 集:ExcelApi 1.12 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/events-comment-event-handler.yaml

await Excel.run(async (context) => {
    const comments = context.workbook.worksheets.getActiveWorksheet().comments;

    // Register the onAdded, onChanged, and onDeleted comment event handlers.
    comments.onAdded.add(commentAdded);
    comments.onChanged.add(commentChanged);
    comments.onDeleted.add(commentDeleted);

    await context.sync();

    console.log("Added event handlers for when comments are added, changed, or deleted.");
});

...

async function commentChanged(event: Excel.CommentChangedEventArgs) {
    // Retrieve the changed comment using the comment ID.
    // Note: This function assumes only a single comment is changed at a time. 
    await Excel.run(async (context) => {
        const changedComment = context.workbook.comments.getItem(event.commentDetails[0].commentId);

        // Load the changed comment's data.
        changedComment.load(["content", "authorName", "creationDate"]);

        await context.sync();

        // Print out the changed comment's data.
        console.log(`A comment was changed:`);
        console.log(`    ID: ${event.commentDetails[0].commentId}`);
        console.log(`    Updated comment content: ${changedComment.content}`);
        console.log(`    Comment author: ${changedComment.authorName}`);
        console.log(`    Creation date: ${changedComment.creationDate}`);
    });
}

onDeleted

在批注集合中删除批注时发生。

readonly onDeleted: OfficeExtension.EventHandlers<Excel.CommentDeletedEventArgs>;

事件类型

注解

[ API 集:ExcelApi 1.12 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/events-comment-event-handler.yaml

await Excel.run(async (context) => {
    const comments = context.workbook.worksheets.getActiveWorksheet().comments;

    // Register the onAdded, onChanged, and onDeleted comment event handlers.
    comments.onAdded.add(commentAdded);
    comments.onChanged.add(commentChanged);
    comments.onDeleted.add(commentDeleted);

    await context.sync();

    console.log("Added event handlers for when comments are added, changed, or deleted.");
});

...

async function commentDeleted(event: Excel.CommentDeletedEventArgs) {
    // Print out the deleted comment's ID.
    // Note: This function assumes only a single comment is deleted at a time. 
    await Excel.run(async (context) => {
        console.log(`A comment was deleted:`);
        console.log(`    ID: ${event.commentDetails[0].commentId}`);
    });
}