Skip to content
Open
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
24 changes: 19 additions & 5 deletions src/nullxlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -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) {
Copy link
Collaborator

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.

Suggested change
addSheetFromData(data, name, column_alignments) {
addSheetFromData(data, name, options) {

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;
}
Expand All @@ -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',
Expand Down Expand Up @@ -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>`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could potentially lead to having two s attributes on a cell: s="1" s="4"
Did you test if that won't result in an invalid document?

Copy link
Author

Choose a reason for hiding this comment

The 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>`;
});
Expand Down