Ziv-Barber / officegen

Standalone Office Open XML files (Microsoft Office 2007 and later) generator for Word (docx), PowerPoint (pptx) and Excell (xlsx) in javascript. The output is a stream.
MIT License
2.65k stars 471 forks source link

\n not working in DOCX - replaces with space #254

Closed fil-filiposki closed 5 years ago

fil-filiposki commented 6 years ago

Hi,

It appears '\n' is replaced with a space when adding text for DOCX. How do you create a new line?

Example below:

var p = 'firstline\nnextline';  
pObj.addText(p,{font_face:'Segoe UI Light',color:'#666666',font_size:10});

Returns:

firstline nextline

I am using Officegen 0.4.5.

CraigEge commented 6 years ago

p.addLineBreak() is the same as Shift + Enter in Office Word (A new line in the current paragraph)

fil-filiposki commented 6 years ago

Hi Craig,

Thanks for your help, I have tried replacing '\n' with '+p.addLineBreak()+' however doesn't appear to be working. I have also tried other options such as '\r\n' & '\r' however also no luck. I do wonder how everybody else has been able to implement new lines in strings? I would have thought this would be necessary requirement for most word documents.

Any other ideas or workarounds you could suggest?

CraigEge commented 6 years ago

It isn't inline, you would do this:

var pObj = docx.createP();
pObj.addText('First Line');
pObj.addLineBreak();
pObj.addText('Second Line');

If you already had text with newlines simply split the string in to an array and loop over it to achieve the same effect.

fil-filiposki commented 6 years ago

Hi Craig,

Thanks for your help, I can confirm this works - for anybody else looking to do the same:

var string = 'test\ntesttest\n\ntesttesttest';
var split = string.split(/\n/);
var pObj = docx.createP();
for(var x of split){
  pObj.addText(x);
  pObj.addLineBreak();
}
Ziv-Barber commented 5 years ago

I'll leave it open because I'll add an automatic support for this like I'm doing for pptx files. I'll be ready for version 0.6.3 (in a few days).

Ziv-Barber commented 5 years ago

Implemented in 0.6.3