7
7
{-# LANGUAGE RankNTypes #-}
8
8
{-# LANGUAGE ScopedTypeVariables #-}
9
9
{-# LANGUAGE StrictData #-}
10
- {-# LANGUAGE TemplateHaskell #-}
11
10
12
11
-- | Writes Excel files from a stream, which allows creation of
13
12
-- large Excel files while remaining in constant memory.
@@ -16,11 +15,6 @@ module Codec.Xlsx.Writer.Stream
16
15
writeXlsxWithSharedStrings ,
17
16
SheetWriteSettings (.. ),
18
17
defaultSettings ,
19
- wsSheetView ,
20
- wsZip ,
21
- wsColumnProperties ,
22
- wsRowProperties ,
23
- wsStyles ,
24
18
25
19
-- *** Shared strings
26
20
sharedStrings ,
@@ -55,6 +49,8 @@ import Codec.Xlsx.Writer.Internal
55
49
import Codec.Xlsx.Writer.Internal.Stream
56
50
import Conduit (PrimMonad , yield , (.|) )
57
51
import qualified Conduit as C
52
+ import Data.Generics.Labels
53
+ import GHC.Generics (Generic )
58
54
#ifdef USE_MICROLENS
59
55
import Data.Traversable.WithIndex
60
56
import Lens.Micro.Platform
@@ -86,7 +82,8 @@ import qualified Text.XML as TXML
86
82
import Text.XML.Stream.Render
87
83
import Text.XML.Unresolved (elementToEvents )
88
84
89
- upsertSharedStrings :: (MonadState SharedStringState m ) => Row -> m [(Text , Int )]
85
+ upsertSharedStrings ::
86
+ (MonadState SharedStringState m ) => Row -> m [(Text , Int )]
90
87
upsertSharedStrings row =
91
88
traverse upsertSharedString items
92
89
where
@@ -115,33 +112,32 @@ sharedStringsStream =
115
112
116
113
-- | Settings for writing a single sheet.
117
114
data SheetWriteSettings = MkSheetWriteSettings
118
- { _wsSheetView :: [SheetView ],
115
+ { wsSheetView :: [SheetView ],
119
116
-- | Enable zipOpt64=True if you intend writing large xlsx files, zip needs 64bit for files over 4gb.
120
- _wsZip :: ZipOptions ,
121
- _wsColumnProperties :: [ColumnsProperties ],
122
- _wsRowProperties :: Map Int RowProperties ,
123
- _wsStyles :: Styles
117
+ wsZip :: ZipOptions ,
118
+ wsColumnProperties :: [ColumnsProperties ],
119
+ wsRowProperties :: Map Int RowProperties ,
120
+ wsStyles :: Styles
124
121
}
122
+ deriving stock (Generic )
125
123
126
124
instance Show SheetWriteSettings where
127
125
-- ZipOptions lacks a show instance-}
128
126
show (MkSheetWriteSettings s _ y r _) =
129
127
printf
130
- " MkSheetWriteSettings{ _wsSheetView =%s, _wsColumnProperties =%s, _wsZip =defaultZipOptions, _wsRowProperties =%s }"
128
+ " MkSheetWriteSettings{ wsSheetView =%s, wsColumnProperties =%s, wsZip =defaultZipOptions, wsRowProperties =%s }"
131
129
(show s)
132
130
(show y)
133
131
(show r)
134
132
135
- makeLenses ''SheetWriteSettings
136
-
137
133
defaultSettings :: SheetWriteSettings
138
134
defaultSettings =
139
135
MkSheetWriteSettings
140
- { _wsSheetView = [] ,
141
- _wsColumnProperties = [] ,
142
- _wsRowProperties = mempty ,
143
- _wsStyles = emptyStyles,
144
- _wsZip =
136
+ { wsSheetView = [] ,
137
+ wsColumnProperties = [] ,
138
+ wsRowProperties = mempty ,
139
+ wsStyles = emptyStyles,
140
+ wsZip =
145
141
defaultZipOptions
146
142
{ zipOpt64 = False
147
143
-- There is a magick number in the zip archive package,
@@ -196,7 +192,7 @@ writeXlsxWithSharedStrings ::
196
192
ConduitT () Row m () ->
197
193
ConduitT () ByteString m Word64
198
194
writeXlsxWithSharedStrings settings sharedStrings' items =
199
- combinedFiles settings sharedStrings' items .| zipStream (settings ^. wsZip)
195
+ combinedFiles settings sharedStrings' items .| zipStream (settings ^. # wsZip)
200
196
201
197
-- massive amount of boilerplate needed for excel to function
202
198
boilerplate ::
@@ -213,7 +209,9 @@ boilerplate settings sharedStrings' =
213
209
ZipDataSource $ writeContentTypes .| eventsToBS
214
210
),
215
211
(zipEntry " xl/workbook.xml" , ZipDataSource $ writeWorkbook .| eventsToBS),
216
- (zipEntry " xl/styles.xml" , ZipDataByteString $ coerce $ settings ^. wsStyles),
212
+ ( zipEntry " xl/styles.xml" ,
213
+ ZipDataByteString $ coerce $ settings ^. # wsStyles
214
+ ),
217
215
( zipEntry " xl/_rels/workbook.xml.rels" ,
218
216
ZipDataSource $ writeWorkbookRels .| eventsToBS
219
217
),
@@ -231,7 +229,9 @@ combinedFiles settings sharedStrings' items =
231
229
boilerplate settings sharedStrings'
232
230
<> [ ( zipEntry " xl/worksheets/sheet1.xml" ,
233
231
ZipDataSource $
234
- items .| C. runReaderC settings (writeWorkSheet sharedStrings') .| eventsToBS
232
+ items
233
+ .| C. runReaderC settings (writeWorkSheet sharedStrings')
234
+ .| eventsToBS
235
235
)
236
236
]
237
237
@@ -359,12 +359,14 @@ writeEvents = renderBuilder (def {rsPretty = False})
359
359
sheetViews ::
360
360
forall m . (MonadReader SheetWriteSettings m ) => forall i . ConduitT i Event m ()
361
361
sheetViews = do
362
- sheetView <- view wsSheetView
362
+ sheetView <- view # wsSheetView
363
363
364
364
unless (null sheetView) $ el (n_ " sheetViews" ) $ do
365
365
let view' :: [Element ]
366
366
view' =
367
- setNameSpaceRec spreadSheetNS . toXMLElement . toElement (n_ " sheetView" )
367
+ setNameSpaceRec spreadSheetNS
368
+ . toXMLElement
369
+ . toElement (n_ " sheetView" )
368
370
<$> sheetView
369
371
370
372
C. yieldMany $ elementToEvents =<< view'
@@ -389,7 +391,7 @@ setNameSpaceRec space xelm =
389
391
390
392
columns :: (MonadReader SheetWriteSettings m ) => ConduitT Row Event m ()
391
393
columns = do
392
- colProps <- view wsColumnProperties
394
+ colProps <- view # wsColumnProperties
393
395
let cols :: Maybe TXML. Element
394
396
cols = nonEmptyElListSimple (n_ " cols" ) $ map (toElement (n_ " col" )) colProps
395
397
traverse_ (C. yieldMany . elementToEvents . toXMLElement) cols
@@ -409,7 +411,7 @@ mapRow ::
409
411
mapRow sharedStrings' sheetItem = do
410
412
mRowProp <-
411
413
preview $
412
- wsRowProperties
414
+ # wsRowProperties
413
415
. ix (unRowIndex rowIx)
414
416
. rowHeightLens
415
417
. _Just
@@ -446,7 +448,9 @@ renderCellType sharedStrings' cell =
446
448
maybe
447
449
mempty
448
450
(attr " t" . renderType sharedStrings')
449
- $ cell ^? cellValue . _Just
451
+ $ cell
452
+ ^? cellValue
453
+ . _Just
450
454
451
455
renderCell :: Map Text Int -> Cell -> Text
452
456
renderCell sharedStrings' cell = renderValue sharedStrings' val
0 commit comments