title: How to add conditional formatting to an Excel file in Javascript?
body: <p>How should I add conditional formatting to some columns in an Excel file that I generate in Node.js? When I add the xml to the worksheet like this <a href="https://stackoverflow.com/q/16225694/369446">answer</a> I get errors in Excel, thought the file opens. I generate the file using <a href="https://github.com/SheetJS/js-xlsx" rel="nofollow noreferrer">xlsx</a> in Node.js so the Excel file looks like this inside:</p>
  
  <ul>
  <li>Sample.xlsx
  
  <ul>
  <li>[Content_Types].xml</li>
  <li>...</li>
  <li>xl
  
  <ul>
  <li>_rels</li>
  <li>sharedString.xml</li>
  <li>styles.xml</li>
  <li>theme</li>
  <li>workbook.xml</li>
  <li>worksheets
  
  <ul>
  <li><strong>sheet1.xml</strong></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I write my conditional formatting into sheet1.xml, here is the code to do this:</p>
  
  <pre><code>var fs = require("fs");
  var jszip = require("jszip");
  var CONDITIONAL_FORMATTING = '&lt;conditionalFormatting sqref="A1"&gt;' +
    '&lt;cfRule type="expression" dxfId="0" priority="1"&gt;&lt;formula&gt;LEN(A1)&amp;' +
    'gt;2&lt;/formula&gt;&lt;/cfRule&gt;&lt;/conditionalFormatting&gt;';
  var WORKSHEET1 = 'xl/worksheets/sheet1.xml';
  
  fs.readFile("excel/rewrite.xlsx", function(err, data) {
      jszip.loadAsync(data).
          then(function(zip) {
              rezip = zip;
              return zip.file(WORKSHEET1).async("string");
          }).then(function (worksheet) {
              var pos = worksheet.indexOf('&lt;pageMargins');
              if(pos &gt; 0) {
                  worksheet = worksheet.substring(0,pos) +
                    CONDITIONAL_FORMATTING + worksheet.substring(pos);
              } 
              rezip.file(WORKSHEET1,worksheet);
              rezip.generateNodeStream({type:'nodebuffer',streamFiles:true})
                  .pipe(fs.createWriteStream('out.xlsx'))
                  .on('finish', function () {
                      console.log('done');    
                  });
          });
  });
  </code></pre>