Skip to content

Commit

Permalink
Merge branch 'master' into jackkum-print-params
Browse files Browse the repository at this point in the history
# Conflicts:
#	spec/integration/workbook.spec.js
#	spec/unit/xlsx/xform/sheet/data/sheet.2.0.json
#	spec/unit/xlsx/xform/sheet/data/sheet.2.1.json
  • Loading branch information
guyonroche committed Aug 16, 2016
2 parents 54ab82d + 56dfc7a commit faf05db
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 165 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ npm install exceljs
Kudos to <a href="https://github.com/yonjah">yonjah</a> and <a href="https://www.youtube.com/watch?v=wCfE-9bhY2Y">Josh Emerson</a> for the resolution.
</li>
<li>
Fix for <a href="https://github.com/guyonroche/exceljs/issues/162">Multiple Sheets opens in 'Group' mode in Excel</a>.
My bad - overzealous sheet view code.
Fixed <a href="https://github.com/guyonroche/exceljs/issues/162">Multiple Sheets opens in 'Group' mode in Excel</a> again.
Added <a href="#workbook-views">Workbook views</a>.
</li>
<li>Also fix for empty sheet generating invalid xlsx.</li>
</ul>
Expand All @@ -40,6 +40,7 @@ npm install exceljs
<ul>
<li><a href="#create-a-workbook">Create a Workbook</a></li>
<li><a href="#set-workbook-properties">Set Workbook Properties</a></li>
<li><a href="#workbook-views">Workbook Views</a></li>
<li><a href="#add-a-worksheet">Add a Worksheet</a></li>
<li><a href="#access-worksheets">Access Worksheets</a></li>
<li><a href="#worksheet-properties">Worksheet Properties</a></li>
Expand Down Expand Up @@ -102,6 +103,17 @@ workbook.created = new Date(1985, 8, 30);
workbook.modified = new Date();
```

## Workbook Views

```javascript
workbook.views = [
{
x: 0, y: 0, width: 10000, height: 20000,
firstSheet: 0, activeTab: 1, visibility: 'visible'
}
]
```

## Add a Worksheet

```javascript
Expand Down Expand Up @@ -185,11 +197,6 @@ Each view also supports various properties:
| zoomScale | 100 | Percentage zoom to use for the view |
| zoomScaleNormal | 100 | Normal zoom for the view |
| style | undefined | Presentation style - one of pageBreakPreview or pageLayout. Note pageLayout is not compatable with frozen views |
| x | 0 | x position of the view's window |
| y | 0 | y position of the view's window |
| width | 12000 | Width of the view's window |
| height | 24000 | Height of the view's window |
| visibility | 'visible' | Visibility of the view - one of 'visible' or 'hidden' |

### Frozen Views

Expand Down Expand Up @@ -1217,5 +1224,4 @@ In practical terms, this error only seems to arise with over 98 sheets (or 49 sh
| 0.2.10 | <ul><li>Refactoring Complete. All unit and integration tests pass.</li></ul> |
| 0.2.11 | <ul><li><a href="#outline-level">Outline Levels</a> thanks to <a href="https://github.com/cricri">cricri</a> for the contribution.</li><li><a href="#worksheet-properties">Worksheet Properties</a></li><li>Further refactoring of worksheet writer.</li></ul> |
| 0.2.12 | <ul><li><a href="#worksheet-views">Sheet Views</a> thanks to <a href="https://github.com/cricri">cricri</a> again for the contribution.</li></ul> |


| 0.2.13 | <ul><li>Fix for <a href="https://github.com/guyonroche/exceljs/issues">exceljs might be vulnerable for regular expression denial of service</a>. Kudos to <a href="https://github.com/yonjah">yonjah</a> and <a href="https://www.youtube.com/watch?v=wCfE-9bhY2Y">Josh Emerson</a> for the resolution.</li><li>Fix for <a href="https://github.com/guyonroche/exceljs/issues/162">Multiple Sheets opens in 'Group' mode in Excel</a>. My bad - overzealous sheet view code.</li><li>Also fix for empty sheet generating invalid xlsx.</li></ul>
5 changes: 4 additions & 1 deletion lib/doc/workbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var Workbook = module.exports = function() {
this.created = new Date();
this.modified = this.created;
this._worksheets = [];
this.views = [];
this._definedNames = new DefinedNames();
};

Expand Down Expand Up @@ -139,7 +140,8 @@ Workbook.prototype = {
created: this.created,
modified: this.modified,
worksheets: this._worksheets.map(function(worksheet) { return worksheet.model; }),
definedNames: this._definedNames.model
definedNames: this._definedNames.model,
views: this.views
};
},
set model(value) {
Expand All @@ -164,5 +166,6 @@ Workbook.prototype = {
});

