(sheets) Add option to select single row when matching multiple

Closes #501
This commit is contained in:
Baptiste Arnaud
2023-05-12 11:21:24 -04:00
parent 45224f9fb3
commit 55dbb1abc7
3 changed files with 90 additions and 46 deletions

View File

@@ -38,10 +38,13 @@ export const getRow = async (
await doc.loadInfo()
const sheet = doc.sheetsById[sheetId]
const rows = await sheet.getRows()
const filteredRows = rows.filter((row) =>
referenceCell
? row[referenceCell.column as string] === referenceCell.value
: matchFilter(row, filter as NonNullable<typeof filter>)
const filteredRows = getTotalRows(
options.totalRowsToExtract,
rows.filter((row) =>
referenceCell
? row[referenceCell.column as string] === referenceCell.value
: matchFilter(row, filter as NonNullable<typeof filter>)
)
)
if (filteredRows.length === 0) {
log = {
@@ -99,3 +102,20 @@ export const getRow = async (
}
return { outgoingEdgeId, logs: log ? [log] : undefined }
}
const getTotalRows = <T>(
totalRowsToExtract: GoogleSheetsGetOptions['totalRowsToExtract'],
rows: T[]
): T[] => {
switch (totalRowsToExtract) {
case 'All':
case undefined:
return rows
case 'First':
return rows.slice(0, 1)
case 'Last':
return rows.slice(-1)
case 'Random':
return [rows[Math.floor(Math.random() * rows.length)]]
}
}