[GH-ISSUE #281] worksheet_merge_range might produce a corrupt file #223

Closed
opened 2026-05-05 11:59:00 -06:00 by gitea-mirror · 4 comments
Owner

Originally created by @znakeeye on GitHub (Apr 1, 2020).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/281

Originally assigned to: @jmcnamara on GitHub.

I want to create this:

image

So I tried this:

#include "xlsxwriter.h"

int main()
{
    auto wb = workbook_new_opt("foo.xlsx");
    auto ws = workbook_add_worksheet(wb, "Sheet1");

    int row = 0;
    worksheet_merge_range(ws, row, 0, row, 2, "AB", NULL);
    row++;
    worksheet_merge_range(ws, row, 0, row, 1, "A", NULL);
    worksheet_merge_range(ws, row, 1, row, 2, "B", NULL);

    workbook_close(wb);
    
    return 0;
}

Then when opening the file Excel complains. Shouldn't libxlsxwriter be able to detect this error?

It seems impossible to create this cell pattern using the merge API. Ideas?

Originally created by @znakeeye on GitHub (Apr 1, 2020). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/281 Originally assigned to: @jmcnamara on GitHub. I want to create this: ![image](https://user-images.githubusercontent.com/958469/78145430-29b26c80-7431-11ea-8558-9b408ab2f9da.png) So I tried this: ```c #include "xlsxwriter.h" int main() { auto wb = workbook_new_opt("foo.xlsx"); auto ws = workbook_add_worksheet(wb, "Sheet1"); int row = 0; worksheet_merge_range(ws, row, 0, row, 2, "AB", NULL); row++; worksheet_merge_range(ws, row, 0, row, 1, "A", NULL); worksheet_merge_range(ws, row, 1, row, 2, "B", NULL); workbook_close(wb); return 0; } ``` Then when opening the file Excel complains. Shouldn't `libxlsxwriter` be able to detect this error? It seems impossible to create this cell pattern using the merge API. Ideas?
gitea-mirror 2026-05-05 11:59:00 -06:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@znakeeye commented on GitHub (Apr 1, 2020):

I can get the pattern if I change the last merge call to:

worksheet_write_string(ws, row, 2, "B", NULL);

Do you think it is up to the developer to handle this? Or should the merge method have a special case where it internally calls worksheet_write_string(self, first_row, last_col, string, format) for first_row == last_row?

<!-- gh-comment-id:607272081 --> @znakeeye commented on GitHub (Apr 1, 2020): I can get the pattern if I change the last merge call to: ``` worksheet_write_string(ws, row, 2, "B", NULL); ``` Do you think it is up to the developer to handle this? Or should the merge method have a special case where it internally calls `worksheet_write_string(self, first_row, last_col, string, format)` for `first_row == last_row`?
Author
Owner

@znakeeye commented on GitHub (Apr 1, 2020):

The more I think of it, I believe that worksheet_merge_range should return some new error code for this overlapping issue. That way we can take action should the error occur.

It's very hard to detect this condition before calling the method. You would have to maintain a dictionary of added merge ranges and check if the new merge would overlap.

<!-- gh-comment-id:607278916 --> @znakeeye commented on GitHub (Apr 1, 2020): The more I think of it, I believe that `worksheet_merge_range` should return some new error code for this overlapping issue. That way we can take action should the error occur. It's very hard to detect this condition before calling the method. You would have to maintain a dictionary of added merge ranges and check if the new merge would overlap.
Author
Owner

@znakeeye commented on GitHub (Apr 1, 2020):

I guess overlap-detection would be quite costly? Certainly don't want to reduce performance.

<!-- gh-comment-id:607344739 --> @znakeeye commented on GitHub (Apr 1, 2020): I guess overlap-detection would be quite costly? Certainly don't want to reduce performance.
Author
Owner

@jmcnamara commented on GitHub (Apr 1, 2020):

I think you worked out all the answers here yourself:

  • Overlapping merge ranges causes a corrupt file warning.
  • Thus you should avoid doing that.
  • The library should warn or prevent the user from doing that.
  • But it is expensive and tricky so currently it doesn't.
<!-- gh-comment-id:607528111 --> @jmcnamara commented on GitHub (Apr 1, 2020): I think you worked out all the answers here yourself: - Overlapping merge ranges causes a corrupt file warning. - Thus you should avoid doing that. - The library should warn or prevent the user from doing that. - But it is expensive and tricky so currently it doesn't.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#223
No description provided.