Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read + write style information to .xlsx #263

Open
wants to merge 104 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
0227496
Add ability to save cell styles to .xlsx
pietersv Feb 28, 2015
845d2a8
Added support for alignment
pietersv Feb 28, 2015
a562150
Use jQuery parseXML to preserve case in XML
pietersv Mar 1, 2015
5f7a0f2
Removed dependence on cheerio, jquery; added xmlbuilder;
pietersv Mar 2, 2015
cd2b20f
Remove stray logs
pietersv Mar 2, 2015
576b59f
Fix issue with custom formats
pietersv Mar 2, 2015
05b58d8
Escape embedded quotes
pietersv Mar 3, 2015
1f02e5b
Handle embedded quotes in number formats
pietersv Mar 5, 2015
91ec052
Added support for borders
pietersv Mar 7, 2015
5e44331
Remove stray console.log() calls
pietersv Mar 7, 2015
c3af249
Add styles to README.md
pietersv Mar 9, 2015
97d4001
Update Style specifications in README.md
pietersv Mar 9, 2015
33dc7b8
Update README.md
pietersv Mar 9, 2015
aa8f3a7
Add support for textRotation
pietersv Mar 12, 2015
9b76f6b
Update README for textRotation
pietersv Mar 12, 2015
1a51c40
Remove broken link
Mar 12, 2015
0e2b8a4
Merge pull request #1 from knownasilya/patch-2
Mar 12, 2015
ad2a120
Update README.md
Mar 13, 2015
4f4f7fc
Fix issue exporting font color
pietersv Mar 20, 2015
33c32f0
Merge remote-tracking branch 'origin/master'
pietersv Mar 20, 2015
27a300b
Read styles - font, fill and numFmt
pietersv Mar 28, 2015
37a031c
Parse font bold-italic-strike-outline-shadow-superscript-subscript
pietersv Mar 28, 2015
995f5e8
Parse font bold-italic-strike-outline-shadow-superscript-subscript
pietersv Mar 28, 2015
8da3738
Update README.md
pietersv Mar 28, 2015
9102c82
Added round-trip test for styles
pietersv Mar 29, 2015
114f257
Merge remote-tracking branch 'origin/master'
pietersv Apr 15, 2015
5f9576a
Read borders and alignments
pietersv Apr 15, 2015
0b1a040
Merge changes from upsteam
pietersv Apr 15, 2015
08e648b
Update tests to reflect change in spec for fills
pietersv Apr 16, 2015
51b7cab
Parse raw_rgb
pietersv Apr 17, 2015
89066b4
Save fill color to .rgb but not tint
pietersv Apr 17, 2015
5dbf215
Correct hex2RGB coding, apply tints
pietersv Apr 17, 2015
c287383
Add syntax highlighting to readme examples
Apr 20, 2015
acf60c2
Merge pull request #2 from knownasilya/patch-2
Apr 20, 2015
40cfb0c
Document wrapText
pietersv May 8, 2015
21e1976
Merge branch 'master' of https://github.com/protobi/js-xlsx
pietersv May 8, 2015
858abf1
Remove requirement that require, module or jquery be defined before u…
pietersv May 12, 2015
ed0cd9b
Add alignment; update README
pietersv May 21, 2015
4099a24
Default gray125 style per pull request #3
pietersv Jun 4, 2015
0255a05
Remove temp example files from repo
pietersv Jun 12, 2015
8801e76
Make dist and update package name
pietersv Jun 13, 2015
0096c09
Update README, bower, package
pietersv Jun 13, 2015
9145958
Update README, bower, package
pietersv Jun 13, 2015
a602355
Update README, bower, package
pietersv Jun 13, 2015
d60a0a5
Update README, bower, package
pietersv Jun 13, 2015
3010d93
Update README, bower, package
pietersv Jun 13, 2015
c034eda
Update README, bower, package
pietersv Jun 13, 2015
1ca3c3e
Remove lab, .idea, test-csv, test-acid, etc. from repo
pietersv Jun 26, 2015
e7b06d7
Remove lab, .idea, test-csv, test-acid, etc. from repo
pietersv Jun 26, 2015
f52c80e
Remove lab, .idea, test-csv, test-acid, etc. from repo
pietersv Jun 26, 2015
dcf6f17
Retain styles for blank cells
pietersv Aug 11, 2015
0d1281c
Added test files
pietersv Aug 11, 2015
d76234a
Merge remote-tracking branch 'origin/master' into beta
pietersv Aug 11, 2015
3e3633a
Merge master to preserve blank style; increment version number
pietersv Aug 11, 2015
348c533
Update bower version to 0.8.5
pietersv Aug 11, 2015
57b6fd4
Pull changes to persist styles with blank cells; increment versions f…
pietersv Aug 11, 2015
7461389
Adds cheerio dependencie for tests/test-style.js
godu Aug 13, 2015
905c512
Adds tests
godu Aug 13, 2015
599f285
Breaks ref in style object
godu Aug 13, 2015
d00f36f
Merge pull request #10 from CoorpAcademy/fix-ref-styles
pietersv Aug 13, 2015
03b755b
Add showGridlines
pietersv Dec 4, 2015
fd83f20
Merge branch 'master' of https://github.com/protobi/js-xlsx into beta
pietersv Dec 4, 2015
8b1add6
Add showGridLines as option
pietersv Dec 4, 2015
2886195
Merge branch 'master' of https://github.com/protobi/js-xlsx
pietersv Dec 4, 2015
634699f
Merge branch 'master' of https://github.com/protobi/js-xlsx into beta
pietersv Dec 4, 2015
d4e0b8b
Bump bower version to 0.87
pietersv Dec 4, 2015
43f5698
Add row and column breaks
pietersv Dec 5, 2015
bbeec3e
Add row/col breaks
pietersv Dec 5, 2015
376096b
Add page setup
pietersv Dec 5, 2015
0bf1153
Add page scale and orientation
pietersv Dec 5, 2015
7de41c2
Add doc props
pietersv Dec 5, 2015
f984bd6
Add rowBreaks, colBreaks, scale, showGridLines
pietersv Dec 5, 2015
fe748ad
Copy README from beta
pietersv Mar 8, 2016
166d0e3
Update README.md
pietersv Mar 8, 2016
9407fe8
Update README.md
pietersv Mar 8, 2016
aceebf4
Update README.md
pietersv Mar 8, 2016
0e62411
Update README.md
pietersv Mar 8, 2016
050b74e
Update README.md
pietersv Mar 8, 2016
2ec18e9
Update README.md
pietersv Mar 8, 2016
bed6327
Set tabSelected to 0 per issue #26
pietersv Apr 6, 2016
876c4a0
Set tabSelected to 0 per issue #26
pietersv Apr 6, 2016
eef7da3
Merge branch 'master' of https://github.com/protobi/js-xlsx
pietersv Apr 6, 2016
0b8e538
Add repeating print headers
pietersv May 24, 2016
2424890
Merge remote-tracking branch 'origin/beta' into beta
pietersv May 24, 2016
8198744
merge from beta
pietersv May 25, 2016
245ffb8
Update tests; add feature to freeze headerrow
pietersv May 25, 2016
34d618c
Update tests; add feature to freeze headerrow
pietersv May 25, 2016
3c7f807
Revert package name
pietersv May 25, 2016
796fbb5
Fix file getter: letter case not match
chikh Jul 14, 2016
d3f6343
Apply letter case issue fix to the source code
chikh Sep 10, 2016
d5092b4
Merge pull request #46 from noodoo/fix/lower-case-files
pietersv Sep 29, 2016
6e66789
PR #40 - Skip cells with incorrect link to shared strings
pietersv Oct 11, 2016
1903cff
Merge remote-tracking branch 'origin/master'
pietersv Oct 11, 2016
9edf279
Add freeze pane option
pietersv Apr 3, 2017
f959b02
Add freeze panes
pietersv Apr 3, 2017
a03dc21
Add freeze panes
pietersv Apr 3, 2017
c224819
Fixed Style Table in README.md
zeg-io Apr 4, 2017
68ba4e8
Merge pull request #64 from zeg-io/patch-1
Apr 4, 2017
f2520b7
Freeze columns
pietersv May 31, 2017
c86472d
Merge branch 'master' of https://github.com/protobi/js-xlsx
pietersv May 31, 2017
9233322
Add examples
pietersv Apr 1, 2019
6226e1a
Undo last commit
pietersv Sep 18, 2019
6d0f51f
Fixed horizontal alignment values
oosswwaalldd Aug 27, 2020
8f7a294
Merge pull request #151 from oosswwaalldd/patch-1
pietersv Aug 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removed dependence on cheerio, jquery; added xmlbuilder;
  • Loading branch information
