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

[BUG] Table creation accepts empty row array, resulting in corrupted Excel file #2678

Open
georgbuehler opened this issue Feb 5, 2024 · 0 comments

Comments

@georgbuehler
Copy link

🐛 Bug Report

When creating a table, it's possible to define the rows array, but still leave it empty. ExcelJS will not warn that this will be an issue, but it will result in the Excel file being corrupted. One changed line of code in the validate() method in the Table class should resolve this issue.

Lib version: 9.4.2

Steps To Reproduce

  1. Run the Jest unit test (below) to a project with exceljs installed.
  2. Open the generated EmptyRowsTableTest.xlsx file.
  3. User will receive the following error message: "We found a problem with some content in 'BadTablenameTest.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes."
  4. If you click "Yes" for the repairs, Excel will present the message: "Repaired Records: Table from /xl/tables/table1.xml part (Table)". However, the table will not be created in the file.
import Excel from "exceljs";
import path from "path";

describe("addTable()", () => {
  it("should not allow an empty rows array", async () => {
    const excelDoc = new Excel.Workbook();
    const accountSheet = excelDoc.addWorksheet("Accounts");

    accountSheet.addTable({
      name: "Bob_s_Accounts",
      ref: "A1",
      headerRow: true,
      totalsRow: false,
      style: {
        theme: "TableStyleMedium2",
        showRowStripes: true,
      },
      columns: [
        { name: "Name", filterButton: true },
        { name: "Description", filterButton: true },
        { name: "Credit Limit", filterButton: true },
        { name: "Account Type", filterButton: true },
      ],
      rows: [], // EMPTY ROWS ARRAY -- currently this is permitted, but it will corrupt the Excel file
    });
    expect(excelDoc).toBeInstanceOf(Excel.Workbook);
    await excelDoc.xlsx.writeFile(
      path.join(__dirname, "../../data/EmptyRowsTableTest.xlsx")
    );
  });
});

The expected behaviour:

When the developer tries to create a Table without populating the rows array, a runtime error should result: "Table must have row definitions."

Possible solution (optional, but very helpful):

Update the validate() function to check that the rows array is not only defined by also populated:

assert(table.rows && table.rows.length > 0, 'Table must have row definitions');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant