diff --git a/src/CellTemplatePool.ts b/src/CellTemplatePool.ts index 6350fe3..7cfdc27 100644 --- a/src/CellTemplatePool.ts +++ b/src/CellTemplatePool.ts @@ -10,19 +10,19 @@ import { FinishCell } from './cell/FinishCell'; import { EndRowCell } from './cell/EndRowCell'; // import SumCell from "./cell/SumCell"; // import AverageCell from "./cell/AverageCell"; -// import DeleteCell from "./cell/DeleteCell"; +import { DeleteCell } from './cell/DeleteCell'; // import DumpColsCell from "./cell/DumpColsCell"; import { WsNameCell } from './cell/WsNameCell'; -// import HyperlinkCell from "./cell/HyperlinkCell"; -// import FormulaCell from "./cell/FormulaCell"; +import { HyperlinkCell } from './cell/HyperlinkCell'; +import { FormulaCell } from './cell/FormulaCell'; export class CellTemplatePool { protected cells: CellType[] = [ NormalCell, EndRowCell, VariableCell, - // FormulaCell, - // HyperlinkCell, + FormulaCell, + HyperlinkCell, // ForEachCell, FinishCell, // EndLoopCell, @@ -31,7 +31,7 @@ export class CellTemplatePool { // SumCell, // AverageCell, WsNameCell, - // DeleteCell, + DeleteCell, ]; protected instances: { [key: string]: BaseCell } = {}; diff --git a/src/cell/DeleteCell.js b/src/cell/DeleteCell.js deleted file mode 100644 index 9251735..0000000 --- a/src/cell/DeleteCell.js +++ /dev/null @@ -1,22 +0,0 @@ -import BaseCell from "./BaseCell"; -import {ValueType} from "exceljs"; - -export default class DeleteCell extends BaseCell { - apply(scope) { - super.apply(scope); - - const target = scope.getCurrentTemplateValue().split(' ')[2]; - - scope.vm[target] = undefined; - - scope.setCurrentOutputValue(null); - scope.incrementCol(); - - return this; - } - - static match(cell) { - return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 9) === '#! DELETE'; - } - -} \ No newline at end of file diff --git a/src/cell/DeleteCell.ts b/src/cell/DeleteCell.ts new file mode 100644 index 0000000..cdb228d --- /dev/null +++ b/src/cell/DeleteCell.ts @@ -0,0 +1,25 @@ +import { BaseCell } from './BaseCell'; +import { Cell, ValueType } from 'exceljs'; +import { Scope } from '../Scope'; + +export class DeleteCell extends BaseCell { + public static match(cell: Cell): boolean { + return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 9) === '#! DELETE'; + } + + public apply(scope: Scope): DeleteCell { + super.apply(scope); + + const target = scope.getCurrentTemplateValue()?.toString().split(' ')[2]; //todo make some function for scope.getCurrentTemplateValue()?.toString().split(' ') ; + + if (target == undefined) return this; //it's ok here + + scope.vm[target] = undefined; + + scope.setCurrentOutputValue(null); + scope.incrementCol(); + + return this; + } + +} \ No newline at end of file diff --git a/src/cell/FormulaCell.js b/src/cell/FormulaCell.ts similarity index 67% rename from src/cell/FormulaCell.js rename to src/cell/FormulaCell.ts index 91c1e55..e7026a2 100644 --- a/src/cell/FormulaCell.js +++ b/src/cell/FormulaCell.ts @@ -1,19 +1,29 @@ -import BaseCell from "./BaseCell"; -import {ValueType} from "exceljs"; +import { BaseCell } from './BaseCell'; +import { Cell, CellFormulaValue, ValueType } from 'exceljs'; +import { Scope } from '../Scope'; + +export class FormulaCell extends BaseCell { + /** + * @inheritDoc + * @param {Cell} cell + * @returns {boolean} + */ + public static match(cell: Cell): boolean { + return cell && cell.type === ValueType.Formula; + } -class FormulaCell extends BaseCell { /** * @inheritDoc * @param {Scope} scope * @returns {FormulaCell} */ - apply(scope) { + public apply(scope: Scope): FormulaCell { super.apply(scope); const shift = scope.outputCell.r - scope.templateCell.r; const regex = /([a-zA-Z]+)([1-9][0-9]*)/g; - const value = scope.getCurrentTemplateValue(); + const value = scope.getCurrentTemplateValue() as CellFormulaValue; let formula = value.formula; //todo extract method match addresses @@ -25,26 +35,15 @@ class FormulaCell extends BaseCell { addresses.reverse(); //todo extract method getShiftedFormula - let formulaChars = [...formula]; + let formulaChars = Array.from(formula); addresses.forEach(a => formulaChars.splice(a.index, a.len, `${a.col}${a.row + shift}`)); formula = formulaChars.join(''); - scope.setCurrentOutputValue({formula}); + scope.setCurrentOutputValue({ formula } as CellFormulaValue); scope.incrementCol(); return this; } - /** - * @inheritDoc - * @param {Cell} cell - * @returns {boolean} - */ - static match(cell) { - return cell && cell.type === ValueType.Formula; - } - -} - -export default FormulaCell \ No newline at end of file +} \ No newline at end of file diff --git a/src/cell/HyperlinkCell.js b/src/cell/HyperlinkCell.js deleted file mode 100644 index ad0c351..0000000 --- a/src/cell/HyperlinkCell.js +++ /dev/null @@ -1,52 +0,0 @@ -import BaseCell from "./BaseCell"; -import Scope from "../Scope"; -import {ValueType} from "exceljs"; - -export default class HyperlinkCell extends BaseCell { - /** - * @param {Scope} scope - * @returns {HyperlinkCell} - */ - apply(scope) { - super.apply(scope); - - scope.setCurrentOutputValue(null); - - const url = HyperlinkCell._getUrlParam(scope).split('.').reduce((p, c) => p[c] || {}, scope.vm); - if (typeof url === 'string') { - const label = HyperlinkCell._getLabelParam(scope).split('.').reduce((p, c) => p[c] || {}, scope.vm) || url; - console.log(label); - scope.setCurrentOutputValue({text: label, hyperlink: url}); - } - - scope.incrementCol(); - - return this; - } - - /** - * @param {Scope} scope - * @returns {string} - * @protected - */ - static _getLabelParam(scope) { - return scope.getCurrentTemplateValue().split(' ')[2]; - } - - /** - * @param {Scope} scope - * @returns {string} - * @protected - */ - static _getUrlParam(scope) { - return scope.getCurrentTemplateValue().split(' ')[3]; - } - - /** - * @param {Cell} cell - * @returns {boolean} - */ - static match(cell) { - return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 12) === '#! HYPERLINK'; - } -} \ No newline at end of file diff --git a/src/cell/HyperlinkCell.ts b/src/cell/HyperlinkCell.ts new file mode 100644 index 0000000..b14d6d2 --- /dev/null +++ b/src/cell/HyperlinkCell.ts @@ -0,0 +1,34 @@ +import { BaseCell } from './BaseCell'; +import { Scope } from '../Scope'; +import { Cell, ValueType } from 'exceljs'; + +export class HyperlinkCell extends BaseCell { + public static match(cell: Cell): boolean { + return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 12) === '#! HYPERLINK'; + } + + protected static getLabelParam(scope: Scope): string { + return scope.getCurrentTemplateValue()?.toString().split(' ')[2] || ''; + } + + protected static getUrlParam(scope: Scope): string { + return scope.getCurrentTemplateValue()?.toString().split(' ')[3] || ''; + } + + public apply(scope: Scope): HyperlinkCell { + super.apply(scope); + + scope.setCurrentOutputValue(null); + + const url = HyperlinkCell.getUrlParam(scope).split('.').reduce((p, c) => p[c] || {}, scope.vm); + if (typeof url === 'string') { + const label = HyperlinkCell.getLabelParam(scope).split('.').reduce((p, c) => p[c] || {}, scope.vm) || url; + console.log(label); + scope.setCurrentOutputValue({ text: label, hyperlink: url }); + } + + scope.incrementCol(); + + return this; + } +} \ No newline at end of file