XLSX
Comments
Add comments and notes to cells
Adding Comments
Add comments (legacy notes in Excel) to cells in a worksheet:
{
"worksheets": [
{
"name": "With Comments",
"rows": [
{ "cells": [{ "value": "Product" }, { "value": "Price" }, { "value": "Stock" }] },
{ "cells": [{ "value": "Widget A" }, { "value": 9.99 }, { "value": 150 }] },
{ "cells": [{ "value": "Widget B" }, { "value": 14.99 }, { "value": 75 }] },
{ "cells": [{ "value": "Widget C" }, { "value": 24.99 }, { "value": 0 }] }
],
"comments": [
{ "cell": "B2", "author": "Alice", "text": "Discounted from 12.99" },
{ "cell": "C3", "author": "Bob", "text": "Reorder soon" },
{ "cell": "C4", "author": "Alice", "text": "Out of stock!" }
]
}
]
}
import { generateWorkbook } from "@office-open/xlsx";
const buffer = await generateWorkbook({
worksheets: [
{
name: "With Comments",
rows: [
{ cells: [{ value: "Product" }, { value: "Price" }, { value: "Stock" }] },
{ cells: [{ value: "Widget A" }, { value: 9.99 }, { value: 150 }] },
{ cells: [{ value: "Widget B" }, { value: 14.99 }, { value: 75 }] },
{ cells: [{ value: "Widget C" }, { value: 24.99 }, { value: 0 }] },
],
comments: [
{ cell: "B2", author: "Alice", text: "Discounted from 12.99" },
{ cell: "C3", author: "Bob", text: "Reorder soon" },
{ cell: "C4", author: "Alice", text: "Out of stock!" },
],
},
],
});
Comment Options Reference
| Option | Type | Description |
|---|---|---|
cell | string | Cell reference, e.g. "B2" |
author | string | Comment author |
text | string or object | Plain text string, or rich text object with runs |
commentPr | object | Comment properties (anchor, locking, alignment) |
Rich Text Comments
Comments support rich text formatting using the runs property. Each run has a text string and optional properties for formatting:
{
"comments": [
{
"cell": "B2",
"author": "Alice",
"text": {
"runs": [
{
"text": "Note: ",
"properties": {
"bold": true
}
},
{
"text": "Premium grade material"
}
]
}
},
{
"cell": "B3",
"author": "Bob",
"text": {
"runs": [
{
"text": "Best seller! ",
"properties": {
"color": "008000",
"bold": true
}
},
{
"text": "Consider bulk discount."
}
]
}
}
]
}
Rich text run properties
| Property | Type | Description |
|---|---|---|
font | string | Font name |
charset | number | Character set |
family | number | Font family |
bold | boolean | Bold text |
italic | boolean | Italic text |
strike | boolean | Strikethrough |
outline | boolean | Outline text |
shadow | boolean | Shadow text |
condense | boolean | Condense text |
extend | boolean | Extend text |
color | string | Text color as hex, e.g. "FF0000" |
size | number | Font size in points |
underline | "single" | "double" | "singleAccounting" | "doubleAccounting" | "none" | Underline style |
vertAlign | "superscript" | "subscript" | "baseline" | Vertical alignment |
scheme | "major" | "minor" | "none" | Font scheme |
Comment properties (commentPr)
Comments can include anchor and locking settings via commentPr:
{
"comments": [
{
"cell": "B2",
"author": "Alice",
"text": {
"runs": [
{
"text": "Note: ",
"properties": {
"bold": true
}
},
{
"text": "Premium grade material"
}
]
},
"commentPr": {
"locked": false,
"autoFill": true,
"anchor": {
"moveWithCells": true,
"sizeWithCells": false
}
}
}
]
}
CommentPr options
| Property | Type | Description |
|---|---|---|
locked | boolean | Locked |
defaultSize | boolean | Default size |
print | boolean | Print with sheet |
disabled | boolean | Disabled |
autoFill | boolean | Auto fill |
autoLine | boolean | Auto line |
altText | string | Alt text |
textHAlign | "left" | "center" | "right" | "justify" | "distributed" | Text horizontal alignment |
textVAlign | "top" | "center" | "bottom" | "justify" | "distributed" | Text vertical alignment |
lockText | boolean | Lock text |
justLastX | boolean | Justify last line |
autoScale | boolean | Auto scale |
anchor | ObjectAnchorOptions | Object anchor position |