Skip to content

Fix corrupted file with conditional formatting and hyperlinks #2803

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TheAsda
Copy link

@TheAsda TheAsda commented Aug 1, 2024

Summary

I faced the issue with stream writer creating corrupted file. Through some experiments and inspecting differences between the file created by the library and handmade file using Microsoft Excel I found out that the corruption appears when you use conditional formatting together with hyperlinks. The issue is that the order of blocks in the worksheet is wrong. This pull request solves this issue.

Test plan

First we create file using Microsoft Excel with links and some conditional formatting eg. =MOD(ROW();2)=0 to color every odd row:
image
Then we inspect the file and see that the order in sheet is: first conditional formatting, second hyperlinks
image
After that we create similar file using the library:

const ExcelJS = require("exceljs");
const fs = require("fs");

const fileStream = fs.createWriteStream("output.xlsx");

const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
  stream: fileStream,
  useSharedStrings: false,
  useStyles: true,
});
const worksheet = workbook.addWorksheet("Sheet1");
for (let index = 0; index < 4; index++) {
  worksheet.addRow([
    "test",
    {
      text: "Link",
      hyperlink: "https://google.com",
      tooltip: "https://google.com",
    },
  ]);
}

worksheet.addConditionalFormatting({
  ref: `A1:B4`,
  rules: [
    {
      priority: 1,
      type: "expression",
      formulae: ["MOD(ROW(),2)=0"],
      style: {
        fill: {
          type: "pattern",
          pattern: "solid",
          bgColor: "FFEDEDED",
        },
      },
    },
  ],
});

worksheet.commit();
workbook.commit();

If we open the resulting file with Excel we see the error:

image

If we inspect the underlying xml file we see that the order is different:

image

So then we can change the order in worksheet-writer.js and try the script again:

image

The file generates successfully and opens in Excel:

image

@J43fura
Copy link

J43fura commented Mar 24, 2025

I see this PR and many others are not merged,
Did you fork this repo or migrate to another package? If so what did you choose to use?

@TheAsda
Copy link
Author

TheAsda commented Mar 25, 2025

We used yarn patch functionality

@J43fura
Copy link

J43fura commented Mar 25, 2025

Thank you

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

Successfully merging this pull request may close these issues.

3 participants