From 995e0d1dae2e41fc6c4b04b98e5e8e00d3ae75e7 Mon Sep 17 00:00:00 2001 From: Nater Jorde Date: Sun, 9 Sep 2018 14:50:53 -0500 Subject: [PATCH] Fix issue 226 manually merge default font with date format. Libreoffice does not use first font if no font set --- package.json | 1 + source/lib/cell/index.js | 3 ++- source/lib/style/style.js | 23 ++++++++++++----------- source/lib/workbook/workbook.js | 5 +++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 158152a..f70b0c2 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "prepublish": "npm run build; npm run test" }, "dependencies": { + "deepmerge": "2.1.1", "image-size": "0.6.3", "jszip": "3.1.5", "lodash": "4.17.10", diff --git a/source/lib/cell/index.js b/source/lib/cell/index.js index 8a0e270..3a8a47b 100644 --- a/source/lib/cell/index.js +++ b/source/lib/cell/index.js @@ -120,9 +120,10 @@ function dateSetter(val) { var c = this.cells[0]; c.date(thisDate); } - return styleSetter.bind(this)({ + const dtStyle = new Style(this.ws.wb, { numberFormat: '[$-409]' + this.ws.wb.opts.dateFormat }); + return styleSetter.bind(this)(dtStyle); } function styleSetter(val) { diff --git a/source/lib/style/style.js b/source/lib/style/style.js index 2d40788..9e04422 100644 --- a/source/lib/style/style.js +++ b/source/lib/style/style.js @@ -1,5 +1,5 @@ const utils = require('../utils.js'); -const _ = require('lodash'); +const deepmerge = require('deepmerge'); const Alignment = require('./classes/alignment.js'); const Border = require('./classes/border.js'); @@ -7,10 +7,10 @@ const Fill = require('./classes/fill.js'); const Font = require('./classes/font.js'); const NumberFormat = require('./classes/numberFormat.js'); -let _getFontId = (wb, font) => { +let _getFontId = (wb, font = {}) => { // Create the Font and lookup key - font = _.merge({}, wb.opts.defaultFont, font); + font = deepmerge(wb.opts.defaultFont, font); const thisFont = new Font(font); const lookupKey = JSON.stringify(thisFont.toObject()); @@ -65,7 +65,7 @@ let _getBorderId = (wb, border) => { let _getNumFmt = (wb, val) => { let fmt; wb.styleData.numFmts.forEach((f) => { - if (_.isEqual(f.formatCode, val)) { + if (f.formatCode === val) { fmt = f; } }); @@ -170,26 +170,27 @@ class Style { * @returns {Style} */ opts = opts ? opts : {}; + opts = deepmerge(wb.styles[0] ? wb.styles[0] : {}, opts); if (opts.alignment !== undefined) { this.alignment = new Alignment(opts.alignment); } - if (opts.border !== undefined) { + if (opts.border !== undefined) { this.borderId = _getBorderId(wb, opts.border); // attribute 0 based index - this.border = wb.styleData.borders[this.borderId]; + this.border = wb.styleData.borders[this.borderId]; } - if (opts.fill !== undefined) { + if (opts.fill !== undefined) { this.fillId = _getFillId(wb, opts.fill); // attribute 0 based index this.fill = wb.styleData.fills[this.fillId]; } - if (opts.font !== undefined) { + if (opts.font !== undefined) { this.fontId = _getFontId(wb, opts.font); // attribute 0 based index this.font = wb.styleData.fonts[this.fontId]; } - if (opts.numberFormat !== undefined) { + if (opts.numberFormat !== undefined) { if (typeof opts.numberFormat === 'number' && opts.numberFormat <= 164) { this.numFmtId = opts.numberFormat; } else if (typeof opts.numberFormat === 'string') { @@ -281,7 +282,7 @@ class Style { obj.quotePrefix = this.quotePrefix; } - return obj; + return obj; } /** @@ -299,7 +300,7 @@ class Style { } else { thisEle.att(a, thisXF[a]); } - }); + }); } /** diff --git a/source/lib/workbook/workbook.js b/source/lib/workbook/workbook.js index 8bcc0f5..f7dbe14 100644 --- a/source/lib/workbook/workbook.js +++ b/source/lib/workbook/workbook.js @@ -1,10 +1,12 @@ const _ = require('lodash'); +const deepmerge = require('deepmerge'); const fs = require('fs'); const utils = require('../utils.js'); const Worksheet = require('../worksheet'); const Style = require('../style'); const Border = require('../style/classes/border.js'); const Fill = require('../style/classes/fill.js'); +const Font = require('../style/classes/font'); const DXFCollection = require('./dxfCollection.js'); const MediaCollection = require('./mediaCollection.js'); const DefinedNameCollection = require('../classes/definedNameCollection.js'); @@ -82,7 +84,7 @@ class Workbook { this.logger.log('opts.logger is not a valid logger'); } - this.opts = _.merge({}, workbookDefaultOpts, opts); + this.opts = deepmerge(workbookDefaultOpts, opts); this.sheets = []; this.sharedStrings = []; @@ -130,7 +132,6 @@ class Workbook { this.createStyle({ font: this.opts.defaultFont }); - } /**