Skip to content

Commit

Permalink
Add lookupCondition to getRowsOfMatchingRecordSet and findMatchingRow…
Browse files Browse the repository at this point in the history
…ById
  • Loading branch information
ngnathan committed Sep 17, 2024
1 parent ff3cc18 commit 1b69307
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-baboons-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@connectk12/exceljs": patch
---

Add lookupCondition to getRowsOfMatchingRecordSet and findMatchingRowById
23 changes: 19 additions & 4 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const updateCell = (
* @param lastRowNumber - The last row number to search for the matching row.
* @param id - The ID to search for in the worksheet.
* @param lookupCol - The column to search for the ID in the worksheet.
* @param lookupCondition - The condition to match the lookup value.
* @param opts - Options to customize the search process.
* @param opts.findSimilarMatchWithLastDigits - Whether to find a similar match based on the last digits of the ID.
*
Expand All @@ -112,13 +113,15 @@ export const findMatchingRowById = ({
lastRowNumber,
id,
lookupCol,
lookupCondition,
opts,
}: {
worksheet: ExcelJS.Worksheet;
startRowNumber: number;
lastRowNumber: number;
id: string;
lookupCol: string;
lookupCondition?: (currentRowValue: string) => boolean;
opts?: {
findSimilarMatchWithLastDigits?: boolean;
};
Expand All @@ -135,7 +138,14 @@ export const findMatchingRowById = ({
return _id === _lookupValue;
});

if (!row && opts?.findSimilarMatchWithLastDigits) {
if (!row && lookupCondition) {
return rows.find((row) => {
const currentRowId = sanitizeText(
row.getCell(lookupCol).value?.toString()
);
return currentRowId && lookupCondition(currentRowId);
});
} else if (!row && opts?.findSimilarMatchWithLastDigits) {
return rows.find((row) => {
const currentRowId = sanitizeText(
row.getCell(lookupCol).value?.toString()
Expand Down Expand Up @@ -321,6 +331,7 @@ export const findMatchingRowByName = ({
* @param lastRowNumber - The last row number to search for the last row in the record set.
* @param lookupCol - The column to search for the lookup value in the worksheet.
* @param lookupValue - The value to match the lookup value.
* @param lookupCondition - The condition to match the lookup value.
*
* @returns {ExcelJS.Row | undefined} - The last row in the record set if found, otherwise undefined.
*/
Expand Down Expand Up @@ -573,13 +584,15 @@ export const getRowsOfMatchingRecordSet = ({
lastRowNumber,
lookupCol,
lookupValue,
lookupCondition,
opts,
}: {
worksheet: ExcelJS.Worksheet;
startRowNumber: number;
lastRowNumber: number;
lookupCol: string;
lookupValue: string;
lookupCondition?: (currentRowValue: string) => boolean;
opts?: {
findSimilarMatchWithLastDigits?: boolean;
};
Expand All @@ -590,6 +603,7 @@ export const getRowsOfMatchingRecordSet = ({
startRowNumber,
lastRowNumber,
lookupCol,
lookupCondition,
id: lookupValue,
opts,
});
Expand All @@ -600,9 +614,10 @@ export const getRowsOfMatchingRecordSet = ({
lastRowNumber,
lookupCol,
lookupValue,
lookupCondition: opts?.findSimilarMatchWithLastDigits
? (currentRowValue) => currentRowValue?.endsWith(lookupValue)
: undefined,
lookupCondition:
lookupCondition ?? opts?.findSimilarMatchWithLastDigits
? (currentRowValue) => currentRowValue?.endsWith(lookupValue)
: undefined,
});
if (startRow && lastRow) {
rows = worksheet.getRows(
Expand Down

0 comments on commit 1b69307

Please sign in to comment.