Skip to content

Commit 59a685f

Browse files
Or-MiOr-Mi
authored andcommitted
change the loading of usedRange - now load using getCellProperties
1 parent 1618a97 commit 59a685f

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

src/taskpane/components/App.tsx

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { getCellAddress } from "../taskpane";
2020
import { useState } from "react";
2121

22+
2223
// ProJets brand color
2324
const brandColor = "#4B0DFF";
2425
const accentColor = "#FF6B00"; // Orange accent color from the image
@@ -137,7 +138,7 @@ const App = () => {
137138
const styles = useStyles();
138139
const [selectedTab, setSelectedTab] = useState("read");
139140
const [fullPluginMode, setFullPluginMode] = useState("main"); // Tracks the mode within Full Plugin tab
140-
141+
141142
// Read section
142143
const inputId = useId("input");
143144
const inputId2 = useId("input");
@@ -178,79 +179,90 @@ const App = () => {
178179
try {
179180
const workbook = await Excel.run(async (context) => {
180181
var sheets = context.workbook.worksheets;
181-
sheets.load("items");
182-
await context.sync();
183182

184183
const worksheets = [];
184+
var worksheet = sheets.getLast();
185185

186-
for (var worksheet of sheets.items) {
186+
do {
187187
resetProcessedRows();
188188
incrementWorksheet();
189+
worksheet.load("name")
189190

190-
const usedRange = worksheet.getUsedRange();
191-
usedRange.load();
192-
const usedRow = usedRange.getLastRow();
193-
const usedCol = usedRange.getLastColumn();
194-
195-
usedRow.load("rowIndex");
196-
usedCol.load("columnIndex");
191+
var usedRange = worksheet.getUsedRange();
192+
var lastColumn = usedRange.getLastColumn();
193+
var lastRow = usedRange.getLastRow();
194+
lastColumn.load("columnIndex");
195+
lastRow.load("RowIndex");
197196
await context.sync();
198197

199-
const range = worksheet.getRangeByIndexes(0, 0, usedRow.rowIndex + 1, usedCol.columnIndex);
198+
const range = worksheet.getRangeByIndexes(0, 0, lastRow.rowIndex, lastColumn.columnIndex);
199+
usedRange.untrack();
200+
lastColumn.untrack();
201+
lastRow.untrack();
202+
200203
range.load([
201-
"values",
202-
"formulas",
204+
"columnCount",
203205
"formulasR1C1",
204-
"address",
205206
"numberFormat",
206-
"format/font",
207207
"rowCount",
208-
"columnCount",
209208
]);
210-
209+
var properties = range.getCellProperties({
210+
address: true,
211+
format: {
212+
fill: {
213+
color: true
214+
},
215+
font: {
216+
bold: true,
217+
color: true,
218+
italic: true,
219+
name: true,
220+
size: true,
221+
strikethrough: true,
222+
underline: true
223+
}
224+
}
225+
});
211226
await context.sync();
212227

213-
const addresses = range.address.split(":")[0].split("!")[1]; // Get starting address
214-
const baseColumn = addresses.replace(/[0-9]/g, "");
215-
const baseRow = parseInt(addresses.replace(/[^0-9]/g, ""));
216228
const worksheetData = {
217229
name: worksheet.name,
218230
cells: {},
219231
};
232+
220233
setWorksheetRows(range.rowCount);
221234
for (let row = 0; row < range.rowCount; row++) {
222235
incrementProcessedRows();
223236
for (let col = 0; col < range.columnCount; col++) {
224-
const cellAddress = getCellAddress(baseColumn, baseRow, row, col);
225-
const cellFont = range.getCell(row, col).format.font;
226-
const cellFill = range.getCell(row, col).format.fill;
227-
cellFont.load(["bold", "color", "italic", "name", "size", "underline", "backgroundColor"]);
228-
cellFill.load(["color"]);
229-
await context.sync();
230-
231237
const cellData = {
232238
formulaR1C1: range.formulasR1C1[row][col],
233-
address: cellAddress,
239+
address: properties.value[row][col].address,
234240
rowIndex: row,
235-
columnIndex: col,
241+
columnIndex: col,
236242
format: {
237243
font: {
238-
name: cellFont.name,
239-
size: cellFont.size,
240-
bold: cellFont.bold,
241-
italic: cellFont.italic,
242-
underline: cellFont.underline,
243-
color: cellFont.color,
244+
bold: properties.value[row][col].format.font.bold,
245+
color: properties.value[row][col].format.font.color,
246+
italic: properties.value[row][col].format.font.italic,
247+
name: properties.value[row][col].format.font.name,
248+
size: properties.value[row][col].format.font.size,
249+
strikethrough: properties.value[row][col].format.font.strikethrough,
250+
underline: properties.value[row][col].format.font.underline,
244251
},
245252
numberFormat: range.numberFormat[row][col],
246-
backgroundColor: cellFill.color,
253+
backgroundColor: properties.value[row][col].format.fill.color,
247254
},
248255
};
249-
worksheetData.cells[cellAddress] = cellData;
256+
worksheetData.cells[properties.value[row][col].address] = cellData;
257+
250258
}
251259
}
252260
worksheets.push(worksheetData);
253-
}
261+
console.log(worksheetData);
262+
263+
range.untrack();
264+
await context.sync();
265+
} while (worksheet = worksheet.getPrevious());
254266
console.log(worksheets);
255267
return worksheets;
256268
});
@@ -359,7 +371,7 @@ const App = () => {
359371
setIsWriteLoading(true);
360372
setWriteErrorMessage("");
361373
e.preventDefault();
362-
374+
363375
try {
364376
const formData = new FormData(e.currentTarget);
365377
const textArea = formData.get("textArea");

0 commit comments

Comments
 (0)