-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Microsoft Excel support (xlsx)
Closes: #1489 Signed-off-by: Alexander Alemayhu <[email protected]>
- Loading branch information
Showing
12 changed files
with
223 additions
and
114 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
Binary file not shown.
23 changes: 23 additions & 0 deletions
23
src/infrastracture/adapters/fileConversion/__tests__/convertXLSXToHTML.test.ts
This file contains 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,23 @@ | ||
import { readFileSync } from 'fs'; | ||
import Workspace from '../../../../lib/parser/WorkSpace'; | ||
import { convertXLSXToHTML } from '../convertXLSXToHTML'; | ||
import { join } from 'path'; | ||
|
||
describe('convertXLSXToHTML', () => { | ||
beforeAll(() => { | ||
process.env.WORKSPACE_BASE = '/tmp'; | ||
}); | ||
|
||
it('should convert XLSX to HTML and save the file', async () => { | ||
const workspace = new Workspace(true, 'fs'); | ||
const xlsxPath = join(__dirname, '../___mock/sim.xlsx'); | ||
const buffer = readFileSync(xlsxPath); | ||
const html = convertXLSXToHTML(buffer, join(workspace.location, 'Simple.html')); | ||
expect(html).toContain('<!DOCTYPE html>'); | ||
expect(html).toContain('Simple.html'); | ||
}); | ||
|
||
afterAll(() => { | ||
delete process.env.WORKSPACE_BASE; | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
src/infrastracture/adapters/fileConversion/convertXLSXToHTML.ts
This file contains 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,33 @@ | ||
import * as XLSX from 'xlsx'; | ||
|
||
type XLSXRow = [string | undefined, string | undefined, ...unknown[]]; | ||
|
||
export function convertXLSXToHTML(buffer: Buffer, title: string): string { | ||
const workbook = XLSX.read(buffer, { type: 'buffer' }); | ||
const sheetName = workbook.SheetNames[0]; | ||
const worksheet = workbook.Sheets[sheetName]; | ||
const jsonData = XLSX.utils.sheet_to_json(worksheet, { | ||
header: 1, | ||
}) as XLSXRow[]; | ||
|
||
return `<!DOCTYPE html> | ||
<html> | ||
<head><title>${title}</title></head> | ||
<body> | ||
${jsonData | ||
.map((row: XLSXRow) => { | ||
const front = row[0] || ''; | ||
const back = row[1] || ''; | ||
return `<ul class="toggle"> | ||
<li> | ||
<details> | ||
<summary>${front}</summary> | ||
<p>${back}</p> | ||
</details> | ||
</li> | ||
</ul>`; | ||
}) | ||
.join('\n')} | ||
</body> | ||
</html>`; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.