this._definedNames.model = value.definedNames;
this.views = value.views;
}
};
16 changes: 8 additions & 8 deletions lib/stream/xlsx/workbook-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var WorkbookWriter = module.exports = function(options) {
this._definedNames = new DefinedNames();

this._worksheets = [];
this._views = [];
this.views = [];

this.zip = Archiver('zip');
if (options.stream) {
Expand Down Expand Up @@ -284,17 +284,17 @@ WorkbookWriter.prototype = {
});
},
addWorkbook: function() {
var self = this;
var zip = this.zip;
var model = {
worksheets: this._worksheets,
definedNames: this._definedNames.model,
views: this.views
};

return new Bluebird(function(resolve) {
var model = {
worksheets: self._worksheets,
definedNames: self._definedNames.model
};

var xform = new WorkbookXform();
xform.prepare(model);
self.zip.append(xform.toXml(model), {name: '/xl/workbook.xml'});
zip.append(xform.toXml(model), {name: '/xl/workbook.xml'});
resolve();
});
},
Expand Down
7 changes: 1 addition & 6 deletions lib/stream/xlsx/worksheet-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,7 @@ var WorksheetWriter = module.exports = function(options) {
this._workbook = options.workbook;

// views
this._views = options.views || [{state: 'normal'}];
var workbookViews = this._workbook._views;
_.each(this._views, function(view) {
view.workbookViewId = workbookViews.length;
workbookViews.push(view);
});
this._views = options.views || [];

// start writing to stream now
this._writeOpenWorksheet();
Expand Down
23 changes: 13 additions & 10 deletions lib/xlsx/xform/book/workbook-view-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,38 @@ utils.inherits(WorkbookViewXform, BaseXform, {
xWindow: model.x || 0,
yWindow: model.y || 0,
windowWidth: model.width || 12000,
windowHeight: model.height || 24000
windowHeight: model.height || 24000,
firstSheet: model.firstSheet,
activeTab: model.activeTab
};
if (model.visibility && model.visibility !== 'visible') {
attributes.visibility = model.visibility;
}
if (model.active) {
attributes.activeTab = 1;
}
xmlStream.leafNode('workbookView', attributes);
},

parseOpen: function(node) {
if (node.name === 'workbookView') {
var model = this.model = {};
var addS = function(name, value, dflt) {
model[name] = (value !== undefined) ? model[name] = value : dflt;
var s = (value !== undefined) ? model[name] = value : dflt;
if (s !== undefined) {
model[name] = s;
}
};
var addN = function(name, value, dflt) {
model[name] = (value !== undefined) ? model[name] = parseInt(value) : dflt;
};
var addB = function(name, value, dflt) {
model[name] = (value !== undefined) ? model[name] = value === '1' : dflt;
var n = (value !== undefined) ? model[name] = parseInt(value) : dflt;
if (n !== undefined) {
model[name] = n;
}
};
addN('x', node.attributes.xWindow, 0);
addN('y', node.attributes.yWindow, 0);
addN('width', node.attributes.windowWidth, 25000);
addN('height', node.attributes.windowHeight, 10000);
addS('visibility', node.attributes.visibility, 'visible');
addB('active', node.attributes.activeTab, false);
addN('activeTab', node.attributes.activeTab, undefined);
addN('firstSheet', node.attributes.firstSheet, undefined);
return true;
} else {
return false;
Expand Down
19 changes: 0 additions & 19 deletions lib/xlsx/xform/book/workbook-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ utils.inherits(WorkbookXform, BaseXform, {

prepare: function(model) {
model.sheets = model.worksheets;

// prepare bookViews
var views = _.flatten(
model.worksheets.filter(function(worksheet) { return worksheet.views; })
.map(function(worksheet) { return worksheet.views; })
);
if (views && views.length) {
model.views = views;
_.each(views, function(view, index) { view.workbookViewId = index; });
}
},

render: function(xmlStream, model) {
Expand Down Expand Up @@ -177,14 +167,5 @@ utils.inherits(WorkbookXform, BaseXform, {
worksheet.name = sheet.name;
worksheet.id = sheet.id;
});

// reconcile bookViews
_.each(model.worksheets, function(sheet) {
_.each(sheet.views, function(view) {
var bookView = model.views[view.workbookViewId];
_.extend(view, bookView);
delete view.workbookViewId;
});
});
}
});
8 changes: 3 additions & 5 deletions lib/xlsx/xform/sheet/sheet-view-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ utils.inherits(SheetViewXform, BaseXform, {

render: function(xmlStream, model) {
xmlStream.openNode('sheetView', {
workbookViewId: model.workbookViewId,
tabSelected: 1
workbookViewId: model.workbookViewId || 0
});
var add = function(name, value, included) {
if (included) {
xmlStream.addAttribute(name, value);
}
};
add('tabSelected', '1', model.tabSelected);
add('showRuler', '0', model.showRuler === false);
add('showRowColHeaders', '0', model.showRowColHeaders === false);
add('showGridlines', '0', model.showGridlines === false);
Expand Down Expand Up @@ -188,7 +188,7 @@ utils.inherits(SheetViewXform, BaseXform, {
if (this.sheetView.style) {
model.style = this.sheetView.style;
}
} else if (this.sheetView.tabSelected) {
} else {
model = this.model = {
workbookViewId: this.sheetView.workbookViewId,
state: 'normal',
Expand All @@ -205,8 +205,6 @@ utils.inherits(SheetViewXform, BaseXform, {
if (this.sheetView.style) {
model.style = this.sheetView.style;
}
} else {
this.model = undefined;
}
return false;
default:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exceljs",
"version": "0.2.13",
"version": "0.2.14",
"description": "Excel Workbook Manager - Read and Write xlsx and csv Files.",
"private": false,
"license": "MIT",
Expand Down
Empty file added spec-out/README.md
Empty file.
Loading

0 comments on commit faf05db

Please sign in to comment.