d-band / html2xlsx

transform html to excel (just support xlsx)
16 stars 3 forks source link

Enhance cell customization - add data-base64-xlsx-cell-config #64

Open yuanoook opened 3 years ago

yuanoook commented 3 years ago

The use case is like this:

  1. To customize the cell to use the other numFmt, like currency "$"#,##0.00;[Red]\-"$"#,##0.00, or even more;

  2. Set _value: 10000, instead of _value: "10000".

The string _value: "10000" could not be recognized as a calculation dependency in excel software, which bugged our clients a lot.

To use data-base64-xlsx-cell-config

const htmlTo = require('html2xlsx')
const btoa = (window && window.btoa) || (str => Buffer.from(str, 'utf-8').toString('base64'))

htmlTo(`
  <table>
    <tr>
      <td data-base64-xlsx-cell-config="${btoa(JSON.stringify({
          numFmt: '"$"#,##0.00;[Red]\-"$"#,##0.00',
          cellType: 'TypeNumeric',
          _value: 10000
        }))}"
      >
        10000
      </td>
    </tr>
  </table>
`, (err, file) => {
  if (err) return console.error(err);

  file.saveAs()
    .pipe(fs.createWriteStream('test.xlsx'))
    .on('finish', () => console.log('Done.'));
});
coveralls commented 3 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling e48f9b76a562ff053ea2486ef66193f38953be69 on yuanoook:cell-customization-enhancement into e104d08828e6b27a40f761193742ce71d859fbef on d-band:master.

yuanoook commented 3 years ago

Another issue found: NODE_ENV is not a command when try to install 0.4.2 in windows

This could be solved with cross-env, or, run build in prepublish as 0.4.0.