2017-04-05 21 views
0

私はdocxtemplaterを使用してJSONデータをワードドキュメントに変換しています。ドキュメントは正常に生成されています。 docxtemplaterで改行または改行タグ

var sections = {"sections":[{"section_name":"Languages","data":"Tamil\nTelugu\nHindi\nEnglish","key":"8783"},{"section_name":"Skills","data":"JavaScript<br />jQuery<br />CSS<br />","key":"13486"}]}; 


function loadFile(url,callback){ 
     JSZipUtils.getBinaryContent(url,callback); 
    } 

    loadFile("examples/doccc.docx",function(error,content){ 
     if (error) { throw error; }; 
     var zip = new JSZip(content); 
     var doc=new Docxtemplater().loadZip(zip); 
     doc.setOptions({nullGetter: function() { 
      return ""; 
     }}); 
     doc.setData(sections); 

     try { 
      // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...) 
      doc.render(); 
     } 
     catch (error) { 
      var e = { 
       message: error.message, 
       name: error.name, 
       stack: error.stack, 
       properties: error.properties, 
      }; 
      console.log(JSON.stringify({error: e})); 
      // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object). 
      throw error; 
     } 

     var out=doc.getZip().generate({ 
      type:"blob", 
      mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
     }); //Output the document using Data-URI 
     saveAs(out,"output.docx"); 
    }); 

この

は私のテンプレート

{#sections} 
    {section_name} - {data} 
{/sections} 

それはすべてのセクションでのdocxファイルを生成しますが、「\ n」は改行と
タグが文書に文字通り印刷されています。

新しい行を改行として解釈する必要があります。

現在、ワード文書で

Languages 
Tamil\nTelugu\nHindi\nEnglish 
Skills 
JavaScript<br />jQuery<br />CSS<br /> 

...任意の助けのために

Languages 
Tamil 
Telugu 
Hindi 
English 

Skills 
JavaScript 
jQuery 
CSS 

グレイトフルとしてそれを印刷するにはどのように任意のアイデアとしての印刷。

答えて

1

あなたは行うことができます:あなたのテンプレートで

:あなたのコードで

{#sections} 
    {section_name} - 
{@data} 
{/sections} 

を、setDataメソッドの前に:勤務

sections.forEach(function(section){ 
    var lines = section.data.split("\n").split(/<br \/>|\n/g) 
    var pre = "<w:p><w:r><w:t>"; 
    var post = "</w:t></w:r></w:p>"; 
    var lineBreak = "<w:br/>"; 
    section.data = pre + lines.join(lineBreak) + post;  
}) 
+0

を。ありがとう。 テンプレートで{@data}を使用する場合、プレーンテキストであってもすべてのセクションがプレタグとポストタグを持つことが必須です。 –

+0

スペースの問題のコードサンプルもここで共有できます https://github.com/open-xml-templating/docxtemplater/issues/272 ありがとう –