@@ -35,6 +35,13 @@ export interface IWorksheetColumnWidth {
35
35
width : number
36
36
}
37
37
38
+ export interface IColumnProps {
39
+ format ?: string | null
40
+ type ?: string | null
41
+ width ?: string | null
42
+ isFormula ?: boolean | null
43
+ }
44
+
38
45
export type IWorkbookCallback = ( workbook : WorkBook ) => void
39
46
40
47
export { utils , WorkBook , WorkSheet }
@@ -70,92 +77,45 @@ export const getJsonSheetRow = (content: IContent, columns: IColumn[]): IJsonShe
70
77
return jsonSheetRow
71
78
}
72
79
73
- const applyColumnFormat = ( worksheet : WorkSheet , columnIds : string [ ] , columnFormats : Array < string | null > ) => {
80
+ const applyColumnFormat = ( worksheet : WorkSheet , columnIds : string [ ] , columnFormats : IColumnProps [ ] ) => {
74
81
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
76
86
77
- // Skip column if it doesn't have a format
78
- if ( ! columnFormat ) {
87
+ if ( ! columnFormat && ! columnType && ! columnWidth && ! columnFormula ) {
79
88
continue
80
89
}
81
90
82
91
const column = utils . decode_col ( columnIds [ i ] )
83
92
const range = utils . decode_range ( worksheet [ "!ref" ] ?? "" )
84
93
85
94
// 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 ) {
87
96
const ref = utils . encode_cell ( { r : row , c : column } )
88
97
89
- if ( worksheet [ ref ] ) {
90
- worksheet [ ref ] . z = columnFormat
98
+ if ( worksheet [ ref ] && columnWidth ) {
99
+ worksheet [ ref ] . width = columnWidth
91
100
}
92
- }
93
- }
94
- }
95
101
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
99
103
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 )
106
105
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 ) {
111
110
worksheet [ ref ] . t = columnType
112
111
}
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 ) {
132
113
worksheet [ ref ] . f = worksheet [ ref ] . v
133
114
}
134
115
}
135
116
}
136
117
}
137
118
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
-
159
119
const getWorksheetColumnIds = ( worksheet : WorkSheet ) : string [ ] => {
160
120
const columnRange = utils . decode_range ( worksheet [ "!ref" ] ?? "" )
161
121
@@ -230,19 +190,15 @@ const getWorksheet = (jsonSheet: IJsonSheet, settings: ISettings): WorkSheet =>
230
190
const worksheet = utils . json_to_sheet ( jsonSheetRows )
231
191
const worksheetColumnIds = getWorksheetColumnIds ( worksheet )
232
192
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
+ }
244
200
} )
245
- applyColumnWidths ( worksheet , worksheetColumnIds , worksheetColumnWidths )
201
+ applyColumnFormat ( worksheet , worksheetColumnIds , worksheetColumn )
246
202
247
203
worksheet [ "!cols" ] = getWorksheetColumnWidths ( worksheet , settings . extraLength )
248
204
0 commit comments