xlsx-1.1.2.2: Simple and incomplete Excel file parser/writer

Copyright | (c) Adam 2021 (c) Supercede 2021
License | MIT
Stability | experimental
Portability | POSIX
Safe Haskell | Safe-Inferred
Language | Haskell2010

Codec.Xlsx.Parser.Stream

Contents

- Index
- SheetItem
- Row
- Errors

Description

Parse `.xlsx` sheets in constant memory.

All actions on an xlsx file run inside the `XlsxM` monad, and must be run with `runXlsxM`. XlsxM is not a monad transformer, a design inherited from the "zip" package's ZipArchive monad.

Inside the XlsxM monad, you can stream `SheetItem`s (a row) from a particular sheet, using `readSheetByIndex`, which is callback-based and tied to IO.

Synopsis

- data XlsxM a
- runXlsxM :: MonadIO m => FilePath -> XlsxM a -> m a
- data WorkbookInfo = WorkbookInfo {
  - _wiSheets :: [SheetInfo]
  }
- data SheetInfo = SheetInfo {
  - sheetInfoName :: Text
  - sheetInfoRelId :: RefId
  - sheetInfoSheetId :: Int
  }
- wiSheets :: Iso' WorkbookInfo [SheetInfo]
- getWorkbookInfo :: XlsxM WorkbookInfo
- type CellRow = IntMap Cell
- readSheet :: SheetIndex -> (SheetItem -> IO ()) -> XlsxM Bool
- countRowsInSheet :: SheetIndex -> XlsxM (Maybe Int)
- collectItems :: SheetIndex -> XlsxM [SheetItem]
- data SheetIndex
- makeIndex :: Int -> SheetIndex
- makeIndexFromName :: Text -> XlsxM (Maybe SheetIndex)
- data SheetItem = MkSheetItem {
  - _si_sheet_index :: Int
  - _si_row :: ~Row
  }
- si_sheet_index :: Lens' SheetItem Int
- si_row :: Lens' SheetItem Row
- data Row = MkRow {
  - _ri_row_index :: RowIndex
  - _ri_cell_row :: ~CellRow
  }
- ri_row_index :: Lens' Row RowIndex
- ri_cell_row :: Lens' Row CellRow
- data SheetErrors
  - = ParseCoordinateError CoordinateErrors
  - | ParseTypeError TypeError
  - | ParseCellError AddCellErrors
  - | ParseStyleErrors StyleError
  - | HexpatParseError XMLParseError
- data AddCellErrors
  - = ReadError Text String
  - | SharedStringsNotFound Int (Vector Text)
- data CoordinateErrors
  - = CoordinateNotFound SheetValues
  - | NoListElement SheetValue SheetValues
  - | NoTextContent Content SheetValues
  - | DecodeFailure Text SheetValues
- data TypeError
  - = TypeNotFound SheetValues
  - | TypeNoListElement SheetValue SheetValues
  - | UnkownType Text SheetValues
  - | TypeNoTextContent Content SheetValues
- data WorkbookError
  - = LookupError {
    - lookup_attrs :: [(ByteString, Text)]
    - lookup_field :: ByteString
    }
  - | ParseDecimalError Text String

# Documentation

data XlsxM a Source #

#### Instances

Instances details

MonadIO XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods liftIO :: IO a -> XlsxM a Source #
Applicative XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods pure :: a -> XlsxM a Source # (<*>) :: XlsxM (a -> b) -> XlsxM a -> XlsxM b Source # liftA2 :: (a -> b -> c) -> XlsxM a -> XlsxM b -> XlsxM c Source # (*>) :: XlsxM a -> XlsxM b -> XlsxM b Source # (<*) :: XlsxM a -> XlsxM b -> XlsxM a Source #
Functor XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods fmap :: (a -> b) -> XlsxM a -> XlsxM b Source # (<$) :: a -> XlsxM b -> XlsxM a Source #
Monad XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods (>>=) :: XlsxM a -> (a -> XlsxM b) -> XlsxM b Source # (>>) :: XlsxM a -> XlsxM b -> XlsxM b Source # return :: a -> XlsxM a Source #
MonadCatch XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods catch :: (HasCallStack, Exception e) => XlsxM a -> (e -> XlsxM a) -> XlsxM a Source #
MonadMask XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods mask :: HasCallStack => ((forall a. XlsxM a -> XlsxM a) -> XlsxM b) -> XlsxM b Source # uninterruptibleMask :: HasCallStack => ((forall a. XlsxM a -> XlsxM a) -> XlsxM b) -> XlsxM b Source # generalBracket :: HasCallStack => XlsxM a -> (a -> ExitCase b -> XlsxM c) -> (a -> XlsxM b) -> XlsxM (b, c) Source #
MonadThrow XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods throwM :: (HasCallStack, Exception e) => e -> XlsxM a Source #
MonadBaseControl IO XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Associated Types type StM XlsxM a Source # Methods liftBaseWith :: (RunInBase XlsxM IO -> IO a) -> XlsxM a Source # restoreM :: StM XlsxM a -> XlsxM a Source #
MonadBase IO XlsxM Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods liftBase :: IO α -> XlsxM α Source #
type StM XlsxM a Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream type StM XlsxM a

