Skip to content

Rich text and HTML

Give a cell an R array of run objects. Each run is a string cell with its own style. The writer joins the runs. When R is present it is used instead of v.

const XLSX = require("@agent-sheet/wasm");
const ws = XLSX.utils.aoa_to_sheet([["x"]]);
ws.A1.R = [
{ t: "s", v: "bold", s: { bold: true } },
{ t: "s", v: "italic", s: { italic: true } },
];
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "S");
const back = XLSX.read(XLSX.write(wb, { type: "buffer", bookType: "xlsx", cellStyles: true }), { cellStyles: true });
console.log(back.Sheets.S.A1.v, back.Sheets.S.A1.R.length);
Output
bolditalic 2

For XLS, pass bookSST: true when you write to preserve rich-text runs. For XLSB, the plain text survives a round trip, but rich-text runs written with bookSST: true do not come back intact; use XLSX when you need to keep formatting runs.

utils.html_to_rs(html) turns simple markup into a run array without a DOM. It recognizes <b> / <strong>, <i>, <u> and <s> formatting, but does not apply CSS.

const XLSX = require("@agent-sheet/wasm");
const ws = XLSX.utils.aoa_to_sheet([["x"]]);
ws.A1.R = XLSX.utils.html_to_rs("<b>bold</b><i>italic</i>");
console.log(JSON.stringify(ws.A1.R));
Output
[{"t":"s","v":"bold","s":{"bold":true}},{"t":"s","v":"italic","s":{"italic":true}}]

In a browser you can build richer runs with CSS. Put the HTML in a table cell and call utils.table_to_sheet on the table. See DOM table ingress.

Comments accept R too. See Comments.

template_set_aoa accepts R in a cell object. See Template: cells, styles and formulas.