question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New line in richText cell

See original GitHub issue

Take the following code:

let excel = require('exceljs')
let wb = new excel.Workbook()
let ws = wb.addWorksheet('test')

ws.getCell('B2').value = {
  richText: [
    {
      font: {
        color: {
          argb: `99E391`
        }
      },
      text: `Chuck Norris' tears cure cancer,\n`
    },
    {
      font: {
        color: {
          argb: `FF6565`
        }
      },
      text: `too bad Chuck Norris has never cries.`
    }
  ]
}

wb.xlsx.writeFile('./test.xlsx').then(err => {
  if (!err) {
    console.log(`file written successfully.`)
  } else {
    console.log(`error writing file: ${err}`)
  }
})

The result will be a string resembling the following (Ignoring the line break):

Chuck Norris’ tears cure cancer,too bad Chuck Norris never cries.

It seems as though the line break is being stripped from the ends of the text property when the file is being written.

By simply adding a character after the \n the line break does not get removed. Is there a setting somewhere to turn this functionality on or off?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Alanscutcommented, Feb 25, 2020
3reactions
t1hkcommented, Apr 17, 2019

It works for me, if I change the code in the 3 modules

node_modules\exceljs\dist\es5\xlsx\xform\strings\text-xform.js
node_modules\exceljs\dist\exceljs.js
node_modules\exceljs\dist\exceljs.min.js

from:

if (model[0] === ' ' || model[model.length - 1] === ' ') {
      xmlStream.addAttribute('xml:space', 'preserve');
 }
``

into:

if (model[0] === ’ ’ || model[0] === ‘\n’ || model[model.length - 1] === ’ ’ || model[model.length - 1] === ‘\n’) { xmlStream.addAttribute(‘xml:space’, ‘preserve’); }

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to insert a new line programmatically in a richtextbox ...
In my last example you could see i select the new text and set all the format properties to the value of the...
Read more >
New line code in formula rich text field | Discussions
Hi, I'm trying to add an image thumbnail and text together on a report, and want the text to be under the image....
Read more >
Update Rich Text Content Control with String and New ...
I am passing a string with \n (like “Hello\nWorld”) to a method that sets the Rich text control in Java but the string...
Read more >
How to append \line into RTF using RichTextBox control
Paragraph myParagraph = new Paragraph(); FlowDocument myFlowDocument = new FlowDocument(); // Add some Bold text to the paragraph myParagraph.
Read more >
Excel - Insert a Line Break in a Cell
This one is easy. While you're editing the text of a cell, you can simply press Alt+Enter (or Command+Option+Enter for Mac) to add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found