|
1 | | -{-# OPTIONS_GHC -Wno-deprecations #-} |
2 | | -{-# OPTIONS_GHC -Wno-x-partial #-} |
3 | | - |
4 | 1 | module App.Xlsx |
5 | 2 | ( newXlsx, |
6 | 3 | xlsxFile, |
7 | 4 | xlsxMime, |
8 | 5 | ) |
9 | 6 | where |
10 | 7 |
|
| 8 | +import App.Types |
11 | 9 | import Codec.Xlsx |
12 | 10 | import qualified Data.ByteString.Lazy as BL |
13 | 11 | import qualified Data.Map as Map |
14 | | -import Functora.Miso.Prelude |
| 12 | +import Functora.Miso.Prelude hiding ((^.), _Just) |
15 | 13 | import Lens.Micro hiding (each, to) |
16 | | -import qualified Prelude |
17 | 14 |
|
18 | | -newXlsx :: Map Unicode Rfc2397 -> BL.ByteString |
19 | | -newXlsx imgs = xlsx |
| 15 | +newXlsx :: St Unique -> Map Unicode Rfc2397 -> BL.ByteString |
| 16 | +newXlsx st imgs = xlsx |
20 | 17 | where |
21 | 18 | xlsx = |
22 | 19 | fromXlsx 0 |
23 | 20 | $ def |
24 | | - & atSheet "List1" ?~ sheet |
| 21 | + & atSheet "Delivery Calculator" ?~ sheet |
25 | 22 | sheet = |
26 | 23 | def |
27 | | - & cellValueAt (1, 2) ?~ CellDouble 42.0 |
28 | | - & cellValueAt (3, 2) ?~ CellText "foo" |
29 | | - & #wsDrawing ?~ drawing (Prelude.head $ Map.elems imgs) |
| 24 | + & #wsDrawing ?~ Drawing mempty |
| 25 | + & addHeader st |
| 26 | + & flip |
| 27 | + (foldl $ addRow imgs) |
| 28 | + ( zip [2 ..] |
| 29 | + $ fmap |
| 30 | + (^.. #assetFieldPairs . each . #fieldPairValue) |
| 31 | + (st ^. #stAssets) |
| 32 | + ) |
| 33 | + |
| 34 | +addHeader :: St Unique -> Worksheet -> Worksheet |
| 35 | +addHeader st sheet = |
| 36 | + case sortOn length headers of |
| 37 | + [] -> sheet |
| 38 | + (rowVal : _) -> |
| 39 | + foldl |
| 40 | + ( \acc (colIdx, colVal) -> |
| 41 | + acc |
| 42 | + & cellValueAt (1, colIdx) ?~ CellText colVal |
| 43 | + ) |
| 44 | + sheet |
| 45 | + $ zip [1 ..] rowVal |
| 46 | + where |
| 47 | + headers :: [[Unicode]] |
| 48 | + headers = |
| 49 | + fmap |
| 50 | + ( ^.. |
| 51 | + #assetFieldPairs |
| 52 | + . each |
| 53 | + . #fieldPairKey |
| 54 | + . #fieldOutput |
| 55 | + ) |
| 56 | + $ stAssets st |
| 57 | + |
| 58 | +addRow :: |
| 59 | + Map Unicode Rfc2397 -> |
| 60 | + Worksheet -> |
| 61 | + (RowIndex, [Field DynamicField Unique]) -> |
| 62 | + Worksheet |
| 63 | +addRow imgs sheet (rowIdx, rowVal) = |
| 64 | + foldl |
| 65 | + ( \acc (colIdx, colVal) -> |
| 66 | + addCol imgs acc rowIdx colIdx colVal |
| 67 | + ) |
| 68 | + sheet |
| 69 | + $ zip [1 ..] rowVal |
| 70 | + |
| 71 | +addCol :: |
| 72 | + Map Unicode Rfc2397 -> |
| 73 | + Worksheet -> |
| 74 | + RowIndex -> |
| 75 | + ColumnIndex -> |
| 76 | + Field DynamicField Unique -> |
| 77 | + Worksheet |
| 78 | +addCol imgs sheet rowIdx colIdx field = |
| 79 | + if fieldType field /= FieldTypeImage |
| 80 | + then |
| 81 | + sheet |
| 82 | + & cellValueAt (rowIdx, colIdx) |
| 83 | + ?~ CellText txt |
| 84 | + else case Map.lookup txt imgs of |
| 85 | + -- |
| 86 | + -- TODO : handle img link |
| 87 | + -- |
| 88 | + Nothing -> |
| 89 | + sheet |
| 90 | + & cellValueAt (rowIdx, colIdx) |
| 91 | + ?~ CellText txt |
| 92 | + Just img -> |
| 93 | + sheet |
| 94 | + & #wsDrawing . _Just %~ \case |
| 95 | + Drawing xs -> |
| 96 | + Drawing $ newImg rowIdx colIdx (length xs) img : xs |
| 97 | + where |
| 98 | + txt = field ^. #fieldInput . #uniqueValue |
30 | 99 |
|
31 | | -drawing :: Rfc2397 -> Drawing |
32 | | -drawing rfc2397 = Drawing [anchor1] |
| 100 | +newImg :: RowIndex -> ColumnIndex -> Int -> Rfc2397 -> Anchor FileInfo a |
| 101 | +newImg (RowIndex rowIdx) (ColumnIndex colIdx) imgIdx rfc2397 = |
| 102 | + Anchor |
| 103 | + { anchAnchoring = |
| 104 | + TwoCellAnchor |
| 105 | + { tcaFrom = unqMarker (colIdx - 1, 0) (rowIdx - 1, 0), |
| 106 | + tcaTo = unqMarker (colIdx, 0) (rowIdx, 0), |
| 107 | + tcaEditAs = EditAsTwoCell |
| 108 | + }, |
| 109 | + anchObject = obj, |
| 110 | + anchClientData = def |
| 111 | + } |
33 | 112 | where |
34 | | - anchor1 = |
35 | | - Anchor |
36 | | - { anchAnchoring = |
37 | | - TwoCellAnchor |
38 | | - { tcaFrom = unqMarker (1, 0) (1, 0), |
39 | | - tcaTo = unqMarker (5, 0) (13, 0), |
40 | | - tcaEditAs = EditAsTwoCell |
41 | | - }, |
42 | | - anchObject = obj, |
43 | | - anchClientData = def |
44 | | - } |
45 | 113 | obj = |
46 | 114 | picture |
47 | | - (DrawingElementId 0) |
| 115 | + (DrawingElementId imgIdx) |
48 | 116 | FileInfo |
49 | 117 | { fiFilename = "img", |
50 | 118 | fiContentType = decodeUtf8 $ rfc2397Mime rfc2397, |
|
0 commit comments