Excel.CommentCollection class

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

扩展

属性

context

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

items

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

方法

add(cellAddress, content, contentType)

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

add(cellAddress, content, contentType)

使用给定单元格上的给定内容创建新批注。 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”数组,其中包含从集合的 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 用于枚举 ContentType.Plain,1.11 用于枚举 ContentType.Mention]

返回

注解

API 集:ExcelApi 1.10

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/12-comments-and-notes/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, contentType)

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

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

参数

cellAddress

Excel.Range | string

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

content

Excel.CommentRichContent | string

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

contentType

"Plain" | "Mention"

可选。 批注中包含的内容的类型。 默认值为 enum ContentType.Plain。 [API 集:ExcelApi 1.10 用于枚举 ContentType.Plain,1.11 用于枚举 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”数组,其中包含从集合的 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}`);
    });
}