runXlsxM :: MonadIO m => FilePath -> XlsxM a -> m a Source #

Run a series of actions on an Xlsx file

data WorkbookInfo Source #

Information about the workbook contained in xl/workbook.xml (currently a subset)

Constructors

WorkbookInfo |

Fields

- _wiSheets :: [SheetInfo]

#### Instances

Instances details

Show WorkbookInfo Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> WorkbookInfo -> ShowS Source # show :: WorkbookInfo -> String Source # showList :: [WorkbookInfo] -> ShowS Source #

data SheetInfo Source #

Represents sheets from the workbook.xml file. E.g. <sheet name=Data sheetId="1" state="hidden" r:id="rId2" /

Constructors

SheetInfo |

Fields

- sheetInfoName :: Text
- sheetInfoRelId :: RefId

  The r:id attribute value.

- sheetInfoSheetId :: Int

  The sheetId attribute value

#### Instances

Instances details

Show SheetInfo Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> SheetInfo -> ShowS Source # show :: SheetInfo -> String Source # showList :: [SheetInfo] -> ShowS Source #
Eq SheetInfo Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods (==) :: SheetInfo -> SheetInfo -> Bool Source # (/=) :: SheetInfo -> SheetInfo -> Bool Source #

wiSheets :: Iso' WorkbookInfo [SheetInfo] Source #

getWorkbookInfo :: XlsxM WorkbookInfo Source #

Returns information about the workbook, found in xl/workbook.xml. The result is cached so the XML will only be decompressed and parsed once inside a larger XlsxM action.

type CellRow = IntMap Cell Source #

readSheet Source #

Arguments

:: SheetIndex |
-> (SheetItem -> IO ()) | Function to consume the sheet's rows
-> XlsxM Bool | Returns False if sheet doesn't exist, or True otherwise

countRowsInSheet :: SheetIndex -> XlsxM (Maybe Int) Source #

