Types
XlsxError = object of Exception
- Source Edit
NotExistsXlsxFileError = object of XlsxError
- Source Edit
InvalidXlsxFileError = object of XlsxError
- Source Edit
NotFoundSheetError = object of XlsxError
- Source Edit
UnKnownSheetDataKindError = object of XlsxError
- Source Edit
SheetDataKind {...}{.pure.} = enum Initial, Boolean, Date, Time, Error, InlineStr, Num, SharedString, Formula
- Source Edit
SheetArray = object shape*: tuple[rows: int, cols: int] data*: seq[string] header*: bool colType*: seq[SheetDataKind]
- SheetArray Source Edit
SheetTensor[T] = object shape*: tuple[rows: int, cols: int] data*: seq[T]
- Source Edit
SheetTable = object data*: Table[string, SheetArray]
- Source Edit
Procs
proc extractXml(src: string; dest: string = TempDir) {...}{.inline, raises: [ NotExistsXlsxFileError, OSError, InvalidXlsxFileError, IOError, Defect, Exception], tags: [ReadDirEffect, WriteDirEffect, ReadIOEffect, WriteIOEffect].}
- extract xml file from excel using zip, default path is TempDir. Source Edit
proc parseAllSheetName(fileName: string): seq[string] {...}{.inline, raises: [ NotExistsXlsxFileError, OSError, InvalidXlsxFileError, IOError, Defect, Exception, KeyError, ValueError], tags: [ReadDirEffect, WriteDirEffect, ReadIOEffect, WriteIOEffect].}
- get all sheet name Source Edit
proc parseExcel(fileName: string; sheetName = ""; header = false; skipHeaders = false; escapeStrings = false; trailingRows = false): SheetTable {...}{.raises: [ NotExistsXlsxFileError, OSError, InvalidXlsxFileError, IOError, Defect, Exception, KeyError, ValueError, UnKnownSheetDataKindError, NotFoundSheetError], tags: [ReadDirEffect, WriteDirEffect, ReadIOEffect, WriteIOEffect].}
-
Parse excel and return SheetTable which contains all sheetArray.
trailingRows is used to skip empty lines after last row with elements. But If there are some empty lines before last row with elements, these lines will be kept.
If you want to skip all empty lines, you should use iterator lines and set skipEmptyLines = true.
Examples:
let data = parseExcel("tests/test.xlsx") sheetName = "Sheet2" echo data[sheetName]
Source Edit proc `[]`(s: SheetArray; i, j: Natural): string {...}{.inline, raises: [IndexError], tags: [].}
- Source Edit
proc `[]=`(s: var SheetArray; i, j: Natural; value: string) {...}{.inline, raises: [IndexError], tags: [].}
- Source Edit
proc `$`(s: SheetArray): string {...}{.raises: [], tags: [].}
- display SheetArray Source Edit
proc show(s: SheetArray; rmax = 20; cmax = 5; width = 10) {...}{.raises: [], tags: [].}
-
display SheetArray with more control
Examples:
let sheetName = "sheet2" excel = "tests/nim.xlsx" data = parseExcel(excel, sheetName = sheetName, header = true, skipHeaders = false) data[sheetName].show(width = 20)
Source Edit proc toCsv(s: SheetArray; dest: string; sep = ",") {...}{.inline, raises: [IOError], tags: [WriteIOEffect].}
-
Parse SheetArray and write a Csv file
Examples:
let sheetName = "Sheet2" let data = parseExcel("tests/test.xlsx") data[sheetName].toCsv("tests/test.csv", sep = ",")
Source Edit proc toSeq(s: SheetArray; skipHeaders = false): seq[seq[string]] {...}{.inline, raises: [], tags: [].}
-
Parse SheetArray and return a seq[seq[string]]
Examples:
let sheetName = "Sheet2" let data = parseExcel("tests/test.xlsx") let rows = data[sheetName].toSeq(false) for row in rows: echo row
Source Edit proc readExcel[T: SomeNumber | bool | string](fileName: string; sheetName: string; skipHeaders = false; escapeStrings = false): SheetTensor[ T]
-
read excel for scientific calculation
Examples:
let sheetName = "Sheet1" let data = readExcel[int]("tests/test_read_excel.xlsx", sheetName, skipHeaders = false) assert(data.data == @[1, 4, 7, 9, 4, 7, 0, 3, 12, 54, 24, 887])
Source Edit
Iterators
iterator lines(fileName: string; sheetName: string; escapeStrings = false; skipEmptyLines = false): string {...}{.raises: [NotExistsXlsxFileError, OSError, InvalidXlsxFileError, IOError, Defect, Exception, KeyError, ValueError, NotFoundSheetError, UnKnownSheetDataKindError], tags: [ReadDirEffect, WriteDirEffect, ReadIOEffect, WriteIOEffect].}
-
return lines of xlsx
Examples:
for i in lines("tests/test.xlsx", "Sheet2"): echo i
Source Edit
Templates
template `[]`(s: SheetTable; key: string): SheetArray
- Source Edit