Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Fix issue 226
Browse files Browse the repository at this point in the history
manually merge default font with date format. Libreoffice does not use first font if no font set
  • Loading branch information
natergj committed Sep 9, 2018
1 parent f732832 commit 995e0d1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion source/lib/cell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 12 additions & 11 deletions source/lib/style/style.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const utils = require('../utils.js');
const _ = require('lodash');
const deepmerge = require('deepmerge');

const Alignment = require('./classes/alignment.js');
const Border = require('./classes/border.js');
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());

Expand Down Expand Up @@ -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;
}
});
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -281,7 +282,7 @@ class Style {
obj.quotePrefix = this.quotePrefix;
}

return obj;
return obj;
}

/**
Expand All @@ -299,7 +300,7 @@ class Style {
} else {
thisEle.att(a, thisXF[a]);
}
});
});
}

/**
Expand Down
5 changes: 3 additions & 2 deletions source/lib/workbook/workbook.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -130,7 +132,6 @@ class Workbook {
this.createStyle({
font: this.opts.defaultFont
});

}

/**
Expand Down

0 comments on commit 995e0d1

Please sign in to comment.