Skip to content

fix issue #204 sets default column width #1160

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

Merged
merged 1 commit into from
Mar 31, 2020
Merged
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
8 changes: 5 additions & 3 deletions lib/doc/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const _ = require('../utils/under-dash');
const Enums = require('./enums');
const colCache = require('../utils/col-cache');

const DEFAULT_COLUMN_WIDTH = 9;
Copy link
Member

Choose a reason for hiding this comment

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

I'm really happy to read your pull requests, good job. I had some issues with width before, so I feel twice satisfaction with your changes 👍
but I'm not sure, that it's the correct way to set a const value,
I think that should be property for worksheet and/or workbook, with a possibility to change it by a user.

May I'm wrong, so please clear me this and tell what are you thinking? 😄

Copy link
Member

@Siemienik Siemienik Mar 22, 2020

Choose a reason for hiding this comment

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

Even if I wrote a comment, I think these PR fix some issues anyway, so I approve it to merge 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

@Siemienik First of all, thank you very much for your approval, this will become my motivation. I really like exceljs, it is very helpful for my work, so I am happy to contribute a little bit of my own power to this software.

  • When the user does not set the column width, I will set a default value for the column.
// single column setting
 sheet.getColumn(3).width = 10;
  • Users can globally set the column width of the entire worksheet
worksheet.properties.defaultColWidth= 47;


// Column defines the column properties for 1 column.
// This includes header rows, widths, key, (style), etc.
// Worksheet will condense the columns as appropriate during serialization
Expand All @@ -31,7 +33,7 @@ class Column {
}

get isCustomWidth() {
return this.width !== undefined && this.width !== 8;
return this.width !== undefined && this.width !== DEFAULT_COLUMN_WIDTH;
}

get defn() {
Expand All @@ -48,7 +50,7 @@ class Column {
set defn(value) {
if (value) {
this.key = value.key;
this.width = value.width;
this.width = value.width !== undefined ? value.width : DEFAULT_COLUMN_WIDTH;
this.outlineLevel = value.outlineLevel;
if (value.style) {
this.style = value.style;
Expand Down Expand Up @@ -267,7 +269,7 @@ class Column {
col = {
min: index + 1,
max: index + 1,
width: column.width,
width: column.width !== undefined ? column.width : DEFAULT_COLUMN_WIDTH,
style: column.style,
isCustomWidth: column.isCustomWidth,
hidden: column.hidden,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('WorkbookWriter', () => {
expect(ws2.getColumn(2).alignment).to.deep.equal(
testUtils.styles.namedAlignments.middleCentre
);
expect(ws2.getColumn(2).width).to.equal(undefined);
expect(ws2.getColumn(2).width).to.equal(9);

expect(ws2.getColumn(4).width).to.equal(undefined);

Expand Down
29 changes: 29 additions & 0 deletions spec/unit/doc/column.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,33 @@ describe('Column', () => {
expect(sheet.getCell(11, 1).value).to.equal(11);
expect(sheet.getCell(12, 1).value).to.equal(null);
});
it('sets default column width', () => {
const sheet = createSheetMock();

sheet.addColumn(1, {
header: 'Col 1',
key: 'id1',
style: {
numFmt: '0.00%',
},
});
sheet.addColumn(2, {
header: 'Col 2',
key: 'id2',
style: {
numFmt: '0.00%',
},
width: 10,
});
sheet.getColumn(3).numFmt = '0.00%';

const model = Column.toModel(sheet.columns);
expect(model.length).to.equal(3);

expect(model[0].width).to.equal(9);

expect(model[1].width).to.equal(10);

expect(model[2].width).to.equal(9);
});
});