pietersv committed Mar 2, 2015
commit 5f7a0f2ad27437423516d41fd8bc45bc57dc8b54
349 changes: 0 additions & 349 deletions bits/90_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,355 +197,6 @@ var utils = {
sheet_to_row_object_array: sheet_to_row_object_array
};

if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeof $ != 'undefined')) {
var StyleBuilder = function (options) {

if(typeof module !== "undefined" && typeof require !== 'undefined' ) {
var cheerio = require('cheerio');
createElement = function(str) { return cheerio(cheerio(str, null, null, {xmlMode: true})); };
}
else if (typeof jQuery !== 'undefined' || typeof $ !== 'undefined') {
createElement = function(str) {
return $($.parseXML(str).documentElement);
} //http://stackoverflow.com/a/11719466
}
else {
createElement = function() { } // this class should never have been instantiated
}


var customNumFmtId = 164;


var table_fmt = {
0: 'General',
1: '0',
2: '0.00',
3: '#,##0',
4: '#,##0.00',
9: '0%',
10: '0.00%',
11: '0.00E+00',
12: '# ?/?',
13: '# ??/??',
14: 'm/d/yy',
15: 'd-mmm-yy',
16: 'd-mmm',
17: 'mmm-yy',
18: 'h:mm AM/PM',
19: 'h:mm:ss AM/PM',
20: 'h:mm',
21: 'h:mm:ss',
22: 'm/d/yy h:mm',
37: '#,##0 ;(#,##0)',
38: '#,##0 ;[Red](#,##0)',
39: '#,##0.00;(#,##0.00)',
40: '#,##0.00;[Red](#,##0.00)',
45: 'mm:ss',
46: '[h]:mm:ss',
47: 'mmss.0',
48: '##0.0E+0',
49: '@',
56: '"上午/下午 "hh"時"mm"分"ss"秒 "',
65535: 'General'
};
var fmt_table = {};

for (var idx in table_fmt) {
fmt_table[table_fmt[idx]] = idx;

}


var baseXmlprefix = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
var baseXml =
'<styleSheet xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"\
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" mc:Ignorable="x14ac">\
<numFmts count="1">\
<numFmt numFmtId="164" formatCode="0.00%"/>\
</numFmts>\
<fonts count="0" x14ac:knownFonts="1"></fonts>\
<fills count="0"></fills>\
<borders count="0"></borders>\
<cellStyleXfs count="1">\
<xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>\
</cellStyleXfs>\
<cellXfs count="0"></cellXfs>\
<cellStyles count="1">\
<cellStyle name="Normal" xfId="0" builtinId="0"/>\
</cellStyles>\
<dxfs count="0"/>\
<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4"/>\
</styleSheet>';

_hashIndex = {};
_listIndex = [];

return {

initialize: function (options) {
if (typeof cheerio !== 'undefined') {
this.$styles = cheerio.load(baseXml, {xmlMode: true});
this.$styles.find = function(q) { return this(q)}
}
else {
this.$styles = $($.parseXML(baseXml).documentElement);
}


// need to specify styles at index 0 and 1.
// the second style MUST be gray125 for some reason

var defaultStyle = options.defaultCellStyle;
if (!defaultStyle) defaultStyle = {
font: {name: 'Calibri', sz: '11'},
fill: { fgColor: { patternType: "none"}},
border: {},
numFmt: null
};
if (!defaultStyle.border) { defaultStyle.border = {}}

var gray125Style = JSON.parse(JSON.stringify(defaultStyle));
gray125Style.fill = { fgColor: { patternType: "gray125"}}

this.addStyles([defaultStyle, gray125Style]);
return this;
},

// create a style entry and returns an integer index that can be used in the cell .s property
// these format of this object follows the emerging Common Spreadsheet Format
addStyle: function (attributes) {
var attributes = this._duckTypeStyle(attributes);
var hashKey = JSON.stringify(attributes);
var index = _hashIndex[hashKey];
if (index == undefined) {
index = this._addXf(attributes || {});
_hashIndex[hashKey] = index;

}
else {
index = _hashIndex[hashKey];
}
return index;
},

// create style entries and returns array of integer indexes that can be used in cell .s property
addStyles: function (styles) {
var self = this;
return styles.map(function (style) {
return self.addStyle(style);
})
},

_duckTypeStyle: function(attributes) {

if (typeof attributes == 'object' && (attributes.patternFill || attributes.fgColor)) {
return {fill: attributes }; // this must be read via XLSX.parseFile(...)
}
else if (attributes.font || attributes.numFmt || attributes.border || attributes.fill) {
return attributes;
}
else {
return this._getStyleCSS(attributes)
}
},

_getStyleCSS: function(css) {
return css; //TODO
},

// Create an <xf> record for the style as well as corresponding <font>, <fill>, <border>, <numfmts>
// Right now this is simple and creates a <font>, <fill>, <border>, <numfmts> for every <xf>
// We could perhaps get fancier and avoid duplicating auxiliary entries as Excel presumably intended, but bother.
_addXf: function (attributes) {


var fontId = this._addFont(attributes.font);
var fillId = this._addFill(attributes.fill);
var borderId = this._addBorder(attributes.border);
var numFmtId = this._addNumFmt(attributes.numFmt);

var $xf = createElement('<xf></xf>')
.attr("numFmtId", numFmtId)
.attr("fontId", fontId)
.attr("fillId", fillId)
.attr("borderId", 0)
.attr("xfId", "0");

if (fontId > 0) {
$xf.attr('applyFont', "1");
}
if (fillId > 0) {
$xf.attr('applyFill', "1");
}
if (borderId > 0) {
$xf.attr('applyBorder', "1");
}
if (numFmtId > 0) {
$xf.attr('applyNumberFormat', "1");
}

if (attributes.alignment) {
var $alignment = createElement('<alignment></alignment>');
if (attributes.alignment.horizontal) { $alignment.attr('horizontal', attributes.alignment.horizontal);}
if (attributes.alignment.vertical) { $alignment.attr('vertical', attributes.alignment.vertical);}
if (attributes.alignment.indent) { $alignment.attr('indent', attributes.alignment.indent);}
if (attributes.alignment.wrapText) { $alignment.attr('wrapText', attributes.alignment.wrapText);}
$xf.append($alignment).attr('applyAlignment',1)

}

var $cellXfs = this.$styles.find('cellXfs');

$cellXfs.append($xf);
var count = +$cellXfs.attr('count') + 1;

$cellXfs.attr('count', count);
return count - 1;
},

_addFont: function (attributes) {
if (!attributes) {
return 0;
}

var $font = createElement('<font/>', null, null, {xmlMode: true});

$font.append(createElement('<sz/>').attr('val', attributes.sz))
.append(createElement('<color/>').attr('theme', '1'))
.append(createElement('<name/>').attr('val', attributes.name))
// .append(createElement('<family/>').attr('val', '2'))
// .append(createElement('<scheme/>').attr('val', 'minor'));

if (attributes.bold) $font.append('<b/>');
if (attributes.underline) $font.append('<u/>');
if (attributes.italic) $font.append('<i/>');

if (attributes.color) {
if (attributes.color.theme) {
$font.append(createElement('<color/>').attr('theme', attributes.color.theme));
} else if (attributes.color.rgb) {
$font.append(createElement('<color/>').attr('rgb', attributes.color.rgb));
}
}


var $fonts = this.$styles.find('fonts');
$fonts.append($font);

var count = $fonts.children().length;
$fonts.attr('count', count);
return count - 1;
},

_addNumFmt: function (numFmt) {
if (!numFmt) {
return 0;
}

if (typeof numFmt == 'string') {
var numFmtIdx = fmt_table[numFmt];
if (numFmtIdx >= 0) {
return numFmtIdx; // we found a match against built in formats
}
}

if (numFmt == +numFmt) {
return numFmt; // we're matching an integer against some known code
}

var $numFmt = createElement('<numFmt/>', null, null, {xmlMode: true})
.attr("numFmtId", ++customNumFmtId )
.attr("formatCode", numFmt);

var $numFmts = this.$styles.find('numFmts');
$numFmts.append($numFmt);

var count = $numFmts.children().length;
$numFmts.attr('count', count);
return customNumFmtId;
},

_addFill: function (attributes) {

if (!attributes) {
return 0;
}
var $patternFill = createElement('<patternFill></patternFill>', null, null, {xmlMode: true})
.attr('patternType', attributes.patternType || 'solid');

if (attributes.fgColor) {
//Excel doesn't like it when we set both rgb and theme+tint, but xlsx.parseFile() sets both
//var $fgColor = createElement('<fgColor/>', null, null, {xmlMode: true}).attr(attributes.fgColor)
if (attributes.fgColor.rgb) {

if (attributes.fgColor.rgb.length == 6) {
attributes.fgColor.rgb = "FF" + attributes.fgColor.rgb /// add alpha to an RGB as Excel expects aRGB
}
var $fgColor = createElement('<fgColor/>', null, null, {xmlMode: true}).
attr('rgb', attributes.fgColor.rgb);
$patternFill.append($fgColor);
}
else if (attributes.fgColor.theme) {
var $fgColor = createElement('<fgColor/>', null, null, {xmlMode: true});
$fgColor.attr('theme', attributes.fgColor.theme);
if (attributes.fgColor.tint) {
$fgColor.attr('tint', attributes.fgColor.tint);
}
$patternFill.append($fgColor);
}

if (!attributes.bgColor) {
attributes.bgColor = { "indexed": "64"}
}
}

if (attributes.bgColor) {
var $bgColor = createElement('<bgColor/>', null, null, {xmlMode: true}).attr(attributes.bgColor);
$patternFill.append($bgColor);
}

var $fill = createElement('<fill></fill>')
.append($patternFill);

this.$styles.find('fills').append($fill);
var $fills = this.$styles.find('fills')
$fills.append($fill);

var count = $fills.children().length;
$fills.attr('count', count);
return count - 1;
},

_addBorder: function (attributes) {
if (!attributes) {
return 0;
}
var $border = createElement('<border></border>')
.append('<left></left>')
.append('<right></right>')
.append('<top></top>')
.append('<bottom></bottom>')
.append('<diagonal></diagonal>');

var $borders = this.$styles.find('borders');
$borders.append($border);

var count = $borders.children().length;
$borders.attr('count', count);
return count;
},

toXml: function () {
if (this.$styles.find('numFmts').children().length == 0) {
this.$styles.find('numFmts').remove();
}
if (this.$styles.xml) { return this.$styles.xml(); }
else {
var s = new XMLSerializer(); //http://stackoverflow.com/a/5744268
return baseXmlprefix + s.serializeToString(this.$styles[0]);;
}
}
}.initialize(options||{});
}
}
Loading