Skip to content

Commit

Permalink
Resolve issues with data functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ngnathan committed Aug 20, 2024
1 parent 496a1a2 commit bc7926b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-otters-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@connectk12/exceljs": patch
---

Resolve issues with data functions
70 changes: 40 additions & 30 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ export const findMatchingRowById = ({
return undefined;
}

const row = rows.find(
(row) => id === sanitizeText(row.getCell(lookupCol).value?.toString())
);
const row = rows.find((row) => {
const lookupValue = sanitizeText(row.getCell(lookupCol).value?.toString());
return id === lookupValue;
});

if (!row && opts?.findSimilarMatchWithLastDigits) {
return rows.find((row) => {
Expand Down Expand Up @@ -337,9 +338,11 @@ export const findLastRowInRecordSet = ({
lookupValue: ExcelJS.CellValue;
lookupCondition?: (currentRowValue: string) => boolean;
}): ExcelJS.Row | undefined => {
const rows = worksheet.getRows(startRowNumber, lastRowNumber);
const rows = worksheet.getRows(
startRowNumber,
lastRowNumber - startRowNumber + 1
);
if (!rows) {
// throw new Error("No matching rows found");
console.log("No matching rows found");
return undefined;
}
Expand All @@ -348,14 +351,14 @@ export const findLastRowInRecordSet = ({
let currentRowValue = sanitizeText(
row.getCell(lookupCol).value?.toString()
);
if (!currentRowValue) return true;
if (lookupCondition?.(currentRowValue)) return true;
return currentRowValue === lookupValue;
if (lookupCondition) {
return currentRowValue && !lookupCondition(currentRowValue);
}
return currentRowValue !== lookupValue;
});

const lastRowInRecordSet = nextMatchingRow
? worksheet.getRow(nextMatchingRow.number - 1)
: worksheet.getRow(lastRowNumber);
: undefined;
return lastRowInRecordSet;
};

Expand Down Expand Up @@ -566,36 +569,43 @@ export const getRowsOfMatchingRecordSet = ({
lastRowNumber,
lookupCol,
lookupValue,
lookupCondition,
opts,
}: {
worksheet: ExcelJS.Worksheet;
startRowNumber: number;
lastRowNumber: number;
lookupCol: string;
lookupValue: string;
lookupCondition?: (lookupValue: string) => boolean;
opts?: {
findSimilarMatchWithLastDigits?: boolean;
};
}): ExcelJS.Row[] => {
let rowNumber = startRowNumber;
let currentRow = worksheet.getRow(rowNumber);
let currentlookupValue = currentRow.getCell(lookupCol).value?.toString();
const rows = [];
while (
rowNumber <= lastRowNumber &&
currentlookupValue !== lookupValue &&
(!opts?.findSimilarMatchWithLastDigits ||
(opts?.findSimilarMatchWithLastDigits &&
!currentlookupValue?.endsWith(lookupValue))) &&
(!currentlookupValue ||
(currentlookupValue && !lookupCondition?.(currentlookupValue)))
) {
rows.push(currentRow);
rowNumber++;
currentRow = worksheet.getRow(rowNumber);
currentlookupValue = currentRow.getCell(lookupCol).value?.toString();
}): ExcelJS.Row[] | undefined => {
let rows: ExcelJS.Row[] | undefined = undefined;
const startRow = findMatchingRowById({
worksheet,
startRowNumber,
lastRowNumber,
lookupCol,
id: lookupValue,
opts,
});
if (startRow) {
const lastRow = findLastRowInRecordSet({
worksheet,
startRowNumber: startRow.number,
lastRowNumber,
lookupCol,
lookupValue,
lookupCondition: opts?.findSimilarMatchWithLastDigits
? (currentRowValue) => currentRowValue?.endsWith(lookupValue)
: undefined,
});
if (startRow && lastRow) {
rows = worksheet.getRows(
startRow.number,
lastRow.number - startRow.number + 1
);
}
}
return rows;
};
Expand Down

0 comments on commit bc7926b

Please sign in to comment.