Skip to content

Commit

Permalink
exceljs#951: Force formula re-calculation on file open from Excel
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymon Lubieniecki authored and Szymon Lubieniecki committed Oct 3, 2019
1 parent fb0e9ea commit 9f949d3
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ workbook.lastPrinted = new Date(2016, 9, 27);
workbook.properties.date1904 = true;
```

## Set Calculation Properties

```javascript
// Force workbook calculation on load
workbook.calcProperties.fullCalcOnLoad = true;
```

## Workbook Views

The Workbook views controls how many separate windows Excel will open when viewing the workbook.
Expand Down
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,11 @@ export interface Worksheet {
*/
properties: WorksheetProperties;

/**
* Workbook calculation Properties
*/
calcProperties: CalculationProperties;

/**
* Open panes representing the sheet
*/
Expand Down Expand Up @@ -1169,6 +1174,13 @@ export interface Worksheet {
getTable(name: string): Table;
}

export interface CalculationProperties {
/**
* Whether the application shall perform a full recalculation when the workbook is opened
*/
fullCalcOnLoad: boolean
}

export interface WorksheetProperties {
/**
* Color of the tab
Expand Down
3 changes: 3 additions & 0 deletions lib/doc/workbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Workbook {
this.created = new Date();
this.modified = this.created;
this.properties = {};
this.calcProperties = {};
this._worksheets = [];
this.views = [];
this.media = [];
Expand Down Expand Up @@ -170,6 +171,7 @@ class Workbook {
contentStatus: this.contentStatus,
themes: this._themes,
media: this.media,
calcProperties: this.calcProperties,
};
}

Expand All @@ -191,6 +193,7 @@ class Workbook {
this.contentStatus = value.contentStatus;

this.properties = value.properties;
this.calcProperties = value.calcProperties;
this._worksheets = [];
value.worksheets.forEach(worksheetModel => {
const {id, name, state} = worksheetModel;
Expand Down
1 change: 1 addition & 0 deletions lib/stream/xlsx/workbook-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ WorkbookWriter.prototype = {
definedNames: this._definedNames.model,
views: this.views,
properties: {},
calcProperties: {},
};

return new Promise(resolve => {
Expand Down
26 changes: 26 additions & 0 deletions lib/xlsx/xform/book/workbook-calc-properties-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const BaseXform = require('../base-xform');

class WorkbookCalcPropertiesXform extends BaseXform {
render(xmlStream, model) {
xmlStream.leafNode('calcPr', {
calcId: 171027,
fullCalcOnLoad: model.fullCalcOnLoad ? 1 : undefined,
});
}

parseOpen(node) {
if (node.name === 'calcPr') {
this.model = {};
return true;
}
return false;
}

parseText() {}

parseClose() {
return false;
}
}

module.exports = WorkbookCalcPropertiesXform;
7 changes: 4 additions & 3 deletions lib/xlsx/xform/book/workbook-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DefinedNameXform = require('./defined-name-xform');
const SheetXform = require('./sheet-xform');
const WorkbookViewXform = require('./workbook-view-xform');
const WorkbookPropertiesXform = require('./workbook-properties-xform');
const WorkbookCalcPropertiesXform = require('./workbook-calc-properties-xform');

class WorkbookXform extends BaseXform {
constructor() {
Expand All @@ -21,7 +22,7 @@ class WorkbookXform extends BaseXform {
bookViews: new ListXform({tag: 'bookViews', count: false, childXform: new WorkbookViewXform()}),
sheets: new ListXform({tag: 'sheets', count: false, childXform: new SheetXform()}),
definedNames: new ListXform({tag: 'definedNames', count: false, childXform: new DefinedNameXform()}),
calcPr: WorkbookXform.STATIC_XFORMS.calcPr,
calcPr: new WorkbookCalcPropertiesXform(),
};
}

Expand Down Expand Up @@ -84,7 +85,7 @@ class WorkbookXform extends BaseXform {
this.map.bookViews.render(xmlStream, model.views);
this.map.sheets.render(xmlStream, model.sheets);
this.map.definedNames.render(xmlStream, model.definedNames);
this.map.calcPr.render(xmlStream);
this.map.calcPr.render(xmlStream, model.calcProperties);

xmlStream.closeNode();
}
Expand Down Expand Up @@ -125,6 +126,7 @@ class WorkbookXform extends BaseXform {
sheets: this.map.sheets.model,
properties: this.map.workbookPr.model || {},
views: this.map.bookViews.model,
calcProperties: {},
};
if (this.map.definedNames.model) {
this.model.definedNames = this.map.definedNames.model;
Expand Down Expand Up @@ -231,7 +233,6 @@ WorkbookXform.STATIC_XFORMS = {
tag: 'fileVersion',
$: {appName: 'xl', lastEdited: 5, lowestEdited: 5, rupBuild: 9303},
}),
calcPr: new StaticXform({tag: 'calcPr', $: {calcId: 171027}}),
};

module.exports = WorkbookXform;
1 change: 1 addition & 0 deletions lib/xlsx/xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class XLSX {
model.definedNames = workbook.definedNames;
model.views = workbook.views;
model.properties = workbook.properties;
model.calcProperties = workbook.calcProperties;
break;
}

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": "3.0.0",
"version": "3.0.1",
"description": "Excel Workbook Manager - Read and Write xlsx and csv Files.",
"private": false,
"license": "MIT",
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/xlsx/xform/book/data/book.1.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
],
"properties": {
"date1904": true
},
"calcProperties": {
"fullCalcOnLoad": true
}
}
2 changes: 1 addition & 1 deletion spec/unit/xlsx/xform/book/data/book.1.2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<sheet name="Hidden" sheetId="2" r:id="rId2" state="hidden"/>
<sheet name="VeryHidden" sheetId="3" r:id="rId3" state="veryHidden"/>
</sheets>
<calcPr calcId="171027"/>
<calcPr calcId="171027" fullCalcOnLoad="1"/>
</workbook>
3 changes: 2 additions & 1 deletion spec/unit/xlsx/xform/book/data/book.1.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
],
"properties": {
"date1904": true
}
},
"calcProperties": {}
}
5 changes: 3 additions & 2 deletions spec/unit/xlsx/xform/book/data/book.2.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"sheets": [
{"name": "Values", "id": 1, "rId": "rId1"}
],
"properties": {}
}
"properties": {},
"calcProperties": {}
}
32 changes: 32 additions & 0 deletions spec/unit/xlsx/xform/book/workbook-calc-properties-xform.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const testXformHelper = require('./../test-xform-helper');

const WorkbookCalcPropertiesXform = verquire('xlsx/xform/book/workbook-calc-properties-xform');

const expectations = [
{
title: 'default',
create() {
return new WorkbookCalcPropertiesXform();
},
preparedModel: {},
xml:
'<calcPr calcId="171027"></calcPr>',
parsedModel: {},
tests: ['render', 'renderIn'],
},
{
title: 'fullCalcOnLoad',
create() {
return new WorkbookCalcPropertiesXform();
},
preparedModel: {fullCalcOnLoad: true},
xml:
'<calcPr calcId="171027" fullCalcOnLoad="1"></calcPr>',
parsedModel: {},
tests: ['render', 'renderIn', 'parse'],
},
];

describe('WorkbookCalcPropertiesXform', () => {
testXformHelper(expectations);
});

0 comments on commit 9f949d3

Please sign in to comment.