Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const fileAsUint8Array = await createXlsx({
})
```

Styles accept an `alignment` object, for example
`{ alignment: { vertical: "center", wrapText: true } }`.
Alignment is preserved when a cell also uses a date or datetime format.

## Contributing

Follow [Coding Style Guide](https://github.com/litejs/litejs/wiki/Style-Guide),
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ describe("xlsx", function() {
var { createFiles, createXlsx } = require("..")
, compressionSuported = typeof CompressionStream !== "undefined" && typeof Response !== "undefined"

test("cell alignment and formatted styles", function(assert) {
var files = createFiles({
styles: {
Wrapped: { font: { b: true }, alignment: { vertical: 'center', wrapText: true } },
Unwrapped: { alignment: { horizontal: 'right', wrapText: false } }
},
sheets: [[[
{ style: 'Wrapped', value: 'line one\nline two' },
{ style: 'Wrapped', format: 'date', value: new Date(1514900750001) },
{ style: 'Unwrapped', value: 10 }
]]]
})
, styles = files.find(f => f.name === 'xl/styles.xml').content
assert.equal((styles.match(/<alignment vertical="center" wrapText="true"\/>/g) || []).length, 2, 'alignment survives format merging')
assert.ok(styles.includes('applyAlignment="1"'), 'alignment enabled on cell style')
assert.ok(styles.includes('<alignment horizontal="right" wrapText="false"/>'), 'false remains explicit')
assert.ok(!styles.includes('alignment="'), 'alignment is a child, never an attribute')
assert.end()
})

function sheet1(data, sheet) {
return createFiles({ sheets: [{ data, ...sheet }] }).find(f => f.name === 'xl/worksheets/sheet1.xml').content
}
Expand Down
2 changes: 1 addition & 1 deletion test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createFiles, createXlsx, Workbook, XlsxFile } from ".."

var workbook: Workbook = {
styles: {
bold: { font: { sz: 11, name: "Calibri", b: true } },
bold: { font: { sz: 11, name: "Calibri", b: true }, alignment: { vertical: "center", wrapText: true } },
border: { border: "thin" },
fill: { fill: "FFFF00" },
},
Expand Down
11 changes: 11 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ export interface FillDef {
pattern?: string
}

export interface AlignmentDef {
horizontal?: "general" | "left" | "center" | "right" | "fill" | "justify" | "centerContinuous" | "distributed"
vertical?: "top" | "center" | "bottom" | "justify" | "distributed"
wrapText?: boolean
shrinkToFit?: boolean
textRotation?: number
indent?: number
readingOrder?: number
}

export interface StyleDef {
alignment?: AlignmentDef
font?: FontDef
border?: string | BorderSides
fill?: string | FillDef
Expand Down
8 changes: 7 additions & 1 deletion xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
bgColor: normalizeRgb(newFill.bgColor)
}
a[1] = xf.push({
alignment: a[1].alignment,
applyAlignment: a[1].alignment ? 1 : UNDEF,
fontId: newFont ? font.push(newFont) - 1 : 0,
applyFont: newFont ? 1 : UNDEF,
borderId: newBorder ? border.push(newBorder) - 1 : UNDEF,
Expand Down Expand Up @@ -190,7 +192,11 @@
(k, v) => toXml(k, { style: v && v.style || v }, v && v.color ? { color: [toColor(v.color)] } : 0)
) +
'<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>' +
toXml('cellXfs', { count: xf.length }, { xf }) +
toXml('cellXfs', { count: xf.length }, xf.map(
f => toXml('xf', assign({}, f, { alignment: UNDEF }), {
alignment: f.alignment ? [f.alignment] : UNDEF
})
).join('')) +
'<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>' +
'</styleSheet>'
},
Expand Down
Loading