Skip to content

Commit c16053b

Browse files
committed
refactor: helper-name, workbook-table
1 parent 325a293 commit c16053b

File tree

7 files changed

+132
-131
lines changed

7 files changed

+132
-131
lines changed

src/helper/cell-id-helper.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Thanks to Nathan Wall
2+
// https://stackoverflow.com/questions/12504042/what-is-a-method-that-can-be-used-to-increment-letters#12504061
3+
4+
export default class CellIdHelper {
5+
private _chars: string;
6+
private _nextId: number[];
7+
8+
constructor(chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
9+
this._chars = chars;
10+
this._nextId = [0];
11+
}
12+
13+
start(index: number): this {
14+
this._nextId = [index];
15+
return this;
16+
}
17+
18+
next(): string {
19+
const r = [];
20+
for (const char of this._nextId) {
21+
r.unshift(this._chars[char]);
22+
}
23+
this._increment();
24+
return r.join('');
25+
}
26+
27+
_increment(): void {
28+
for (let i = 0; i < this._nextId.length; i++) {
29+
const val = ++this._nextId[i];
30+
if (val >= this._chars.length) {
31+
this._nextId[i] = 0;
32+
} else {
33+
return;
34+
}
35+
}
36+
this._nextId.push(0);
37+
}
38+
39+
// eslint-disable-next-line
40+
*[Symbol.iterator]() {
41+
while (true) {
42+
yield this.next();
43+
}
44+
}
45+
46+
static increment(letterNumber: number): string {
47+
const Generator = new this('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
48+
return Generator.start(letterNumber).next();
49+
}
50+
51+
static setRange(range: string, colId: number, length?: number): string {
52+
const info = range.split('!');
53+
const spans = info[1].split(':');
54+
const start = spans[0].split('$');
55+
const startRow = Number(spans[0].split('$')[2]);
56+
57+
const colLetter = CellIdHelper.increment(colId);
58+
59+
let endCell = '';
60+
if (length !== undefined) {
61+
const endRow = String(startRow + length - 1);
62+
endCell = `:$${colLetter}$${endRow}`;
63+
}
64+
65+
const newRange = `${info[0]}!$${colLetter}$${start[2]}${endCell}`;
66+
return newRange
67+
}
68+
69+
static getSpanString(
70+
startColNumber: number,
71+
startRowNumber: number,
72+
cols: number,
73+
rows: number,
74+
): string {
75+
const startColLetter = CellIdHelper.increment(startColNumber);
76+
const endColLetter = CellIdHelper.increment(startColNumber + cols);
77+
const endRowNumber = startRowNumber + rows;
78+
return `${startColLetter}${startRowNumber}:${endColLetter}${endRowNumber}`;
79+
}
80+
81+
static getCellAddressString(c: number, r: number): string {
82+
const colLetter = CellIdHelper.increment(c);
83+
return `${colLetter}${r + 1}`;
84+
}
85+
}

src/helper/string-id-generator.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/modify/chart.ts

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
import {
22
ChartData,
33
ChartColumn,
4-
ModificationPatternChildren,
4+
ModificationTags,
55
} from '../types/chart-types';
66
import { GeneralHelper } from '../helper/general-helper';
77
import { XmlHelper } from '../helper/xml-helper';
8-
import StringIdGenerator from '../helper/string-id-generator';
8+
import StringIdGenerator from '../helper/cell-id-helper';
99

1010
export class ModifyChart {
1111
root: XMLDocument;
12-
StringIdGenerator: StringIdGenerator;
1312
data: ChartData;
1413
height: number;
1514
width: number;
1615
columns: ChartColumn[];
1716

1817
constructor(root: XMLDocument, data: ChartData, columns: ChartColumn[]) {
1918
this.root = root;
20-
this.StringIdGenerator = new StringIdGenerator(
21-
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
22-
);
2319
this.data = data;
2420
this.columns = GeneralHelper.arrayify(columns);
2521
this.height = this.data.categories.length;
2622
this.width = this.columns.length;
2723
}
2824

2925
pattern(
30-
pattern: ModificationPatternChildren,
26+
tags: ModificationTags,
3127
root?: XMLDocument | Element,
3228
): void {
3329
root = root || this.root;
3430

35-
for (const tag in pattern) {
36-
const parentPattern = pattern[tag];
31+
for (const tag in tags) {
32+
const parentPattern = tags[tag];
3733
const index = parentPattern.index || 0;
3834
this.assertNode(root.getElementsByTagName(tag), index);
3935
const element = root.getElementsByTagName(tag)[index];
@@ -71,47 +67,10 @@ export class ModifyChart {
7167
};
7268

7369
range = (series: number, length?: number) => (element: Element): void => {
74-
this.setRange(element, series, length);
70+
const range = element.firstChild.textContent
71+
element.firstChild.textContent = StringIdGenerator.setRange(range, series, length);;
7572
};
7673

77-
setRange(element: Element, colId: number, length?: number): void {
78-
const range = element.firstChild.textContent;
79-
const info = range.split('!');
80-
const spans = info[1].split(':');
81-
const start = spans[0].split('$');
82-
const startRow = Number(spans[0].split('$')[2]);
83-
const colLetter = this.StringIdGenerator.start(colId).next();
84-
85-
let endCell = '';
86-
if (length !== undefined) {
87-
const endRow = String(startRow + length - 1);
88-
endCell = `:$${colLetter}$${endRow}`;
89-
}
90-
91-
const newRange = `${info[0]}!$${colLetter}$${start[2]}${endCell}`;
92-
93-
element.firstChild.textContent = newRange;
94-
}
95-
96-
getSpanString(
97-
startColNumber: number,
98-
startRowNumber: number,
99-
cols: number,
100-
rows: number,
101-
): string {
102-
const startColLetter = this.StringIdGenerator.start(startColNumber).next();
103-
const endColLetter = this.StringIdGenerator.start(
104-
startColNumber + cols,
105-
).next();
106-
const endRowNumber = startRowNumber + rows;
107-
return `${startColLetter}${startRowNumber}:${endColLetter}${endRowNumber}`;
108-
}
109-
110-
getCellAddressString(c: number, r: number): string {
111-
const colLetter = this.StringIdGenerator.start(c).next();
112-
return `${colLetter}${r + 1}`;
113-
}
114-
11574
assertNode(collection: HTMLCollectionOf<Element>, index: number): void {
11675
if (!collection[index]) {
11776
const tplNode = collection[collection.length - 1];

src/modify/chartspace.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ChartData,
33
ChartColumn,
44
ModificationPattern,
5-
ModificationPatternChildren,
5+
ModificationTags,
66
} from '../types/chart-types';
77
import { ModifyChart } from './chart';
88

@@ -46,8 +46,8 @@ export class ModifyChartspace extends ModifyChart {
4646

4747
series = (
4848
index: number,
49-
children: ModificationPatternChildren,
50-
): ModificationPatternChildren => {
49+
children: ModificationTags,
50+
): ModificationTags => {
5151
return {
5252
'c:ser': {
5353
index: index,
@@ -56,7 +56,7 @@ export class ModifyChartspace extends ModifyChart {
5656
};
5757
};
5858

59-
seriesId = (series: number): ModificationPatternChildren => {
59+
seriesId = (series: number): ModificationTags => {
6060
return {
6161
'c:idx': {
6262
modify: this.attribute('val', series),
@@ -70,7 +70,7 @@ export class ModifyChartspace extends ModifyChart {
7070
seriesLabel = (
7171
label: string,
7272
series: number,
73-
): ModificationPatternChildren => {
73+
): ModificationTags => {
7474
return {
7575
'c:f': {
7676
modify: this.range(series + 1),
@@ -86,7 +86,7 @@ export class ModifyChartspace extends ModifyChart {
8686
value: number,
8787
index: number,
8888
series: number,
89-
): ModificationPatternChildren => {
89+
): ModificationTags => {
9090
return {
9191
'c:val': this.point(value, index, series + 1),
9292
'c:cat': this.point(index, 0, label),

src/modify/workbook-table.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1+
import CellIdHelper from '../helper/cell-id-helper';
12
import { ChartData, ChartColumn } from '../types/chart-types';
23
import { Workbook } from '../types/types';
34
import { ModifyChart } from './chart';
45

56
export class ModifyWorkbookTable extends ModifyChart {
6-
sharedStrings: XMLDocument;
7-
table: Workbook['table'];
8-
97
constructor(workbook: Workbook, data: ChartData, columns?: ChartColumn[]) {
108
super(workbook.table, data, columns);
11-
this.sharedStrings = workbook.sharedStrings;
12-
this.table = workbook.table;
139
}
1410

1511
setWorkbookTable(): void {
16-
this.table
17-
.getElementsByTagName('table')[0]
18-
.setAttribute('ref', this.getSpanString(0, 1, this.width, this.height));
19-
20-
const tableColumns = this.table.getElementsByTagName('tableColumns')[0];
21-
tableColumns.setAttribute('count', String(this.width + 1));
12+
this.pattern({
13+
'table': {
14+
modify: this.attribute('ref', CellIdHelper.getSpanString(0, 1, this.width, this.height))
15+
},
16+
'tableColumns': {
17+
modify: this.attribute('count', this.width + 1)
18+
}
19+
})
2220
}
2321

2422
setWorkbookTableColumn(c: number, label: string): void {
25-
const tableColumns = this.table.getElementsByTagName('tableColumns')[0];
26-
this.assertNode(tableColumns.getElementsByTagName('tableColumn'), c);
27-
28-
const tableColumn = tableColumns.getElementsByTagName('tableColumn')[c];
29-
tableColumn.setAttribute('id', String(c + 1));
30-
tableColumn.setAttribute('name', label);
23+
this.pattern({
24+
'tableColumn': {
25+
index: c,
26+
modify: [
27+
this.attribute('id', c + 1),
28+
this.attribute('name', label),
29+
]
30+
}
31+
})
3132
}
3233
}

0 commit comments

Comments
 (0)