Returns number of rows in the given sheet (identified by the sheet's ID, AKA the sheetId attribute, AKA `sheetInfoSheetId`), or Nothing if the sheet does not exist. Does not perform a full parse of the XML into `SheetItem`s, so it should be more efficient than counting via `readSheetByIndex`.

collectItems :: SheetIndex -> XlsxM [SheetItem] Source #

this will collect the sheetitems in a list. useful for cases were memory is of no concern but a sheetitem type in a list is needed.

## Index

data SheetIndex Source #

datatype representing a sheet index, looking it up by name can be done with `makeIndexFromName`, which is the preferred approach. although `makeIndex` is available in case it's already known.

#### Instances

Instances details

NFData SheetIndex Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods rnf :: SheetIndex -> () Source #

makeIndex :: Int -> SheetIndex Source #

This does *no* checking if the index exists or not. you could have index out of bounds issues because of this.

makeIndexFromName :: Text -> XlsxM (Maybe SheetIndex) Source #

Look up the index of a case insensitive sheet name

## SheetItem

data SheetItem Source #

Sheet item

The current sheet at a time, every sheet is constructed of these items.

Constructors

MkSheetItem |

Fields

- _si_sheet_index :: Int

  The sheet number

- _si_row :: ~Row

#### Instances

Instances details

Generic SheetItem Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Associated Types type Rep SheetItem :: Type -> Type Source # Methods from :: SheetItem -> Rep SheetItem x Source # to :: Rep SheetItem x -> SheetItem Source #
Show SheetItem Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> SheetItem -> ShowS Source # show :: SheetItem -> String Source # showList :: [SheetItem] -> ShowS Source #
NFData SheetItem Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods rnf :: SheetItem -> () Source #
type Rep SheetItem Source # |

Instance details

Defined in Codec.Xlsx.Parser.Stream

type Rep SheetItem = D1 ('MetaData "SheetItem" "Codec.Xlsx.Parser.Stream" "xlsx-1.1.2.2-DopzEu8STPN4igxHgE9OUe" 'False) (C1 ('MetaCons "MkSheetItem" 'PrefixI 'True) (S1 ('MetaSel ('Just "_si_sheet_index") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "_si_row") 'NoSourceUnpackedness 'SourceLazy 'DecidedLazy) (Rec0 Row)))

si_sheet_index :: Lens' SheetItem Int Source #

si_row :: Lens' SheetItem Row Source #

## Row

data Row Source #

Constructors

MkRow |

Fields

- _ri_row_index :: RowIndex

  Row number

- _ri_cell_row :: ~CellRow

  Row itself

#### Instances

Instances details

Generic Row Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Associated Types type Rep Row :: Type -> Type Source # Methods from :: Row -> Rep Row x Source # to :: Rep Row x -> Row Source #
Show Row Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> Row -> ShowS Source # show :: Row -> String Source # showList :: [Row] -> ShowS Source #
NFData Row Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods rnf :: Row -> () Source #
type Rep Row Source # |

Instance details

Defined in Codec.Xlsx.Parser.Stream

type Rep Row = D1 ('MetaData "Row" "Codec.Xlsx.Parser.Stream" "xlsx-1.1.2.2-DopzEu8STPN4igxHgE9OUe" 'False) (C1 ('MetaCons "MkRow" 'PrefixI 'True) (S1 ('MetaSel ('Just "_ri_row_index") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 RowIndex) :*: S1 ('MetaSel ('Just "_ri_cell_row") 'NoSourceUnpackedness 'SourceLazy 'DecidedLazy) (Rec0 CellRow)))

ri_row_index :: Lens' Row RowIndex Source #

ri_cell_row :: Lens' Row CellRow Source #

# Errors

data SheetErrors Source #

Constructors

ParseCoordinateError CoordinateErrors | Error while parsing coordinates
ParseTypeError TypeError | Error while parsing types
ParseCellError AddCellErrors | Error while parsing cells
ParseStyleErrors StyleError |
HexpatParseError XMLParseError |

#### Instances

Instances details

Exception SheetErrors Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods toException :: SheetErrors -> SomeException Source # fromException :: SomeException -> Maybe SheetErrors Source # displayException :: SheetErrors -> String Source #
Show SheetErrors Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> SheetErrors -> ShowS Source # show :: SheetErrors -> String Source # showList :: [SheetErrors] -> ShowS Source #

data AddCellErrors Source #

Constructors

ReadError | Could not read current cell value

Fields

- Text

  Original value

- String

  Error message

SharedStringsNotFound | Could not find string by index in shared string table

Fields

- Int

  Given index

- (Vector Text)

  Given shared strings to lookup in

#### Instances

Instances details

Show AddCellErrors Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> AddCellErrors -> ShowS Source # show :: AddCellErrors -> String Source # showList :: [AddCellErrors] -> ShowS Source #

data CoordinateErrors Source #

Constructors

CoordinateNotFound SheetValues | If the coordinate was not specified in "r" attribute
NoListElement SheetValue SheetValues | If the value is empty for some reason
NoTextContent Content SheetValues | If the value has something besides `ContentText` inside
DecodeFailure Text SheetValues | If malformed coordinate text was passed

#### Instances

Instances details

Exception CoordinateErrors Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods toException :: CoordinateErrors -> SomeException Source # fromException :: SomeException -> Maybe CoordinateErrors Source # displayException :: CoordinateErrors -> String Source #
Show CoordinateErrors Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> CoordinateErrors -> ShowS Source # show :: CoordinateErrors -> String Source # showList :: [CoordinateErrors] -> ShowS Source #

data TypeError Source #

Constructors

TypeNotFound SheetValues |
TypeNoListElement SheetValue SheetValues |
UnkownType Text SheetValues |
TypeNoTextContent Content SheetValues |

#### Instances

Instances details

Exception TypeError Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods toException :: TypeError -> SomeException Source # fromException :: SomeException -> Maybe TypeError Source # displayException :: TypeError -> String Source #
Show TypeError Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> TypeError -> ShowS Source # show :: TypeError -> String Source # showList :: [TypeError] -> ShowS Source #

data WorkbookError Source #

Constructors

LookupError |

Fields

- lookup_attrs :: [(ByteString, Text)]
- lookup_field :: ByteString

ParseDecimalError Text String |

#### Instances

Instances details

Exception WorkbookError Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods toException :: WorkbookError -> SomeException Source # fromException :: SomeException -> Maybe WorkbookError Source # displayException :: WorkbookError -> String Source #
Show WorkbookError Source # |
Instance details Defined in Codec.Xlsx.Parser.Stream Methods showsPrec :: Int -> WorkbookError -> ShowS Source # show :: WorkbookError -> String Source # showList :: [WorkbookError] -> ShowS Source #