-
Notifications
You must be signed in to change notification settings - Fork 8
Add column_alignments to addSheetFromData #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ export class NullXlsx extends NullDownloader { | |
| /** | ||
| * Creates a new xlsx file | ||
| * @param {string} filename Name of file once generated | ||
| * @param {Object} options Settings | ||
| * @param {{ frozen?: boolean, filter?: boolean }} [options] Settings | ||
| */ | ||
| constructor(filename, options) { | ||
| super(filename, `${ baseContentType }.sheet`); | ||
|
|
@@ -33,14 +33,16 @@ export class NullXlsx extends NullDownloader { | |
| * Create a spreadsheet from an array of arrays of data | ||
| * @param {Array<Array<*>>} data Cell values | ||
| * @param {string} name Name of sheet | ||
| * @param {{[key: number]: 'left' | 'center' | 'right'}} [column_alignments] Column alignment settings | ||
| * @return {NullXlsx} Returns itself for method chaining | ||
| */ | ||
| addSheetFromData(data, name) { | ||
| addSheetFromData(data, name, column_alignments) { | ||
| const i = this.sheets.length + 1; | ||
| this.sheets.push({ | ||
| id: i, | ||
| name: this.escapeXml(name || 'Sheet' + i), | ||
| data | ||
| data, | ||
| column_alignments: column_alignments || {} | ||
| }); | ||
| return this; | ||
| } | ||
|
|
@@ -63,6 +65,8 @@ export class NullXlsx extends NullDownloader { | |
| + '<xf borderId="0" fillId="0" fontId="1" numFmtId="0" xfId="0" applyAlignment="1" applyFont="1"><alignment/></xf>' | ||
| + '<xf borderId="0" fillId="0" fontId="0" numFmtId="164" xfId="0" applyAlignment="1" applyFont="1" applyNumberFormat="1"><alignment /></xf>' | ||
| + '<xf borderId="0" fillId="0" fontId="0" numFmtId="165" xfId="0" applyAlignment="1" applyFont="1" applyNumberFormat="1"><alignment /></xf>' | ||
| + '<xf borderId="0" fillId="0" fontId="0" numFmtId="0" xfId="0" applyAlignment="1" applyFont="1"><alignment horizontal="right"/></xf>' | ||
| + '<xf borderId="0" fillId="0" fontId="0" numFmtId="0" xfId="0" applyAlignment="1" applyFont="1"><alignment horizontal="center"/></xf>' | ||
| + '</cellXfs><cellStyles count="1"><cellStyle xfId="0" name="Normal" builtinId="0"/></cellStyles><dxfs count="0"/></styleSheet>' | ||
| }, { | ||
| name: 'xl/sharedStrings.xml', | ||
|
|
@@ -101,14 +105,24 @@ export class NullXlsx extends NullDownloader { | |
| } | ||
| const cells = row.map((cell, cellIndex) => { | ||
| const cellName = this.colName(cellIndex) + (rowIndex + 1); | ||
| const alignment = sheet.column_alignments[cellIndex]; | ||
| let alignment_style = ''; | ||
|
|
||
| if (alignment === 'right') { | ||
| alignment_style = ' s="4"'; | ||
| } else if (alignment === 'center') { | ||
| alignment_style = ' s="5"'; | ||
| } | ||
| // left alignment uses default style (no explicit style needed) | ||
|
|
||
| if (typeof cell === 'number') { | ||
| return `<c r="${ cellName }"${ style }><v>${ cell }</v></c>`; | ||
| return `<c r="${ cellName }"${ style }${ alignment_style }><v>${ cell }</v></c>`; | ||
| } | ||
| if (cell instanceof Date) { | ||
| const dateStyle = cell.getHours() || cell.getMinutes() || cell.getSeconds() ? 3 : 2; | ||
| return `<c s="${ dateStyle }"><v>${ this.dateToExcelDate(cell) }</v></c>`; | ||
| } | ||
| return `<c t="inlineStr"${ style }><is><t>${ this.escapeXml(cell.toString()) }</t></is></c>`; | ||
| return `<c t="inlineStr"${ style }${ alignment_style }><is><t>${ this.escapeXml(cell.toString()) }</t></is></c>`; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could potentially lead to having two
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me. No error reports from users yet 🤷♂️ But I really have no clue what I'm doing and I don't trust that I'm doing anything the right way here - only that it works for me. |
||
| }); | ||
| return `<row r="${ rowIndex + 1 }">${ cells.join('') }</row>`; | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to send in column_alignments as part of an options object, to allow room for any additional options.