-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 152: Added string template cell (#161)
* Added `TemplateStringCell` test * Added `TemplateStringCell` implementation * Describe `TemplateStringCell` in README.md * Update xlsx-renderer readme
- Loading branch information
Showing
7 changed files
with
83 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 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, ValueType } from 'exceljs'; | ||
import { Scope } from '../Scope'; | ||
|
||
const variableRegex = /\${[^{]+?}/g; | ||
|
||
/** | ||
* @description | ||
* TemplateStringCellinterpolate string and put it into cell as a string value | ||
* * starts width <pre>#` </pre> | ||
*/ | ||
export class TemplateStringCell 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): TemplateStringCell { | ||
super.apply(scope); | ||
|
||
const template = scope | ||
.getCurrentTemplateString() | ||
.substring(3); | ||
|
||
const result = 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 string output: ${ | ||
JSON.stringify(scope.outputCell) | ||
} when template is:${ | ||
JSON.stringify(scope.templateCell) | ||
}` | ||
); | ||
} | ||
|
||
return value; | ||
}); | ||
|
||
// todo refactoring: this is only one line which the logic is different as in TemplateFormulaCell, consider about refactoring | ||
scope.setCurrentOutputValue(result); | ||
scope.incrementCol(); | ||
|
||
return this; | ||
} | ||
} |
Binary file added
BIN
+6.9 KB
packages/xlsx-renderer/tests/integration/data/Renderer018-TemplateString/expected.xlsx
Binary file not shown.
Binary file added
BIN
+9.07 KB
packages/xlsx-renderer/tests/integration/data/Renderer018-TemplateString/template.xlsx
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
packages/xlsx-renderer/tests/integration/data/Renderer018-TemplateString/viewModel.json
This file contains 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,7 @@ | ||
{ | ||
"author": { | ||
"firstName": "Paweł", | ||
"lastName": "Siemienik", | ||
"hobby": "OSS development" | ||
} | ||
} |