Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 545565c

Browse files
authored
Merge pull request #237 from peakon/date1904
Start on support for 1904 based dates
2 parents f6fbc19 + 804e353 commit 545565c

File tree

9 files changed

+107
-10
lines changed

9 files changed

+107
-10
lines changed

lib/doc/workbook.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var CSV = require('./../csv/csv');
3737
var Workbook = module.exports = function() {
3838
this.created = new Date();
3939
this.modified = this.created;
40+
this.properties = {};
4041
this._worksheets = [];
4142
this.views = [];
4243
this._definedNames = new DefinedNames();
@@ -140,6 +141,7 @@ Workbook.prototype = {
140141
lastPrinted: this.lastPrinted,
141142
created: this.created,
142143
modified: this.modified,
144+
properties: this.properties,
143145
worksheets: this._worksheets.filter(Boolean).map(function(worksheet) { return worksheet.model; }),
144146
definedNames: this._definedNames.model,
145147
views: this.views,
@@ -151,7 +153,7 @@ Workbook.prototype = {
151153
category: this.category,
152154
description: this.description,
153155
language: this.language,
154-
revision: this.revision
156+
revision: this.revision,
155157
};
156158
},
157159
set model(value) {
@@ -173,6 +175,7 @@ Workbook.prototype = {
173175
this.language = value.language;
174176
this.revision = value.revision;
175177

178+
this.properties = value.properties;
176179
this._worksheets = [];
177180
value.worksheets.forEach(function(worksheetModel) {
178181
var id = worksheetModel.id;

lib/stream/xlsx/workbook-writer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ WorkbookWriter.prototype = {
302302
var model = {
303303
worksheets: this._worksheets.filter(Boolean),
304304
definedNames: this._definedNames.model,
305-
views: this.views
305+
views: this.views,
306+
properties: {}
306307
};
307308

308309
return new Bluebird(function(resolve) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) 2016 Guyon Roche
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*
22+
*/
23+
'use strict';
24+
25+
var utils = require('../../../utils/utils');
26+
var BaseXform = require('../base-xform');
27+
28+
var WorksheetPropertiesXform = module.exports = function() {
29+
};
30+
31+
utils.inherits(WorksheetPropertiesXform, BaseXform, {
32+
33+
render: function(xmlStream, model) {
34+
xmlStream.leafNode('workbookPr', {
35+
date1904: model.date1904,
36+
defaultThemeVersion: 164011,
37+
filterPrivacy: 1
38+
});
39+
},
40+
41+
parseOpen: function(node) {
42+
if (node.name === 'workbookPr') {
43+
this.model = {};
44+
45+
if (node.attributes.date1904 !== undefined) {
46+
this.model.date1904 = parseInt(node.attributes.date1904);
47+
};
48+
return true;
49+
} else {
50+
return false;
51+
}
52+
},
53+
parseText: function() {
54+
},
55+
parseClose: function() {
56+
return false;
57+
}
58+
});

lib/xlsx/xform/book/workbook-xform.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ var ListXform = require('../list-xform');
3434
var DefinedNameXform = require('./defined-name-xform');
3535
var SheetXform = require('./sheet-xform');
3636
var WorkbookViewXform = require('./workbook-view-xform');
37+
var WorkbookPropertiesXform = require('./workbook-properties-xform');
3738

3839
var WorkbookXform = module.exports = function() {
3940
this.map = {
4041
fileVersion: WorkbookXform.STATIC_XFORMS.fileVersion,
41-
workbookPr: WorkbookXform.STATIC_XFORMS.workbookPr,
42+
workbookPr: new WorkbookPropertiesXform(),
4243
bookViews: new ListXform({tag: 'bookViews', count: false, childXform: new WorkbookViewXform()}),
4344
sheets: new ListXform({tag: 'sheets', count: false, childXform: new SheetXform()}),
4445
definedNames: new ListXform({tag: 'definedNames', count: false, childXform: new DefinedNameXform()}),
@@ -56,7 +57,6 @@ utils.inherits(WorkbookXform, BaseXform, {
5657
},
5758
STATIC_XFORMS: {
5859
fileVersion: new StaticXform({tag: 'fileVersion', $: {appName: 'xl', lastEdited: 5, lowestEdited: 5, rupBuild: 9303}}),
59-
workbookPr: new StaticXform({tag: 'workbookPr', $: {defaultThemeVersion: 164011, filterPrivacy: 1}}),
6060
calcPr: new StaticXform({tag: 'calcPr', $: {calcId: 171027}})
6161
}
6262
},{
@@ -88,7 +88,7 @@ utils.inherits(WorkbookXform, BaseXform, {
8888
xmlStream.openNode('workbook', WorkbookXform.WORKBOOK_ATTRIBUTES);
8989

9090
this.map.fileVersion.render(xmlStream);
91-
this.map.workbookPr.render(xmlStream);
91+
this.map.workbookPr.render(xmlStream, model.properties);
9292
this.map.bookViews.render(xmlStream, model.views);
9393
this.map.sheets.render(xmlStream, model.sheets);
9494
this.map.definedNames.render(xmlStream, model.definedNames);
@@ -129,12 +129,13 @@ utils.inherits(WorkbookXform, BaseXform, {
129129
switch(name) {
130130
case 'workbook':
131131
this.model = {
132-
sheets: this.map.sheets.model
132+
sheets: this.map.sheets.model,
133+
properties: this.map.workbookPr.model,
134+
views: this.map.bookViews.model
133135
};
134136
if (this.map.definedNames.model) {
135137
this.model.definedNames = this.map.definedNames.model;
136138
}
137-
this.model.views = this.map.bookViews.model;
138139

139140
return false;
140141
default:

lib/xlsx/xlsx.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ XLSX.prototype = {
140140
model.sheets = workbook.sheets;
141141
model.definedNames = workbook.definedNames;
142142
model.views = workbook.views;
143+
model.properties = workbook.properties;
143144
});
144145
break;
145146
case 'xl/_rels/workbook.xml.rels':

spec/unit/xlsx/xform/book/data/book.1.1.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
],
55
"sheets": [
66
{"name": "Values", "id": 1, "rId": "rId1"}
7-
]
7+
],
8+
"properties": {
9+
"date1904": 1
10+
}
811
}

spec/unit/xlsx/xform/book/data/book.1.2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
22
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
33
<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="9303"/>
4-
<workbookPr filterPrivacy="1" defaultThemeVersion="164011"/>
4+
<workbookPr filterPrivacy="1" defaultThemeVersion="164011" date1904="1"/>
55
<bookViews>
66
<workbookView visibility="hidden" xWindow="0" yWindow="0" windowWidth="12000" windowHeight="24000"/>
77
</bookViews>

spec/unit/xlsx/xform/book/data/book.1.3.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
],
55
"sheets": [
66
{"name": "Values", "id": 1, "rId": "rId1"}
7-
]
7+
],
8+
"properties": {
9+
"date1904": 1
10+
}
811
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
var WorkbookPropertiesXform = require('../../../../../lib/xlsx/xform/book/workbook-properties-xform');
4+
var testXformHelper = require('./../test-xform-helper');
5+
6+
var expectations = [
7+
{
8+
title: 'default',
9+
create: function() { return new WorkbookPropertiesXform(); },
10+
preparedModel: {},
11+
xml: '<workbookPr defaultThemeVersion="164011" filterPrivacy="1"></workbookPr>',
12+
parsedModel: {},
13+
tests: ['render', 'renderIn']
14+
},
15+
{
16+
title: 'date1904',
17+
create: function() { return new WorkbookPropertiesXform(); },
18+
preparedModel: { date1904: 1},
19+
xml: '<workbookPr date1904="1" defaultThemeVersion="164011" filterPrivacy="1"></workbookPr>',
20+
parsedModel: { date1904: 1},
21+
tests: ['render', 'renderIn', 'parse']
22+
}
23+
];
24+
25+
describe('WorkbookPropertiesXform', function() {
26+
testXformHelper(expectations);
27+
});

0 commit comments

Comments
 (0)