# Notes and hyperlinks

> Cell notes with a red corner marker and hover popup, and hyperlinks to web pages, email addresses and other cells, with keyboard and screen reader support.

## Notes

A note is a short comment attached to a cell. Cells with a note show a small red triangle in their top corner.

```ts
sheet.setNote('E14', { text: 'Includes the one-off Henley renewal.', author: 'Maya Patel' });
sheet.setNote('E15', 'Awaiting the September invoice.');
sheet.getNote('E14'); // { text: 'Includes the one-off Henley renewal.', author: 'Maya Patel' }
sheet.setNote('E15', null);
```

**`CellNote`** (interface from `@birchui/spreadsheet`, full reference: https://docs.birchui.com/api/spreadsheet.md)

```ts
interface CellNote
```

| Property | Type | Description |
| --- | --- | --- |
| `text` | `string` |  |
| `author?` | `string` |  |
| `visible?` | `boolean` |  |
| `width?` | `number` |  |
| `height?` | `number` |  |

- Hover over a cell to see its note in a popup.
- With the keyboard, move to the cell: the note appears beside it, and screen readers read it as the cell's description.
- `visible: true` keeps a note on screen all the time, like Excel's "Show note".
- `width` and `height` set the popup size in pixels.

`getNotes(sheet?)` returns all notes of a sheet keyed by address. Notes move with their cells when rows or columns are inserted or deleted, are undoable, and round-trip to xlsx as Excel notes.

## Threaded comments

Comments are conversations attached to a cell: a first message, replies, authors, dates and a resolved state. They are separate from notes, which are a single block of text.

```ts
const sheet = createSpreadsheet(host, { userName: 'Dana Whitfield' });

sheet.setComment('E2', 'Is this net of the January rebate?');
sheet.addCommentReply('E2', 'Yes, the rebate is already deducted.');
sheet.resolveComment('E2');
sheet.resolveComment('E2', false);
sheet.getComments();
sheet.setComment('E2', null);
```

**`CommentThread`** (interface from `@birchui/spreadsheet`, full reference: https://docs.birchui.com/api/spreadsheet.md)

```ts
interface CommentThread extends CommentReply
```

| Property | Type | Description |
| --- | --- | --- |
| `resolved?` | `boolean` |  |
| `replies?` | `CommentReply[]` |  |
| `text` | `string` | Inherited from CommentReply. |
| `author?` | `string \| undefined` | Inherited from CommentReply. |
| `date?` | `string \| undefined` | Inherited from CommentReply. |
| `mentions?` | `CommentMention[] \| undefined` | Inherited from CommentReply. |

Text passed as a string is stamped with the `userName` option and the current time. Pass a full `{ text, author, date, replies, resolved }` object to store a thread as it is, for example when loading comments from your own backend. Dates are ISO 8601 strings.

A commented cell shows a purple corner marker, faded when the thread is resolved. Clicking the marker, **Review › Show comments**, the cell menu or `openDialog('comments')` opens the **Comments** panel beside the grid. It lists every thread in the workbook with its replies; from there you can jump to the cell, reply (`Ctrl` + `Enter` posts), resolve or reopen, and delete a thread. `Ctrl` + `Alt` + `M` starts a new thread on the active cell.

Comments are saved in the sheet's `comments` map in the workbook JSON, are undoable, and are locked in `readOnly` mode and on a protected sheet unless **Edit objects** is allowed. The notes list is a separate panel: `openDialog('notes')`.

### Mentions

Pass `mentions.search` and typing `@` in a comment or a reply opens a list of people. The function receives the text typed after the `@` and returns up to eight people, at once or as a promise. The arrow keys move through the list, `Enter` or `Tab` inserts the name, `Escape` closes the list.

```ts
const sheet = createSpreadsheet(host, {
  userName: 'Ana Souza',
  mentions: {
    search: (query) => directory.filter((person) => person.name.toLowerCase().startsWith(query.toLowerCase())),
    onMention: ({ id, name, sheet, address, text, author }) => notify(id, `${author} mentioned you on ${sheet}!${address}`),
  },
});
```

A person is `{ id, name, detail? }`; `detail` is shown beside the name in the list. When the comment is posted, each mention is stored on the comment as `{ id, name, start, length }`, `onMention` is called and a `mention` event fires, once per mention. A name that was edited away before posting is not a mention. Sending the notification is up to you: the component contacts nobody.

In xlsx a mention is written as a `mention` element of the threaded comment, and the person list carries your `id` as the person's `userId`, so the ids come back when the file is opened again. This follows the published format and re-imports identically here; it has not been opened in Excel, which in any case does not know your ids and cannot notify anyone from them.

## Hyperlinks

```ts
sheet.setHyperlink('A2', 'https://northwind.example.com/accounts/4471');
sheet.setHyperlink('A3', 'mailto:accounts@northwind.example.com');
sheet.setHyperlink('A4', { location: "'Q3 Budget'!E5", tooltip: 'Go to the Q3 total' });
```

**`Hyperlink`** (interface from `@birchui/spreadsheet`, full reference: https://docs.birchui.com/api/spreadsheet.md)

```ts
interface Hyperlink
```

| Property | Type | Description |
| --- | --- | --- |
| `url?` | `string` |  |
| `location?` | `string` |  |
| `tooltip?` | `string` |  |

A string starting with `http:`, `https:`, `mailto:`, `tel:` or `ftp:` becomes a `url`; `www.` addresses get `https://`, bare email addresses get `mailto:`, and anything else, or text starting with `#`, is a `location` inside the workbook: a cell, a range or a defined name. Other schemes, such as `javascript:`, are refused with a console warning. An empty cell gets the link text as its value.

Linked cells are drawn in the link colour and underlined, unless the cell's own style sets a colour or `underline: false`. Their tooltip shows `tooltip` or the target, and screen readers hear "link to …" after the value.

`Ctrl`-click (`Cmd`-click on macOS) follows a link; a plain click selects the cell, so linked cells stay easy to edit. `followHyperlink(address)` does the same in code. Locations select the target cell, switching sheets if needed. URLs open in a new tab with `noopener`.

Every follow first fires `hyperlinkClick` with `{ sheet, address, url?, location?, preventDefault() }`. Call `preventDefault()` to handle the link yourself, for example to route it inside a single-page app:

```ts
sheet.on('hyperlinkClick', (event) => {
  if (event.url?.startsWith('https://northwind.example.com/')) {
    event.preventDefault();
    router.push(new URL(event.url).pathname);
  }
});
```

`getHyperlink(address)` reads a link and `setHyperlink(address, null)` removes it. Links move with their cells and round-trip to xlsx.
