Repository navigation
fix(release): read Excel start dates, refuse impossible times and repeated columns, keep typed escapes, no suggestions for punctuation #9545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
3ff0517
fix(release): read Excel start dates, refuse impossible times and rep…
waleedlatif1 2d50d6a
fix(people): honor the 1904 date system and cover the sheet reader wi…
waleedlatif1 1f1dbcb
fix(people): pass an out-of-range date serial through instead of fail…
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
apps/sim/app/o/[organizationId]/people/read-people-sheet.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import * as XLSX from 'xlsx' | ||
| import { readPeopleSheet } from '@/app/o/[organizationId]/people/read-people-sheet' | ||
|
|
||
| /** January 31, 2024 as a date serial in each of Excel's two date systems. */ | ||
| const SERIAL_1900 = 45322 | ||
| const SERIAL_1904 = SERIAL_1900 - 1462 | ||
|
|
||
| function workbookFile(rows: unknown[][], { format = 'm/d/yy', date1904 = false } = {}): File { | ||
| const sheet = XLSX.utils.aoa_to_sheet(rows, { dense: false }) | ||
| for (const [address, cell] of Object.entries(sheet)) { | ||
| if (!address.startsWith('!') && (cell as XLSX.CellObject).t === 'n') { | ||
| ;(cell as XLSX.CellObject).z = format | ||
| } | ||
| } | ||
| const workbook = XLSX.utils.book_new() | ||
| XLSX.utils.book_append_sheet(workbook, sheet, 'People') | ||
| workbook.Workbook = { WBProps: { date1904 } } | ||
| const bytes = XLSX.write(workbook, { type: 'array', bookType: 'xlsx' }) | ||
| return new File([bytes], 'people.xlsx') | ||
| } | ||
|
|
||
| describe('readPeopleSheet', () => { | ||
| it.each(['m/d/yy', 'dd/mm/yyyy', 'yyyy-mm-dd'])( | ||
| 'reads a date cell formatted %s as its calendar date, not its displayed text', | ||
| async (format) => { | ||
| const sheet = await readPeopleSheet( | ||
| workbookFile( | ||
| [ | ||
| ['email', 'start date'], | ||
| ['ada@example.com', SERIAL_1900], | ||
| ], | ||
| { format } | ||
| ) | ||
| ) | ||
| expect(sheet.rows).toEqual([['ada@example.com', '2024-01-31']]) | ||
| } | ||
| ) | ||
|
|
||
| it('reads dates from a workbook saved in the 1904 date system', async () => { | ||
| const sheet = await readPeopleSheet( | ||
| workbookFile( | ||
| [ | ||
| ['email', 'start date'], | ||
| ['ada@example.com', SERIAL_1904], | ||
| ], | ||
| { date1904: true } | ||
| ) | ||
| ) | ||
| expect(sheet.rows).toEqual([['ada@example.com', '2024-01-31']]) | ||
| }) | ||
|
|
||
| it('passes an out-of-range date serial through as text for the server to refuse', async () => { | ||
| const sheet = await readPeopleSheet( | ||
| workbookFile([ | ||
| ['email', 'start date'], | ||
| ['ada@example.com', -5], | ||
| ]) | ||
| ) | ||
| expect(sheet.rows[0]?.[0]).toBe('ada@example.com') | ||
| expect(sheet.rows[0]?.[1]).not.toMatch(/^\d{4}-\d{2}-\d{2}$/) | ||
| }) | ||
|
|
||
| it('keeps blank rows in place so issue row numbers match the sheet', async () => { | ||
| const sheet = await readPeopleSheet( | ||
| workbookFile([ | ||
| ['email', 'title'], | ||
| ['ada@example.com', 'Engineer'], | ||
| [], | ||
| ['bo@example.com', 'Lead'], | ||
| ]) | ||
| ) | ||
| expect(sheet.rows).toEqual([ | ||
| ['ada@example.com', 'Engineer'], | ||
| ['', ''], | ||
| ['bo@example.com', 'Lead'], | ||
| ]) | ||
| }) | ||
|
|
||
| it('refuses a header row that repeats a column name', async () => { | ||
| await expect( | ||
| readPeopleSheet( | ||
| workbookFile([ | ||
| ['email', 'title', 'title'], | ||
| ['ada@example.com', 'a', 'b'], | ||
| ]) | ||
| ) | ||
| ).rejects.toThrow('The title column appears more than once') | ||
| }) | ||
| }) |
70 changes: 70 additions & 0 deletions
70
apps/sim/app/o/[organizationId]/people/read-people-sheet.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { PEOPLE_IMPORT_MAX_ROWS } from '@/lib/api/contracts/people' | ||
| import { assertOoxmlPreviewWithinLimits } from '@/lib/file-parsers/ooxml-preview-guard' | ||
|
|
||
| const MAX_FILE_BYTES = 5 * 1024 * 1024 | ||
|
|
||
| export interface PeopleSheet { | ||
| fileName: string | ||
| headers: string[] | ||
| /** Each row's cells, in `headers` order. */ | ||
| rows: string[][] | ||
| } | ||
|
|
||
| /** | ||
| * The first sheet's named columns and its rows of cells, one per sheet row (blank ones included) so the | ||
| * row numbers in the preview match the spreadsheet. Delimited text is read verbatim so the server | ||
| * interprets dates the same way for every file. A spreadsheet date cell is read from its stored | ||
| * value, not the text its locale displays, so `31/01/2024` and `1/31/24` both arrive as | ||
| * `2024-01-31`. | ||
| */ | ||
| export async function readPeopleSheet(file: File): Promise<PeopleSheet> { | ||
| if (file.size > MAX_FILE_BYTES) throw new Error('Choose a file under 5 MB') | ||
| const data = new Uint8Array(await file.arrayBuffer()) | ||
| if (/\.xlsx$/i.test(file.name)) await assertOoxmlPreviewWithinLimits(data) | ||
| const XLSX = await import('xlsx') | ||
| const workbook = XLSX.read(data, { | ||
| type: 'array', | ||
| raw: /\.(csv|tsv)$/i.test(file.name), | ||
| dense: true, | ||
| // Keeps each cell's number format, which is how a date cell is told from a number. | ||
| cellNF: true, | ||
| }) | ||
| const sheet = workbook.Sheets[workbook.SheetNames[0] ?? ''] | ||
| // Workbooks saved in the 1904 date system count their date serials from a different epoch. | ||
| const dateSystem = { date1904: Boolean(workbook.Workbook?.WBProps?.date1904) } | ||
| if (!sheet?.['!ref']) throw new Error('The file has no rows') | ||
| const range = XLSX.utils.decode_range(sheet['!ref']) | ||
| const grid = sheet['!data'] ?? [] | ||
| const text = (row: number, column: number): string => { | ||
| const cell = grid[row]?.[column] | ||
| if (!cell || cell.v === undefined || cell.v === null) return '' | ||
| if (cell.t === 'n' && XLSX.SSF.is_date(cell.z ?? '')) { | ||
| // A serial outside Excel's calendar has no date; its text goes on for the server to refuse. | ||
| const date = XLSX.SSF.parse_date_code(Number(cell.v), dateSystem) | ||
| if (date) | ||
| return `${date.y}-${String(date.m).padStart(2, '0')}-${String(date.d).padStart(2, '0')}` | ||
| } | ||
| return String(cell.w ?? cell.v).trim() | ||
| } | ||
| const columns: { header: string; index: number }[] = [] | ||
| for (let index = range.s.c; index <= range.e.c; index++) { | ||
| const header = text(range.s.r, index) | ||
| if (!header) continue | ||
| if (columns.some((column) => column.header === header)) { | ||
| throw new Error(`The ${header} column appears more than once`) | ||
| } | ||
| columns.push({ header, index }) | ||
| } | ||
| if (!columns.length) throw new Error('The first row should name the columns') | ||
| const rowCount = range.e.r - range.s.r | ||
| if (rowCount > PEOPLE_IMPORT_MAX_ROWS) { | ||
| throw new Error(`Import at most ${PEOPLE_IMPORT_MAX_ROWS} rows at a time`) | ||
| } | ||
| return { | ||
| fileName: file.name, | ||
| headers: columns.map(({ header }) => header), | ||
| rows: Array.from({ length: rowCount }, (_, offset) => | ||
| columns.map(({ index }) => text(range.s.r + 1 + offset, index)) | ||
| ), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.