Skip to content
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

Add pivot table with limitations #2551

Merged
merged 4 commits into from
Oct 31, 2023
Merged

Add pivot table with limitations #2551

merged 4 commits into from
Oct 31, 2023

Conversation

mikez
Copy link
Contributor

@mikez mikez commented Oct 9, 2023

worksheet.addPivotTable(configuration);

Note: Pivot table support is in its early stages with certain limitations, including:

  • No support for reading xlsx documents with existing pivot tables (writing is supported).
  • Pivot table configurations must consist of one "value" item and use the sum metric.
  • Only one pivot table can be added for the entire document.

Example usage

const workbook = new Excel.Workbook();

const worksheet1 = workbook.addWorksheet('Sheet1');
worksheet1.addRows([
  ['A', 'B', 'C', 'D', 'E'],
  ['a1', 'b1', 'c1', 4, 5],
  ['a1', 'b2', 'c1', 4, 5],
  ['a2', 'b1', 'c2', 14, 24],
  ['a2', 'b2', 'c2', 24, 35],
  ['a3', 'b1', 'c3', 34, 45],
  ['a3', 'b2', 'c3', 44, 45],
]);

const worksheet2 = workbook.addWorksheet('Sheet2');
worksheet2.addPivotTable({
  // Source data: entire sheet range
  sourceSheet: worksheet1,
  // Pivot table fields: via header row in `worksheet1`
  rows: ['A', 'B'],
  columns: ['C'],
  values: ['E'], // Exactly 1 field
  metric: 'sum', // Metric: 'sum' only
});

lib/doc/worksheet.js Outdated Show resolved Hide resolved
Copy link
Member

@Siemienik Siemienik left a comment

Choose a reason for hiding this comment

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

Hello @mikez , Thank you for the latest changes, I'm really happy that Exceljs has possibility to gain new feature even with limitations.
I added few comments, I would be glad for answering that 😄

Good job and I love the direction when it comes. 🥇

Comment on lines +72 to +82
if (!model.rows.length) {
throw new Error('No pivot table rows specified.');
}

if (!model.columns.length) {
throw new Error('No pivot table columns specified.');
}

if (model.values.length !== 1) {
throw new Error('Exactly 1 value needs to be specified at this time.');
}
Copy link
Member

Choose a reason for hiding this comment

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

Would It be enabled to add partially configured pivot table without for instance rows set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At this time, the code doesn't support that. You need at least one item in each of {rows, columns, values}.
However, this could be a potential feature down the road if it is requested.

// { name: 'E', sharedItems: null }
// ]

const names = worksheet.getRow(1).values;
Copy link
Member

Choose a reason for hiding this comment

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

Does it always start from row 1?

Copy link
Contributor Author

@mikez mikez Oct 24, 2023

Choose a reason for hiding this comment

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

I made the simplifying assumption that the source data is represented by the entire sheet, with the first row representing the header. Do you think this assumption is too simplistic and won't capture 95% of the cases?

Comment on lines +817 to +824
this.pivotTables.push(pivotTable);
this.workbook.pivotTables.push(pivotTable);
Copy link
Member

Choose a reason for hiding this comment

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

What will happen if someone remove the pivot table from one of these places, or overwrite one of them?
What do you think about making wb.workbook.pivotTables a getter returning pivottables from all worksheets? Or .. a function?
Can it attach one pivotTable to multiple sheets?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What will happen if someone remove the pivot table from one of these places, or overwrite one of them?

@Siemienik It seems you're pointing at the mutability of worksheet.pivotTables and worksheet.workbook.pivotTables and concerns for that clients might mutate this. If you think this type of safety is needed and consistent with elsewhere in ExcelJS, we can definitely make it a getter. I'll take your advice here.

Can it attach one pivotTable to multiple sheets?

At this time, I intend for there to be at most one pivotTable in at most one sheet across the entire workbook. The user should not have to mutate worksheet.pivotTables or worksheet.workbook.pivotTables and only access worksheet.addPivotTable.

lib/doc/pivot-table.js Show resolved Hide resolved
lib/doc/pivot-table.js Show resolved Hide resolved
return `
<pivotField axis="${axis}" ${defaultAttributes}>
<items count="${sharedItems.length + 1}">
${[...range(0, sharedItems.length)].map(index => `<item x="${index}" />`).join('\n ')}
Copy link
Member

Choose a reason for hiding this comment

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

You may try this way:

Suggested change
${[...range(0, sharedItems.length)].map(index => `<item x="${index}" />`).join('\n ')}
${sharedItems.map((item, index) => `<item x="${index}" />`).join('\n ')}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clever. I like this. I will update and force push this.

@mikez
Copy link
Contributor Author

mikez commented Oct 24, 2023

@Siemienik: I added few comments, I would be glad for answering that 😄

Thank you. Please guide me where I might want to add clarifications to the documentation or comments to make it clearer for other readers.

```js
worksheet.addPivotTable(configuration);
```

**Note:** Pivot table support is in its early stages with certain limitations, including:

- Xlsx files with existing pivot tables can't be read (writing is supported).
- Pivot table configurations must have one "value"-item and use the "sum" metric.
- Only one pivot table can be added for the entire document.
Copy link
Member

@Siemienik Siemienik left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for booting up new feature into exceljs 👏

@Siemienik Siemienik merged commit ddab279 into exceljs:master Oct 31, 2023
27 checks passed
@mikez
Copy link
Contributor Author

mikez commented Oct 31, 2023

👉 Feedback and discussion here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants