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

How to write merged Cells #92

Closed
longweiquan opened this issue Jul 30, 2014 · 6 comments
Closed

How to write merged Cells #92

longweiquan opened this issue Jul 30, 2014 · 6 comments

Comments

@longweiquan
Copy link

First of all, thank you for sharing this great library. I have a question about how to create the merged cells while writing files. I found the attribute Sheets['!merges'] while reading the files with cells, the same attributes don't work for writing.

@SheetJSDev
Copy link
Contributor

@longweiquan thanks for reaching out!

Right now merged cells are not being written. I would accept a contribution :) The code would look like this:

function write_ws_xml_merges(merges) {
    if(merges.length == 0) return "";
    var o = '<mergeCells count="' + merges.length + '">';
    for(var i = 0; i != merges.length; ++i) o += '<mergeCell ref="' + encode_range(merges[i]) + '"/>'
    return o + '</mergeCells>';
}
/* Add this line in write_ws_xml, just before if(o.length>2) { o[o.length] = ('</worksheet>'); o[1]=o[1].replace("/>",">"); } */
    if(ws['!merges'] !== undefined && ws['!merges'].length > 0) o[o.length] = (write_ws_xml_merges(ws['!merges']));

I have not added it yet because, to be honest, I'm not particularly happy with the current representation of merged cells. It's strange because certain operations are not well-defined (for example, if you merge A1:B2, what should happen if you try to access cell A2, B1, or B2?).

@clayzermk1 @mchapman any thoughts on the issue of merged cells representation?

@longweiquan
Copy link
Author

@SheetJSDev, thank you for your rapid reply.

Suggestion, maybe you can keep the same behavior as Excel do. When cells are merged in Excel, only the top-left cell is left and the others are deleted.

This concern the manipulation of the Sheets object, maybe you can even write some utils for merging cells to avoid wrong usage.

@clayzermk1
Copy link

@SheetJSDev I think what @longweiquan said about being consistent with Excel is a rational thing to do. Shouldn't be that bad to check for an existing merge range in a proposed merge range, remove the existing range, and then create the proposed range (did I interpret that correctly?).

@longweiquan
Copy link
Author

@clayzermk1, thank you for the interpretation :), that's exactly what I mean.

pridkett added a commit to pridkett/js-xlsx that referenced this issue Aug 5, 2014
This is the proposed solution from @SheetJSDev to write merged cells. It seems
to work well enough for what I need.

see: SheetJS#92

Signed-off-by: Patrick Wagstrom <patrick@wagstrom.net>
@Suncatcher
Copy link

So the merging is still not possible?

@reviewher
Copy link
Contributor

Merging was added in 0.7.10. #416 (comment) has an example in nodejs. https://jsfiddle.net/w8t0g8xn/ is a fiddle

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

5 participants