Skip to content

Commit 1ad8a11

Browse files
authored
Merge pull request #148 from ghiscoding/chore/optimize-code
chore: optimize a few files for perf/size and add more typings
2 parents 9af6d56 + 5f752da commit 1ad8a11

File tree

13 files changed

+256
-220
lines changed

13 files changed

+256
-220
lines changed

packages/excel-builder-vanilla-types/dist/index.d.ts

Lines changed: 106 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ declare class TextNode {
3131
toString(): string;
3232
}
3333
export declare class XMLNode {
34-
nodeName: string;
35-
children: XMLNode[];
34+
readonly nodeName: string;
35+
readonly children: XMLNode[];
3636
nodeValue: string;
3737
attributes: {
3838
[key: string]: any;
@@ -83,21 +83,27 @@ export declare class TwoCellAnchor {
8383
toXML(xmlDoc: XMLDOM, content: any): XMLNode;
8484
}
8585
export interface AnchorOption {
86-
/** X offset in EMU's */
86+
/** X offset in EMUs (English Metric Units) */
8787
x: number;
88-
/** Y offset in EMU's */
88+
/** Y offset in EMUs (English Metric Units) */
8989
y: number;
90+
/** Optional: X offset as boolean (for cell anchoring) */
9091
xOff?: boolean;
92+
/** Optional: Y offset as boolean (for cell anchoring) */
9193
yOff?: boolean;
92-
/** Width in EMU's */
93-
height: number;
94-
/** Height in EMU's */
95-
width: number;
94+
/** Width in EMUs */
95+
width?: number;
96+
/** Height in EMUs */
97+
height?: number;
98+
/** Reference to parent Drawing instance */
9699
drawing?: Drawing;
97100
}
98101
export interface DualAnchorOption {
102+
/** Ending anchor position (AnchorOption) */
99103
to: AnchorOption;
104+
/** Starting anchor position (AnchorOption) */
100105
from: AnchorOption;
106+
/** Reference to parent Drawing instance */
101107
drawing?: Drawing;
102108
}
103109
/**
@@ -114,7 +120,7 @@ export declare class Drawing {
114120
* @param {Object} config Shorthand - pass the created anchor coords that can normally be used to construct it.
115121
* @returns {Anchor}
116122
*/
117-
createAnchor(type: "absoluteAnchor" | "oneCellAnchor" | "twoCellAnchor", config: any): AbsoluteAnchor | OneCellAnchor | TwoCellAnchor;
123+
createAnchor(type: "absoluteAnchor" | "oneCellAnchor" | "twoCellAnchor", config: Partial<AnchorOption | DualAnchorOption>): AbsoluteAnchor | OneCellAnchor | TwoCellAnchor;
118124
}
119125
/**
120126
*
@@ -157,6 +163,24 @@ export declare class AbsoluteAnchor {
157163
export type ExcelColorStyle = string | {
158164
theme: number;
159165
};
166+
export type BorderStyle = {
167+
style?: string;
168+
color?: string | {
169+
theme: number;
170+
[key: string]: any;
171+
};
172+
};
173+
export interface BorderInstruction {
174+
top?: BorderStyle;
175+
left?: BorderStyle;
176+
right?: BorderStyle;
177+
bottom?: BorderStyle;
178+
diagonal?: BorderStyle;
179+
outline?: boolean;
180+
diagonalUp?: boolean;
181+
diagonalDown?: boolean;
182+
id?: number;
183+
}
160184
export interface ExcelAlignmentStyle {
161185
/** Horizontal alignment of cell content */
162186
horizontal?: "center" | "fill" | "general" | "justify" | "left" | "right";
@@ -561,7 +585,12 @@ export type Relation = {
561585
[id: string]: {
562586
id: string;
563587
schema: string;
564-
object: any;
588+
object: {
589+
id: string;
590+
target?: string | null;
591+
targetMode?: string;
592+
[key: string]: any;
593+
};
565594
data?: {
566595
id: number;
567596
schema: string;
@@ -586,9 +615,13 @@ export declare class RelationshipManager {
586615
};
587616
addRelation(object: {
588617
id: string;
618+
target?: string | null;
619+
targetMode?: string;
589620
}, type: keyof typeof Util.schemas): string;
590621
getRelationshipId(object: {
591622
id: string;
623+
target?: string | null;
624+
targetMode?: string;
592625
}): string | null;
593626
toXML(): XMLDOM;
594627
}
@@ -675,40 +708,16 @@ declare class StyleSheet$1 {
675708
*/
676709
createTableStyle(instructions: any): void;
677710
/**
678-
* All params optional
679-
* Expects: {
680-
* top: {},
681-
* left: {},
682-
* right: {},
683-
* bottom: {},
684-
* diagonal: {},
685-
* outline: boolean,
686-
* diagonalUp: boolean,
687-
* diagonalDown: boolean
688-
* }
689-
* Each border should follow:
711+
* All params optional. each border should follow:
690712
* {
691713
* style: styleString, http://www.schemacentral.com/sc/ooxml/t-ssml_ST_BorderStyle.html
692714
* color: ARBG color (requires the A, so for example FF006666)
693715
* }
694716
* @param {Object} border
695717
*/
696-
createBorderFormatter(border: any): any;
718+
createBorderFormatter(border: BorderInstruction): BorderInstruction;
697719
/**
698-
* Supported font styles:
699-
* bold
700-
* italic
701-
* underline (single, double, singleAccounting, doubleAccounting)
702-
* size
703-
* color
704-
* fontName
705-
* strike (strikethrough)
706-
* outline (does this actually do anything?)
707-
* shadow (does this actually do anything?)
708-
* superscript
709-
* subscript
710-
*
711-
* Color is a future goal - at the moment it's looking a bit complicated
720+
* Font styles, color is a future goal - at the moment it's looking a bit complicated
712721
* @param {Object} instructions
713722
*/
714723
createFontStyle(instructions: ExcelFontStyle): any;
@@ -752,24 +761,36 @@ export declare class Table {
752761
id: string;
753762
tableId: string;
754763
displayName: string;
755-
dataCellStyle: any;
764+
dataCellStyle: string | null;
756765
dataDfxId: number | null;
757766
headerRowBorderDxfId: number | null;
758-
headerRowCellStyle: any;
767+
headerRowCellStyle: string | null;
759768
headerRowCount: number;
760769
headerRowDxfId: number | null;
761770
insertRow: boolean;
762771
insertRowShift: boolean;
763-
ref: any;
772+
ref: [
773+
number[],
774+
number[]
775+
] | null;
764776
tableBorderDxfId: number | null;
765777
totalsRowBorderDxfId: number | null;
766-
totalsRowCellStyle: any;
778+
totalsRowCellStyle: string | null;
767779
totalsRowCount: number;
768780
totalsRowDxfId: number | null;
769-
tableColumns: any;
770-
autoFilter: any;
771-
sortState: any;
772-
styleInfo: any;
781+
tableColumns: ExcelTableColumn[];
782+
autoFilter: [
783+
number[],
784+
number[]
785+
] | null;
786+
sortState: ExcelSortState | null;
787+
styleInfo: {
788+
themeStyle?: string;
789+
showFirstColumn?: boolean;
790+
showLastColumn?: boolean;
791+
showColumnStripes?: boolean;
792+
showRowStripes?: boolean;
793+
};
773794
constructor(config?: any);
774795
initialize(config: any): void;
775796
setReferenceRange(start: number[], end: number[]): void;
@@ -881,21 +902,23 @@ export declare class Worksheet {
881902
name: string;
882903
id: string;
883904
_timezoneOffset: number;
884-
relations: any;
905+
relations: RelationshipManager | null;
885906
columnFormats: ExcelColumn[];
886907
data: (number | string | boolean | Date | null | ExcelColumnMetadata)[][];
887908
mergedCells: string[][];
888909
columns: ExcelColumn[];
889-
sheetProtection: any;
910+
sheetProtection: {
911+
exportXML: (doc: XMLDOM) => XMLNode;
912+
} | false;
890913
_headers: [
891-
left?: any,
892-
center?: any,
893-
right?: any
914+
left?: string | CharType | any[],
915+
center?: string | CharType | any[],
916+
right?: string | CharType | any[]
894917
];
895918
_footers: [
896-
left?: any,
897-
center?: any,
898-
right?: any
919+
left?: string | CharType | any[],
920+
center?: string | CharType | any[],
921+
right?: string | CharType | any[]
899922
];
900923
_tables: Table[];
901924
_drawings: Array<Table | Drawings>;
@@ -923,20 +946,39 @@ export declare class Worksheet {
923946
* @returns {Object}
924947
*/
925948
exportData(): {
926-
relations: any;
949+
relations: {
950+
relations: {
951+
[id: string]: {
952+
id: string;
953+
schema: string;
954+
object: {
955+
id: string;
956+
target?: string | null;
957+
targetMode?: string;
958+
[key: string]: any;
959+
};
960+
data?: {
961+
id: number;
962+
schema: string;
963+
object: any;
964+
};
965+
};
966+
};
967+
lastId: number;
968+
} | undefined;
927969
columnFormats: ExcelColumn[];
928970
data: (string | number | boolean | Date | ExcelColumnMetadata | null)[][];
929971
columns: ExcelColumn[];
930972
mergedCells: string[][];
931973
_headers: [
932-
left?: any,
933-
center?: any,
934-
right?: any
974+
left?: string | any[] | CharType | undefined,
975+
center?: string | any[] | CharType | undefined,
976+
right?: string | any[] | CharType | undefined
935977
];
936978
_footers: [
937-
left?: any,
938-
center?: any,
939-
right?: any
979+
left?: string | any[] | CharType | undefined,
980+
center?: string | any[] | CharType | undefined,
981+
right?: string | any[] | CharType | undefined
940982
];
941983
_tables: Table[];
942984
_rowInstructions: any;
@@ -1154,7 +1196,10 @@ export declare class Workbook {
11541196
media: {
11551197
[filename: string]: MediaMeta;
11561198
};
1157-
printTitles: any;
1199+
printTitles?: Record<string, {
1200+
top?: number;
1201+
left?: string;
1202+
}>;
11581203
constructor();
11591204
initialize(): void;
11601205
createWorksheet(config?: any): Worksheet;

packages/excel-builder-vanilla/src/Excel/Drawing/AbsoluteAnchor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { AnchorOption } from './Drawing.js';
21
import { Util } from '../Util.js';
32
import type { XMLDOM } from '../XMLDOM.js';
3+
import type { AnchorOption } from './Drawing.js';
44

55
/**
66
*
@@ -20,7 +20,7 @@ export class AbsoluteAnchor {
2020
constructor(config: AnchorOption) {
2121
if (config) {
2222
this.setPos(config.x, config.y);
23-
this.setDimensions(config.width, config.height);
23+
this.setDimensions(config.width || 0, config.height || 0);
2424
}
2525
}
2626

packages/excel-builder-vanilla/src/Excel/Drawing/Drawing.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@ import { TwoCellAnchor } from './TwoCellAnchor.js';
55
// import { Picture } from './Picture.js';
66

77
export interface AnchorOption {
8-
/** X offset in EMU's */
8+
/** X offset in EMUs (English Metric Units) */
99
x: number;
10-
/** Y offset in EMU's */
10+
/** Y offset in EMUs (English Metric Units) */
1111
y: number;
12+
/** Optional: X offset as boolean (for cell anchoring) */
1213
xOff?: boolean;
14+
/** Optional: Y offset as boolean (for cell anchoring) */
1315
yOff?: boolean;
14-
/** Width in EMU's */
15-
height: number;
16-
/** Height in EMU's */
17-
width: number;
16+
/** Width in EMUs */
17+
width?: number;
18+
/** Height in EMUs */
19+
height?: number;
20+
/** Reference to parent Drawing instance */
1821
drawing?: Drawing;
1922
}
2023

2124
export interface DualAnchorOption {
25+
/** Ending anchor position (AnchorOption) */
2226
to: AnchorOption;
27+
/** Starting anchor position (AnchorOption) */
2328
from: AnchorOption;
29+
/** Reference to parent Drawing instance */
2430
drawing?: Drawing;
2531
}
2632

@@ -39,11 +45,10 @@ export class Drawing {
3945
* @param {Object} config Shorthand - pass the created anchor coords that can normally be used to construct it.
4046
* @returns {Anchor}
4147
*/
42-
// TODO: couldn't get function override working, but hopefully in the future
43-
// createAnchor(type: 'absoluteAnchor', config: AnchorOption): AbsoluteAnchor;
44-
// createAnchor(type: 'oneCellAnchor', config: AnchorOption): OneCellAnchor;
45-
// createAnchor(type: 'twoCellAnchor', config: DualAnchorOption): TwoCellAnchor;
46-
createAnchor(type: 'absoluteAnchor' | 'oneCellAnchor' | 'twoCellAnchor', config: any): AbsoluteAnchor | OneCellAnchor | TwoCellAnchor {
48+
createAnchor(
49+
type: 'absoluteAnchor' | 'oneCellAnchor' | 'twoCellAnchor',
50+
config: Partial<AnchorOption | DualAnchorOption>,
51+
): AbsoluteAnchor | OneCellAnchor | TwoCellAnchor {
4752
config ??= {} as AnchorOption | DualAnchorOption;
4853
config.drawing = this;
4954
switch (type) {

packages/excel-builder-vanilla/src/Excel/Drawing/OneCellAnchor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class OneCellAnchor {
2222
constructor(config: AnchorOption) {
2323
if (config) {
2424
this.setPos(config.x, config.y, config.xOff, config.yOff);
25-
this.setDimensions(config.width, config.height);
25+
this.setDimensions(config.width || 0, config.height || 0);
2626
}
2727
}
2828

packages/excel-builder-vanilla/src/Excel/Pane.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
import type { XMLDOM } from './XMLDOM.js';
88

99
export class Pane {
10-
/*
11-
Possible Values:
12-
null
13-
split Split
14-
frozen Frozen
15-
frozenSplit Frozen Split
16-
http://www.datypic.com/sc/ooxml/t-ssml_ST_PaneState.html
17-
*/
1810
state: null | 'split' | 'frozen' | 'frozenSplit' = null;
1911
xSplit: number | null = null;
2012
ySplit: number | null = null;

0 commit comments

Comments
 (0)