Skip to content

Commit

Permalink
WIP: #2 three classes migrated to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Siemienik committed Feb 15, 2020
1 parent 92b1613 commit f1f5bb4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 99 deletions.
12 changes: 6 additions & 6 deletions src/CellTemplatePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,7 +31,7 @@ export class CellTemplatePool {
// SumCell,
// AverageCell,
WsNameCell,
// DeleteCell,
DeleteCell,
];

protected instances: { [key: string]: BaseCell } = {};
Expand Down
22 changes: 0 additions & 22 deletions src/cell/DeleteCell.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/cell/DeleteCell.ts
Original file line number Diff line number Diff line change
@@ -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;
}

}
37 changes: 18 additions & 19 deletions src/cell/FormulaCell.js → src/cell/FormulaCell.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
52 changes: 0 additions & 52 deletions src/cell/HyperlinkCell.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/cell/HyperlinkCell.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit f1f5bb4

Please sign in to comment.