Skip to content

Commit 080ef96

Browse files
AKritskiyAKritskiy
authored andcommitted
optimisation
1 parent d77af35 commit 080ef96

File tree

1 file changed

+31
-75
lines changed

1 file changed

+31
-75
lines changed

packages/main-library/src/index.ts

Lines changed: 31 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export interface IWorksheetColumnWidth {
3535
width: number
3636
}
3737

38+
export interface IColumnProps {
39+
format?: string | null
40+
type?: string | null
41+
width?: string | null
42+
isFormula?: boolean | null
43+
}
44+
3845
export type IWorkbookCallback = (workbook: WorkBook) => void
3946

4047
export { utils, WorkBook, WorkSheet }
@@ -70,92 +77,45 @@ export const getJsonSheetRow = (content: IContent, columns: IColumn[]): IJsonShe
7077
return jsonSheetRow
7178
}
7279

73-
const applyColumnFormat = (worksheet: WorkSheet, columnIds: string[], columnFormats: Array<string | null>) => {
80+
const applyColumnFormat = (worksheet: WorkSheet, columnIds: string[], columnFormats: IColumnProps[]) => {
7481
for (let i = 0; i < columnIds.length; i += 1) {
75-
const columnFormat = columnFormats[i]
82+
const columnFormat = columnFormats[i].format
83+
const columnType = columnFormats[i].type
84+
const columnWidth = columnFormats[i].width
85+
const columnFormula = columnFormats[i].isFormula
7686

77-
// Skip column if it doesn't have a format
78-
if (!columnFormat) {
87+
if (!columnFormat && !columnType && !columnWidth && !columnFormula) {
7988
continue
8089
}
8190

8291
const column = utils.decode_col(columnIds[i])
8392
const range = utils.decode_range(worksheet["!ref"] ?? "")
8493

8594
// Note: Range.s.r + 1 skips the header row
86-
for (let row = range.s.r + 1; row <= range.e.r; ++row) {
95+
for (let row = range.s.r; row <= range.e.r; ++row) {
8796
const ref = utils.encode_cell({ r: row, c: column })
8897

89-
if (worksheet[ref]) {
90-
worksheet[ref].z = columnFormat
98+
if (worksheet[ref] && columnWidth) {
99+
worksheet[ref].width = columnWidth
91100
}
92-
}
93-
}
94-
}
95101

96-
const applyColumnTypes = (worksheet: WorkSheet, columnIds: string[], columnTypes: Array<string | null>) => {
97-
for (let i = 0; i < columnIds.length; i += 1) {
98-
const columnType = columnTypes[i]
102+
if (row === 0) continue
99103

100-
if (!columnType) {
101-
continue
102-
}
103-
104-
const column = utils.decode_col(columnIds[i])
105-
const range = utils.decode_range(worksheet["!ref"] ?? "")
104+
console.log(columnFormula)
106105

107-
for (let row = range.s.r + 1; row <= range.e.r; ++row) {
108-
const ref = utils.encode_cell({ r: row, c: column })
109-
110-
if (worksheet[ref]) {
106+
if (worksheet[ref] && columnFormat) {
107+
worksheet[ref].z = columnFormat
108+
}
109+
if (worksheet[ref] && columnType) {
111110
worksheet[ref].t = columnType
112111
}
113-
}
114-
}
115-
}
116-
117-
const applyColumnFormuls = (worksheet: WorkSheet, columnIds: string[], columnFormuls: Array<boolean | null>) => {
118-
for (let i = 0; i < columnIds.length; i += 1) {
119-
const columnFormul = columnFormuls[i]
120-
121-
if (!columnFormul) {
122-
continue
123-
}
124-
125-
const column = utils.decode_col(columnIds[i])
126-
const range = utils.decode_range(worksheet["!ref"] ?? "")
127-
128-
for (let row = range.s.r + 1; row <= range.e.r; ++row) {
129-
const ref = utils.encode_cell({ r: row, c: column })
130-
131-
if (worksheet[ref] && worksheet[ref].v[0] === "=") {
112+
if (worksheet[ref] && worksheet[ref].v[0] === "=" && columnFormula) {
132113
worksheet[ref].f = worksheet[ref].v
133114
}
134115
}
135116
}
136117
}
137118

138-
const applyColumnWidths = (worksheet: WorkSheet, columnIds: string[], columnWidths: Array<string | null>) => {
139-
for (let i = 0; i < columnIds.length; i += 1) {
140-
const columnWidth = columnWidths[i]
141-
142-
if (!columnWidth) {
143-
continue
144-
}
145-
146-
const column = utils.decode_col(columnIds[i])
147-
const range = utils.decode_range(worksheet["!ref"] ?? "")
148-
149-
for (let row = range.s.r; row <= range.e.r; ++row) {
150-
const ref = utils.encode_cell({ r: row, c: column })
151-
152-
if (worksheet[ref]) {
153-
worksheet[ref].width = columnWidth
154-
}
155-
}
156-
}
157-
}
158-
159119
const getWorksheetColumnIds = (worksheet: WorkSheet): string[] => {
160120
const columnRange = utils.decode_range(worksheet["!ref"] ?? "")
161121

@@ -230,19 +190,15 @@ const getWorksheet = (jsonSheet: IJsonSheet, settings: ISettings): WorkSheet =>
230190
const worksheet = utils.json_to_sheet(jsonSheetRows)
231191
const worksheetColumnIds = getWorksheetColumnIds(worksheet)
232192

233-
const worksheetColumnFormats = jsonSheet.columns.map((jsonSheetColumn) => jsonSheetColumn.format ?? null)
234-
applyColumnFormat(worksheet, worksheetColumnIds, worksheetColumnFormats)
235-
236-
const worksheetColumnTypes = jsonSheet.columns.map((jsonSheetColumn) => jsonSheetColumn.type ?? null)
237-
applyColumnTypes(worksheet, worksheetColumnIds, worksheetColumnTypes)
238-
239-
const worksheetColumnFormuls = jsonSheet.columns.map((jsonSheetColumn) => jsonSheetColumn.isFormula ?? null)
240-
applyColumnFormuls(worksheet, worksheetColumnIds, worksheetColumnFormuls)
241-
242-
const worksheetColumnWidths = jsonSheet.columns.map((jsonSheetColumn) => {
243-
return jsonSheetColumn.width ?? null
193+
const worksheetColumn = jsonSheet.columns.map((jsonSheetColumn) => {
194+
return {
195+
format: jsonSheetColumn.format ?? null,
196+
type: jsonSheetColumn.type ?? null,
197+
isFormula: jsonSheetColumn.isFormula ?? null,
198+
width: jsonSheetColumn.width ?? null,
199+
}
244200
})
245-
applyColumnWidths(worksheet, worksheetColumnIds, worksheetColumnWidths)
201+
applyColumnFormat(worksheet, worksheetColumnIds, worksheetColumn)
246202

247203
worksheet["!cols"] = getWorksheetColumnWidths(worksheet, settings.extraLength)
248204

0 commit comments

Comments
 (0)