Skip to content

#951: Force formula re-calculation on file open from Excel #980

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

Merged
merged 3 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,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 @@ -1170,6 +1170,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 Expand Up @@ -1413,6 +1420,11 @@ export class Workbook {
lastPrinted: Date;
properties: WorkbookProperties;

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

/**
* xlsx file format operations
*/
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 @@ -268,6 +268,7 @@ class WorkbookWriter {
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
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);
});