Skip to content

Commit 7cbf6ff

Browse files
author
Irene Alvarado
committed
adding xlsx functions. mainly to support type checking
1 parent ee01a68 commit 7cbf6ff

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

examples/xlsx/prices.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Name,Amount,Price
2+
One,500,$0.50
3+
Two,13,$10
4+
Thre,-3,"$3,000"

examples/xlsx/prices.xlsx

4.75 KB
Binary file not shown.

examples/xlsx/xlsx-example.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { xlsx, readXLSX } from '../../xlsx.ts'
2+
import { writeCSV } from '../../csv.ts'
3+
4+
const inputFilename = './examples/xlsx/prices.xlsx'
5+
const outputFilename = './examples/xlsx/prices.csv'
6+
7+
// read about what the xlsx library can do here: https://github.com/SheetJS/sheetjs
8+
9+
const workbook = await readXLSX(inputFilename)
10+
const sheetData = workbook.Sheets[workbook.SheetNames[0]]
11+
const csvString = await xlsx.utils.sheet_to_csv(sheetData) // can use to_json, to_txt, to_html, to_formulae
12+
13+
// Write to csv
14+
writeCSV(outputFilename, csvString)

mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './json.ts'
22
export * from './csv.ts'
3-
export * from './image.ts'
3+
export * from './image.ts'
4+
export * from './xlsx.ts'

xlsx.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import xlsxlib from 'https://jspm.dev/xlsx'
2+
import * as XLSX from 'https://cdn.deno.land/sheetjs/versions/v0.16.8/raw/types/index.d.ts';
3+
const xlsx = xlsxlib as typeof XLSX;
4+
5+
// read more about the library here: https://github.com/SheetJS/sheetjs
6+
7+
export { xlsx } // export the right types for this lib
8+
9+
// returns a Workbook
10+
export async function readXLSX(path: string) {
11+
const rawText = await Deno.readFile(path)
12+
const workbook = await xlsx.read(rawText, { type: 'buffer' })
13+
return workbook
14+
}

0 commit comments

Comments
 (0)