-
-
Notifications
You must be signed in to change notification settings - Fork 35
Issue 158: Added TemplateFormulaCell #160
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
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c206f0c
Added a test for `TemplateFormulaCell`
Siemienik c4f3e72
Implement `TemplateFormulaCell`
Siemienik 39b9c8a
Describe `FormulaTemplateCell`
Siemienik 251d63a
Perf, use lazy preceding quantifier.
Siemienik 186ea1c
Increase test coverage by adding example with incorrect path into var…
Siemienik 96558e8
Fix typos
Siemienik ca53ec8
Update README.md
Siemienik edb276f
Merge branch 'master' into feature/158-template-formula-cell
Siemienik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { BaseCell } from './BaseCell'; | ||
import { Cell, CellFormulaValue, ValueType } from 'exceljs'; | ||
import { Scope } from '../Scope'; | ||
|
||
const variableRegex = /\${[^{]+?}/g; | ||
|
||
/** | ||
* @description | ||
* TemplateFormulaCell interpolate string and put it into cell as a formula | ||
* * starts width `#= ` | ||
*/ | ||
export class TemplateFormulaCell extends BaseCell { | ||
public static match(cell: Cell): boolean { | ||
return ( | ||
cell && | ||
cell.type === ValueType. String && | ||
(cell.isMerged ? cell.master.address === cell.address : true) && | ||
typeof cell.value === 'string' && | ||
cell.value.substring(0, 3) === '#= ' | ||
); | ||
} | ||
|
||
public apply(scope: Scope): TemplateFormulaCell { | ||
super.apply(scope); | ||
|
||
const template = scope | ||
.getCurrentTemplateString() | ||
.substring(3); | ||
|
||
const formula = template.replace(variableRegex, (match) => { | ||
const path = match.slice(2, -1).split('.'); | ||
|
||
// todo refactoring extract, similar like in VariableCell | ||
const value = path.reduce((p, c) => (typeof p === 'object' ? p[c] : p), scope.vm); | ||
if (value === undefined && !scope.isFrozen()) { | ||
// todo do it better (use logger or something like that) | ||
// tslint:disable-next-line:no-console | ||
console.warn( | ||
`WARN: ${path} is undefined for template formula output: ${ | ||
JSON.stringify(scope.outputCell) | ||
} when template is:${ | ||
JSON.stringify(scope.templateCell) | ||
}` | ||
); | ||
} | ||
|
||
return value; | ||
}); | ||
|
||
|
||
scope.setCurrentOutputValue({ formula } as CellFormulaValue); | ||
scope.incrementCol(); | ||
|
||
return this; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+7.73 KB
packages/xlsx-renderer/tests/integration/data/Renderer017-TemplateFormula/expected.xlsx
Binary file not shown.
Binary file added
BIN
+10 KB
packages/xlsx-renderer/tests/integration/data/Renderer017-TemplateFormula/template.xlsx
Binary file not shown.
29 changes: 29 additions & 0 deletions
29
packages/xlsx-renderer/tests/integration/data/Renderer017-TemplateFormula/viewModel.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"items": [ | ||
{ | ||
"name": "Item A", | ||
"weight": 13.5, | ||
"price": 12.1 | ||
}, | ||
{ | ||
"name": "Item B", | ||
"weight": 13.5, | ||
"price": 12 | ||
}, | ||
{ | ||
"name": "Item C", | ||
"weight": 3.5, | ||
"price": 12.7 | ||
}, | ||
{ | ||
"name": "Item D", | ||
"weight": 13, | ||
"price": 2.1 | ||
}, | ||
{ | ||
"name": "Item E", | ||
"weight": 1.5, | ||
"price": 32 | ||
} | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line was added, others lines have only changed
Matching Order