Merge pull request #66 from eladxxx/master

Style/theme implementation
This commit is contained in:
SheetJSDev 2014-05-29 02:25:38 -04:00
commit c91e94dbbb
9 changed files with 510 additions and 4 deletions

View File

@ -210,6 +210,7 @@ that does not start with `!` corresponds to a cell (using `A-1` notation).
- `.c` : comments associated with the cell
- `.z` : the number format string associated with the cell (if requested)
- `.l` : the hyperlink of the cell (.Target holds link, .tooltip is tooltip)
- `.s` : the style/theme of the cell (if applicable)
For dates, `.v` holds the raw date code from the sheet and `.w` holds the text
@ -222,6 +223,7 @@ The exported `read` and `readFile` functions accept an options argument:
| cellFormula | true | Save formulae to the .f field |
| cellHTML | true | Parse rich text and save HTML to the .h field |
| cellNF | false | Save number format string to the .z field |
| cellStyles | false | Save style/theme info to the .s field |
| sheetStubs | false | Create cell objects for stub cells |
| sheetRows | 0 | If >0, read the first `sheetRows` rows ** |
| bookDeps | false | If true, parse calculation chains |

View File

@ -1,2 +1,4 @@
var styles = {}; // shared styles
var themes = {}; // shared themes

View File

@ -1,3 +1,48 @@
/* 18.8.21 fills CT_Fills */
function parse_fills(t, opts) {
styles.Fills = [];
var fill = {};
t[0].match(/<[^>]*>/g).forEach(function(x) {
var y = parsexmltag(x);
switch(y[0]) {
case '<fills': case '<fills>': case '</fills>': break;
/* 18.8.20 fill CT_Fill */
case '<fill>': break;
case '</fill>': styles.Fills.push(fill); fill = {}; break;
/* 18.8.32 patternFill CT_PatternFill */
case '<patternFill':
if(y.patternType) fill.patternType = y.patternType;
break;
case '<patternFill/>': break;
/* 18.8.3 bgColor CT_Color */
case '<bgColor':
if(!fill.bgColor) fill.bgColor = {};
if(y.indexed) fill.bgColor.indexed = parseInt(y.indexed);
if(y.theme) fill.bgColor.theme = parseInt(y.theme);
if(y.tint) fill.bgColor.tint = Number(y.tint);
/* Excel uses 8 character RGB strings? */
if(y.rgb) fill.bgColor.rgb = y.rgb.substring(y.rgb.length - 6);
break;
case '</bgColor>': break;
/* 18.8.19 fgColor CT_Color */
case '<fgColor':
if(!fill.fgColor) fill.fgColor = {};
if(y.theme) fill.fgColor.theme = parseInt(y.theme);
if(y.tint) fill.fgColor.tint = Number(y.tint);
/* Excel uses 8 character RGB strings? */
if(y.rgb) fill.fgColor.rgb = y.rgb.substring(y.rgb.length - 6);
break;
case '</fgColor>': break;
default: if(opts.WTF) throw 'unrecognized ' + y[0] + ' in fills';
}
});
}
/* 18.8.31 numFmts CT_NumFmts */
function parse_numFmts(t, opts) {
styles.NumberFmt = [];
@ -38,6 +83,7 @@ function parse_cellXfs(t, opts) {
/* 18.8.45 xf CT_Xf */
case '<xf': delete y[0];
if(y.numFmtId) y.numFmtId = parseInt(y.numFmtId, 10);
if(y.fillId) y.fillId = parseInt(y.fillId, 10);
styles.CellXf.push(y); break;
case '</xf>': break;
@ -73,7 +119,10 @@ function parse_sty_xml(data, opts) {
if((t=data.match(/<numFmts([^>]*)>.*<\/numFmts>/))) parse_numFmts(t, opts);
/* fonts CT_Fonts ? */
/* fills CT_Fills ? */
/* fills CT_Fills */
if((t=data.match(/<fills([^>]*)>.*<\/fills>/))) parse_fills(t, opts);
/* borders CT_Borders ? */
/* cellStyleXfs CT_CellStyleXfs ? */

File diff suppressed because one or more lines are too long

View File

@ -73,14 +73,24 @@ function parse_ws_xml(data, opts, rels) {
}
/* formatting */
var fmtid = 0;
var fmtid = 0, fillid = 0;
if(cell.s && styles.CellXf) {
var cf = styles.CellXf[cell.s];
if(cf && cf.numFmtId) fmtid = cf.numFmtId;
if(opts.cellStyles && cf && cf.fillId) fillid = cf.fillId;
}
try {
p.w = SSF.format(fmtid,p.v,_ssfopts);
if(opts.cellNF) p.z = SSF._table[fmtid];
if(fillid) {
p.s = styles.Fills[fillid];
if (p.s.fgColor && p.s.fgColor.theme) {
p.s.fgColor.rgb = rgb_tint(themes.themeElements.clrScheme[p.s.fgColor.theme].rgb, p.s.fgColor.tint || 0);
}
if (p.s.bgColor && p.s.bgColor.theme) {
p.s.bgColor.rgb = rgb_tint(themes.themeElements.clrScheme[p.s.bgColor.theme].rgb, p.s.bgColor.tint || 0);
}
}
} catch(e) { if(opts.WTF) throw e; }
s[cell.r] = p;
});

View File

@ -10,6 +10,10 @@ function parse_sty(data, name, opts) {
return (name.substr(-4)===".bin" ? parse_sty_bin : parse_sty_xml)(data, opts);
}
function parse_theme(data, name, opts) {
return parse_theme_xml(data, opts);
}
function parse_sst(data, name, opts) {
return (name.substr(-4)===".bin" ? parse_sst_bin : parse_sst_xml)(data, opts);
}

View File

@ -11,6 +11,7 @@ var fix_read_opts = fix_opts([
['cellNF', false], /* emit cell number format string as .z */
['cellHTML', true], /* emit html string as .h */
['cellFormula', true], /* emit formulae as .f */
['cellStyles', false], /* emits style/theme as .s */
['sheetStubs', false], /* emit empty cells */
['sheetRows', 0, 'n'], /* read n rows (0 = read all rows) */

View File

@ -25,6 +25,9 @@ function parse_zip(zip, opts) {
styles = {};
if(dir.style) styles = parse_sty(getzipdata(zip, dir.style.replace(/^\//,'')),dir.style, opts);
themes = {};
if(opts.cellStyles && dir.themes) themes = parse_theme(getzipdata(zip, dir.themes[0].replace(/^\//,'')),dir.themes[0], opts);
}
var wb = parse_wb(getzipdata(zip, dir.workbooks[0].replace(/^\//,'')), dir.workbooks[0], opts);
@ -105,6 +108,7 @@ function parse_zip(zip, opts) {
SheetNames: props.SheetNames,
Strings: strs,
Styles: styles,
Themes: themes,
SSF: SSF.get_table()
};
if(opts.bookFiles) {

256
xlsx.js

File diff suppressed because one or more lines are too long