From e48f9b76a562ff053ea2486ef66193f38953be69 Mon Sep 17 00:00:00 2001 From: Rango Yuan Date: Wed, 9 Dec 2020 10:11:49 +0000 Subject: [PATCH] enhance cell customization - add data-base64-xlsx-cell-config --- src/index.js | 10 +++++ test/expect/data-base64-cell-config.xlsx | Bin 0 -> 20786 bytes test/index.test.js | 55 +++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 test/expect/data-base64-cell-config.xlsx diff --git a/src/index.js b/src/index.js index a66981b..5019f03 100644 --- a/src/index.js +++ b/src/index.js @@ -136,6 +136,16 @@ module.exports = (html, callback, options = {}) => { cell.hMerge = cs - 1; } + // In case you need the flexibility to config all possible + // _value, formula, numFmt, cellType... + const b64Config = $td.attr('data-base64-xlsx-cell-config'); + if (b64Config) { + try { + const configStr = Buffer.from(b64Config, 'base64').toString('utf-8'); + Object.assign(cell, JSON.parse(configStr)); + } catch (e) {} + } + for (let r = 0; r < rs; r++) { if (offsets[hi + r] === undefined) { offsets[hi + r] = 0; diff --git a/test/expect/data-base64-cell-config.xlsx b/test/expect/data-base64-cell-config.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..7fa2a2adccf17c61671bf7a0e7173b643a113ab5 GIT binary patch literal 20786 zcmeHP&5tC>Rj;)%l7}yYEdf3ywa7xo)78~q)7@KLtM2OVne}$hw7Pm{*S1zBsxqo7 zx3e;HGqd_*WF)jca6lj|4B}#hPd*@EkykPj3n$Eh1N;FT*B`Qtv?nfH%h(?)%wC>)skM7l0QheHwLG3?i(Ae6#pN<+7{w79rd z8w$s(Dh!+9JUIMb&*{otzdIUAFIGrG>53T4Mgu2^s?7#dI0~(vzdp-`uWq6K?U?k>^oDf___F-}S?n z?5*nBO4YJuPmJ97e(2aODu?*0R_OExAa)>oF%h$(fq%Xav5Z^+u_Xlra^QFpB>dV zaIv(ozDgLNG&ILwYv_=TSFNGA;Qs^1wk3*43XUBQwyI03RjWISVt@FmDnM&Mx*~{3 z#At|KlBN#inS_i{5XF}yC}miR78}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zr-5Qu+Fce~i?{!p0Wj1!EaL>@LN8{G{|k{pw%_ zstS!>5p5!oV`grC1}35Qk-Gtm!LDSV>4V)5($nJF9}Bz zI6vlEgd|+=sq6HbGMb@NbIx{~Ivrd2p3hotNvH9}HFTP3krX^WdLbQb)C>jQ3AEYr z5hpPusWA`#!mil2AH4ZZ58GVn`zf{E6=5BxyxK7~4)vpX_blV!bri+(@BW^2)`>b)+gSU~V$G`O* z{QAL*rw2~1*Vn>{sc94^yV~oKE;WR*u7a@PNNy}@BPJ~vxMv7JcMexE z{(`Ox46tvQL0OM)SCjX2+H@#%fi6LrL4Vve-=P1DYzDzBh($(}ya82H>KXo&j~cto zLR#{b*Z=8zpT*Hn`u>v2H*y=8=NKL3OQc^u=Q_VXHh9%;s%bX7(QtnlM>y54&bz`t zt0GJN<1@5$6^y$0rp1@feCExOAHVS1>fiI9d;WPk2m8X8j-~zj-O@UaR0*kOgC-yu zMWp^*fQ~&$UTO1H2Xt6TJ>y)lEj-%!+Ncre724yj5J3$q0KY6lpx02C65+WX?ci#tI)*v006At%F(NXNPI%1(a|duRbCls)HIaD1(Q*K zwHlKcuR`As#U&k9(^_Tlj0U~v_;f=+)(=cklA>mVu2jIuGT2RWi2;DA3^%LLLVB@^ z3!4qxwNR`<5n-jv7K&A>6o?fc*$;3=wF<73L z^4zP4rFbPrj^zUVN;pU_=qLi`jVl#I(&R(z|MVC0YZXkkix(@TP?gH_h{S5v&sqL7 zVziW?K1A$KahFJzv=_mOhvrkjO65B`7=|b%phf87f$$vqH*r2ZBbsLGMew|NrDF2@ z&abg_RsK~3D;{Fb(fpSV#=xaoEiMb?2>yMG=^EK_S;upTBIj>38AG~^V`{g!kdveN znhvI?y`{=7&X{vN!FK8VxhTS7hL { }); }); }); + + it('should htmlToXlsx data base64 cell config ok', (done) => { + /* eslint-disable no-useless-escape */ + const btoa = str => Buffer.from(str, 'utf-8').toString('base64'); + const object2b64 = obj => btoa(JSON.stringify(obj)); + const cells = [0, 1, 10, 100, 1000, 10000, 100000, NaN].map(amount => ({ + numFmt: '"$"#,##0.00;[Red]\-"$"#,##0.00', + cellType: 'TypeNumeric', + _value: amount + })); + const body = cells.map(cell => ` + + + + `).concat(` + + + + `, ` + + + + `); + + htmlTo(` + + + ${body} +
+ `, (err, file) => { + if (err) return done(err); + + const tmpfile = join(tmpdir(), 'data-base64-cell-config.xlsx'); + const expfile = join(__dirname, 'expect/data-base64-cell-config.xlsx'); + file + .saveAs() + .pipe(fs.createWriteStream(tmpfile)) + .on('finish', () => { + const expectFile = fs.createReadStream(expfile); + const actualFile = fs.createReadStream(tmpfile); + streamEqual(expectFile, actualFile, function (err, ok) { + expect(err).to.be.null; + expect(ok).to.be.true; + done(); + }); + }); + }); + }); });