SheetJS / sheetjs

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
https://sheetjs.com/
Apache License 2.0
34.99k stars 8k forks source link

Newline characters in cells of written workbooks #108

Closed notatestuser closed 4 years ago

notatestuser commented 10 years ago

When values v of cells contain unix line endings \n they do not become line breaks in the corresponding cells of the written workbook. However, when the Windows carriage return newline combos \r\n are used Excel appears to properly break the lines when a cell containing them is double clicked (edited).

screen shot 2014-09-11 at 11 44 40

So there appear to be two things to potentially address here:

  1. Unix line endings \n do not become new lines at all in the written file
  2. Windows line endings \r\n only become newlines in Excel when the cell is edited. This may be a shortfall in Excel for Mac 2011 and may not apply to the Windows version.
SheetJSDev commented 10 years ago

Try writing with the shared string table (bookSST:true option when you write).

I explored Excel 2011 a bit and found:

1) Shared strings table parser apparently treat both forms of newline (literal \n as well as the escaped x000d) as new lines

2) Inline strings parser ignores the literal newline and only process the x000d encoded newline

3) Shared strings parser assumes whitespace is preserved and inline strings parser assumes whitespace is not preserved. The xml:space="preserve" attribute is completely ignored in both cases.

You'd think that Excel would use one unified XML parser, but it appears that is not the case :/ The fix here is straightforward, but the question still remains: should the writer handle both types of newline (\n and \r\n)? A literal encoding of both in the shared string table is treated as two newlines, not one, so there is some ambiguity there

tophsic commented 7 years ago

Using booSST:true seemed to work for me when opening xlsx file on Linux and LibreOffice but didn't work on Excel on Win 10.

reviewher commented 7 years ago

@tophsic @dferer can you share either a code sample or a bad file generated by the library?

lumio commented 7 years ago

@reviewher here is a code example.

It works fine with macOS numbers. I didn't test it with LibreOffice yet. But Excel 15.17 under macOS ignores the line breaks at first, but adds them as soon as you double click on the cell.

qqilihq commented 5 years ago

Did someone find a workaround? We the same issue on Excel macOS and Excel Windows -- text is shown in a single line and changes on double click.

Apple Numbers shows the file correctly.

zaddok commented 5 years ago

I have the same problem. Did anyone find a workaround? \n and \r\n don't actually break in Excel on Mac at least.

fozcode commented 4 years ago

I could only get this working correctly using the Pro edition where the saving of cell styles is supported. It needed: \n in the string value, bookSST: true, cellStyles: true in XLSX.write, and cell.s = { alignment: { wrapText: true } } in the cell style.

SheetJSDev commented 4 years ago

To summarize: merely placing a newline character is insufficient. Excel requires the text wrapping style attribute to be applied to the cell, otherwise it is rendered like a whitespace character. This is a part of our Pro builds.

@fozcode I don't believe bookSST: true is required, if you have a bad case email us.

fozcode commented 4 years ago

@SheetJSDev I've tested again and you are correct, bookSST: true is not required for Excel.

In LibreOffice a multi-line string is displayed on one line unless you set bookSST: true, which I guess is a LibreOffice bug. When you click into the cell the lines are then shown correctly.

jefleponot commented 4 years ago

it seems to not be correct tag in xlxs.js file :line 19974 .replace(/\n/g, "\<text:line-break/>") instead of .replace(/\n/g, "</text:p>\<text:p>")

SheetJSDev commented 4 years ago

@jefleponot Reviewing the spec http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html

<table:table-cell> (9.1.4) has <text:p> (5.1.3) children

<text:line-break> (6.1.5) is a valid child of <text:p>, which was originally interpreted to be a newline character.

You're proposing we create separate paragraphs for each logical line, which is fine but I'm curious why the line break doesn't work

jefleponot commented 4 years ago

Could I propose a PR ? Could you accept it ?

jefleponot commented 4 years ago

https://bugs.documentfoundation.org/show_bug.cgi?id=43107

lukaselmer commented 3 years ago

To summarize: merely placing a newline character is insufficient. Excel requires the text wrapping style attribute to be applied to the cell, otherwise it is rendered like a whitespace character. This is a part of our Pro builds.

@fozcode I don't believe bookSST: true is required, if you have a bad case email us.

@SheetJSDev is there a solution for the community edition?

GusMartins499 commented 3 years ago

a solution for the commu

same issue

jdegger commented 1 year ago

Anybody ever found a solution for community edition? We can not access the pro edition because we have not received replies yet when we reach out...

binaryn3xus commented 11 months ago

Why is something as simple as a newline behind a paywall (Pro)?

kentmor commented 10 months ago

Same issue

KrunchMuffin commented 7 months ago

😞

vfa-tanna commented 2 months ago

Add more VBA Code to resolve this wsSrc.Cells.WrapText = True