From 91ec052ae68f7198f36ba80cfb866281f7b0aaa8 Mon Sep 17 00:00:00 2001 From: Pieter Sheth-Voss Date: Fri, 6 Mar 2015 23:55:50 -0500 Subject: [PATCH] Added support for borders --- bits/91_xmlbuilder.js | 4 +++ bits/92_stylebuilder.js | 54 +++++++++++++++++++++++++++++++------- xlsx.js | 58 ++++++++++++++++++++++++++++++++++------- 3 files changed, 96 insertions(+), 20 deletions(-) diff --git a/bits/91_xmlbuilder.js b/bits/91_xmlbuilder.js index aadf8e6..86d5dd1 100644 --- a/bits/91_xmlbuilder.js +++ b/bits/91_xmlbuilder.js @@ -32,6 +32,10 @@ var XmlNode = (function () { } XmlNode.prototype.attr = function (attr, value) { + if (value == undefined) { + delete this._attributes[attr]; + return this; + } if (arguments.length == 0) { return this._attributes; } diff --git a/bits/92_stylebuilder.js b/bits/92_stylebuilder.js index ccf8582..c2cbdde 100644 --- a/bits/92_stylebuilder.js +++ b/bits/92_stylebuilder.js @@ -170,7 +170,7 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo .attr("numFmtId", numFmtId) .attr("fontId", fontId) .attr("fillId", fillId) - .attr("borderId", 0) + .attr("borderId", borderId) .attr("xfId", "0"); if (fontId > 0) { @@ -200,6 +200,8 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo var count = +this.$cellXfs.children().length; this.$cellXfs.attr('count', count); + + console.log($xf); return count - 1; }, @@ -312,22 +314,54 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo return count - 1; }, - _addBorder: function (attributes) { - if (!attributes) { - return 0; + _getSubBorder: function(direction, spec) { + + var $direction = XmlNode(direction); + if (spec){ + if (spec.style) $direction.attr('style', spec.style); + if (spec.color) { + var $color = XmlNode('color'); + if (spec.color.auto) { + $color.attr('auto', spec.color.auto); + } + else if (spec.color.rgb) { + $color.attr('rgb', spec.color.rgb); + } + else if (spec.color.theme || spec.color.tint) { + $color.attr('theme', spec.color.theme || "1"); + $color.attr('tint', spec.color.tint || "0"); + } + $direction.append($color) + } } + return $direction; + }, + + _addBorder: function (attributes) { + if (!attributes) { return 0; } + + var self = this; + var $border = XmlNode('border') - .append(new XmlNode('left')) - .append(new XmlNode('right')) - .append(new XmlNode('top')) - .append(new XmlNode('bottom')) - .append(new XmlNode('diagonal')); + .attr("diagonalUp",attributes.diagonalUp) + .attr("diagonalDown",attributes.diagonalDown); + + var directions = ["left","right","top","bottom","diagonal"]; + + directions.forEach(function(direction) { + $border.append(self._getSubBorder(direction, attributes[direction])) + }); + +// if (!attributes.left && !attributes.right && !attributes.top && !attributes.bottom && !attributes.diagonal) { return 0;} + + console.log($border.toXml()); this.$borders.append($border); var count = this.$borders.children().length; this.$borders.attr('count', count); - return count; + console.log(count); + return count -1; }, toXml: function () { diff --git a/xlsx.js b/xlsx.js index ed99ce1..0f93b78 100644 --- a/xlsx.js +++ b/xlsx.js @@ -5445,6 +5445,10 @@ var XmlNode = (function () { } XmlNode.prototype.attr = function (attr, value) { + if (value == undefined) { + delete this._attributes[attr]; + return this; + } if (arguments.length == 0) { return this._attributes; } @@ -5667,7 +5671,7 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo .attr("numFmtId", numFmtId) .attr("fontId", fontId) .attr("fillId", fillId) - .attr("borderId", 0) + .attr("borderId", borderId) .attr("xfId", "0"); if (fontId > 0) { @@ -5697,6 +5701,8 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo var count = +this.$cellXfs.children().length; this.$cellXfs.attr('count', count); + + console.log($xf); return count - 1; }, @@ -5809,22 +5815,54 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo return count - 1; }, - _addBorder: function (attributes) { - if (!attributes) { - return 0; + _getSubBorder: function(direction, spec) { + + var $direction = XmlNode(direction); + if (spec){ + if (spec.style) $direction.attr('style', spec.style); + if (spec.color) { + var $color = XmlNode('color'); + if (spec.color.auto) { + $color.attr('auto', spec.color.auto); + } + else if (spec.color.rgb) { + $color.attr('rgb', spec.color.rgb); + } + else if (spec.color.theme || spec.color.tint) { + $color.attr('theme', spec.color.theme || "1"); + $color.attr('tint', spec.color.tint || "0"); + } + $direction.append($color) + } } + return $direction; + }, + + _addBorder: function (attributes) { + if (!attributes) { return 0; } + + var self = this; + var $border = XmlNode('border') - .append(new XmlNode('left')) - .append(new XmlNode('right')) - .append(new XmlNode('top')) - .append(new XmlNode('bottom')) - .append(new XmlNode('diagonal')); + .attr("diagonalUp",attributes.diagonalUp) + .attr("diagonalDown",attributes.diagonalDown); + + var directions = ["left","right","top","bottom","diagonal"]; + + directions.forEach(function(direction) { + $border.append(self._getSubBorder(direction, attributes[direction])) + }); + +// if (!attributes.left && !attributes.right && !attributes.top && !attributes.bottom && !attributes.diagonal) { return 0;} + + console.log($border.toXml()); this.$borders.append($border); var count = this.$borders.children().length; this.$borders.attr('count', count); - return count; + console.log(count); + return count -1; }, toXml: function () {