Skip to content

Commit 9c77c24

Browse files
Or-MiOr-Mi
authored andcommitted
changed readWorksheet to read in little chunks
1 parent d93551b commit 9c77c24

File tree

1 file changed

+71
-38
lines changed

1 file changed

+71
-38
lines changed

src/taskpane/components/App.tsx

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -175,32 +175,9 @@ const App = () => {
175175

176176
const readWorkbook = async () => {
177177
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([
204181
"columnCount",
205182
"formulasR1C1",
206183
"numberFormat",
@@ -223,16 +200,9 @@ const App = () => {
223200
}
224201
}
225202
});
226-
await context.sync();
227-
228-
const worksheetData = {
229-
name: worksheet.name,
230-
cells: {},
231-
};
203+
await range.context.sync();
232204

233-
setWorksheetRows(range.rowCount);
234205
for (let row = 0; row < range.rowCount; row++) {
235-
incrementProcessedRows();
236206
for (let col = 0; col < range.columnCount; col++) {
237207
const cellData = {
238208
formulaR1C1: range.formulasR1C1[row][col],
@@ -257,12 +227,75 @@ const App = () => {
257227

258228
}
259229
}
260-
worksheets.push(worksheetData);
261-
console.log(worksheetData);
262230

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");
264255
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());
266299
console.log(worksheets);
267300
return worksheets;
268301
});

0 commit comments

Comments
 (0)