@@ -175,32 +175,9 @@ const App = () => {
175
175
176
176
const readWorkbook = async ( ) => {
177
177
resetProgress ( ) ;
178
-
179
- try {
180
- const workbook = await Excel . run ( async ( context ) => {
181
- var sheets = context . workbook . worksheets ;
182
-
183
- const worksheets = [ ] ;
184
- var worksheet = sheets . getLast ( ) ;
185
-
186
- do {
187
- resetProcessedRows ( ) ;
188
- incrementWorksheet ( ) ;
189
- worksheet . load ( "name" )
190
-
191
- var usedRange = worksheet . getUsedRange ( ) ;
192
- var lastColumn = usedRange . getLastColumn ( ) ;
193
- var lastRow = usedRange . getLastRow ( ) ;
194
- lastColumn . load ( "columnIndex" ) ;
195
- lastRow . load ( "RowIndex" ) ;
196
- await context . sync ( ) ;
197
-
198
- const range = worksheet . getRangeByIndexes ( 0 , 0 , lastRow . rowIndex , lastColumn . columnIndex ) ;
199
- usedRange . untrack ( ) ;
200
- lastColumn . untrack ( ) ;
201
- lastRow . untrack ( ) ;
202
-
203
- range . load ( [
178
+
179
+ const readRange = async ( worksheetData , range ) => {
180
+ range . load ( [
204
181
"columnCount" ,
205
182
"formulasR1C1" ,
206
183
"numberFormat" ,
@@ -223,16 +200,9 @@ const App = () => {
223
200
}
224
201
}
225
202
} ) ;
226
- await context . sync ( ) ;
227
-
228
- const worksheetData = {
229
- name : worksheet . name ,
230
- cells : { } ,
231
- } ;
203
+ await range . context . sync ( ) ;
232
204
233
- setWorksheetRows ( range . rowCount ) ;
234
205
for ( let row = 0 ; row < range . rowCount ; row ++ ) {
235
- incrementProcessedRows ( ) ;
236
206
for ( let col = 0 ; col < range . columnCount ; col ++ ) {
237
207
const cellData = {
238
208
formulaR1C1 : range . formulasR1C1 [ row ] [ col ] ,
@@ -257,12 +227,75 @@ const App = () => {
257
227
258
228
}
259
229
}
260
- worksheets . push ( worksheetData ) ;
261
- console . log ( worksheetData ) ;
262
230
263
- range . untrack ( ) ;
231
+ return worksheetData ;
232
+ }
233
+
234
+ try {
235
+ const workbook = await Excel . run ( async ( context ) => {
236
+ var sheets = context . workbook . worksheets ;
237
+
238
+ const worksheets = [ ] ;
239
+ var worksheet = sheets . getFirst ( ) ;
240
+
241
+ do {
242
+ await context . sync ( ) ;
243
+ if ( worksheet . isNullObject ) {
244
+ break ;
245
+ }
246
+ resetProcessedRows ( ) ;
247
+ incrementWorksheet ( ) ;
248
+ worksheet . load ( "name" )
249
+
250
+ var usedRange = worksheet . getUsedRange ( ) ;
251
+ var lastColumn = usedRange . getLastColumn ( ) ;
252
+ var lastRow = usedRange . getLastRow ( ) ;
253
+ lastColumn . load ( "columnIndex" ) ;
254
+ lastRow . load ( "RowIndex" ) ;
264
255
await context . sync ( ) ;
265
- } while ( worksheet = worksheet . getPrevious ( ) ) ;
256
+
257
+ var worksheetData = {
258
+ name : worksheet . name ,
259
+ cells : { } ,
260
+ } ;
261
+
262
+ var rowsCount = lastRow . rowIndex ;
263
+ var columnsCount = lastColumn . columnIndex ;
264
+ usedRange . untrack ( ) ;
265
+ lastColumn . untrack ( ) ;
266
+ lastRow . untrack ( ) ;
267
+
268
+ var currentRow = 0 ;
269
+ var currentColumn = 0 ;
270
+ setWorksheetRows ( rowsCount ) ;
271
+
272
+ const COLUMN_CHUNK_SIZE = 100 ;
273
+ const ROW_CHUNK_SIZE = 2 ;
274
+
275
+ while ( currentRow < rowsCount ) {
276
+ let lastRowIndex = Math . min ( currentRow + ROW_CHUNK_SIZE , rowsCount ) ;
277
+ currentColumn = 0 ;
278
+ while ( currentColumn < columnsCount ) {
279
+ let lastColumnIndex = Math . min ( currentColumn + COLUMN_CHUNK_SIZE , columnsCount ) ;
280
+ const range = worksheet . getRangeByIndexes ( currentRow , currentColumn , lastRowIndex - currentRow , lastColumnIndex - currentColumn ) ;
281
+ worksheetData = await readRange ( worksheetData , range ) ;
282
+ range . untrack ( ) ;
283
+ await context . sync ( ) ;
284
+
285
+ currentColumn = lastColumnIndex ;
286
+ }
287
+ currentRow = lastRowIndex ;
288
+ incrementProcessedRows ( ) ;
289
+ incrementProcessedRows ( ) ;
290
+ }
291
+
292
+ if ( lastRow . rowIndex == 0 )
293
+ {
294
+ continue ;
295
+ }
296
+
297
+ worksheets . push ( worksheetData ) ;
298
+ } while ( worksheet = worksheet . getNextOrNullObject ( ) ) ;
266
299
console . log ( worksheets ) ;
267
300
return worksheets ;
268
301
} ) ;
0 commit comments