Skip to content

Tables

Tables are objects in ws["!tables"].

Key Meaning
ref range of the table (string or range object). When it is left out, the table covers the whole sheet
name table name. One is generated when it is left out
header “table has headers”. 0 or false turns headers and the filter off
filter filter buttons. 0 or false turns them off
style table style options (see below)

With headers on, the first row gives the column names. Every name must be unique. A duplicate raises an error that names the cell to change, for example table columns 1 and 0 have the same header 'Item'; try setting cell B1 to 'Item2'.

style key Excel option
name table style such as "Medium9": "Light1" to "Light21", "Medium1" to "Medium28", "Dark1" to "Dark11"
rowstripe banded rows
colstripe banded columns
colfirst highlight the first column
collast highlight the last column

The default style is Medium9 with row stripes on and column stripes off.

const XLSX = require("@agent-sheet/wasm");
const ws = XLSX.utils.aoa_to_sheet([["Item", "Price"]]);
XLSX.utils.sheet_add_json(ws, [{ Item: "abc", Price: 1.23 }, { Item: "def", Price: 4.56 }], { origin: -1, skipHeader: true });
XLSX.utils.sheet_add_json(ws, [{ Item: "ghi", Price: 7.89 }], { origin: -1, skipHeader: true });
ws["!tables"] = [{ name: "MyTable", ref: "A1:B4", style: { name: "Medium1", rowstripe: true, colstripe: false } }];
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 });
const t = back.Sheets.S["!tables"][0];
console.log(t.name, t.ref, t.style.name, t.cols.map((c) => c.name));
Output
MyTable A1:B4 Medium1 [ 'Item', 'Price' ]
const XLSX = require("@agent-sheet/wasm");
const ws = XLSX.utils.aoa_to_sheet([["Item", "Item"], [1, 2]]);
ws["!tables"] = [{ name: "T", ref: "A1:B2" }];
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "S");
try {
XLSX.write(wb, { type: "buffer", bookType: "xlsx" });
} catch (e) {
console.log(String(e));
console.log(typeof e);
}
Output
table columns 1 and 0 have the same header 'Item'; try setting cell B1 to 'Item2'
string

This error is thrown as a plain string, not as an Error object. The original package does the same. See Error behavior.