Skip to content

Proper support for cross-sheet references, was "Errors creating pivot tables in separate sheets" #160

Description

@LTibbetts

I have followed the examples for pivot tables from the test directory and have been able to successfully generate an xlsx file with a working pivot table. However, when I attempt to move the pivot table to a separate sheet, the writer throws the following exception.

test-xlsx: Safe.fromJustNote Nothing, specified pivot table field does not exist
CallStack (from HasCallStack):
  fromJustNote, called at src/Codec/Xlsx/Writer/Internal/PivotTable.hs:153:11 in xlsx-0.8.2-I2QgKfEkcpi1sJcqZkt6qB:Codec.Xlsx.Writer.Internal.PivotTable

If there is a working example of pivot tables being written to a different sheet, I would love to see it. Any help would be appreciated.

The full code example of what I am currently trying is this with the pivot table in the first sheet and the data in the second sheet.

import Codec.Xlsx
import Control.Lens
import Control.Monad.State.Strict
import qualified Data.ByteString.Lazy as L
import qualified Data.Map as M
import Data.Time.Clock.POSIX

main :: IO ()
main = do
  ct <- getPOSIXTime
  let sheet1 = def {_wsPivotTables = [testPivotTable]}
  let sheet2 = def {_wsCells = testPivotSrcCells}
  let xlsx = def & atSheet "Sheet1" ?~ sheet1 & atSheet "Sheet2" ?~ sheet2
  L.writeFile "example.xlsx" $ fromXlsx ct xlsx

testPivotTable :: PivotTable
testPivotTable =
  PivotTable
    { _pvtName = "PivotTable1",
      _pvtDataCaption = "Values",
      _pvtLocation = CellRef "A1",
      _pvtSrcRef = CellRef "A1:D5",
      _pvtSrcSheet = "Sheet2",
      _pvtRowFields = [FieldPosition colorField],
      _pvtColumnFields = [],
      _pvtDataFields =
        [ DataField
            { _dfName = "Sum of Price",
              _dfField = priceField,
              _dfFunction = ConsolidateSum
            }
        ],
      _pvtFields =
        [ PivotFieldInfo (Just colorField) False FieldSortManual [],
          PivotFieldInfo (Just yearField) False FieldSortManual [],
          PivotFieldInfo (Just priceField) False FieldSortManual [],
          PivotFieldInfo (Just countField) False FieldSortManual []
        ],
      _pvtRowGrandTotals = True,
      _pvtColumnGrandTotals = False,
      _pvtOutline = False,
      _pvtOutlineData = False
    }
  where
    colorField = PivotFieldName "Color"
    yearField = PivotFieldName "Year"
    priceField = PivotFieldName "Price"
    countField = PivotFieldName "Count"

testPivotSrcCells :: CellMap
testPivotSrcCells =
  M.fromList $
    concat
      [ [((row, col), def & cellValue ?~ v) | (col, v) <- zip [1 ..] cells]
        | (row, cells) <- zip [1 ..] cellMap
      ]
  where
    cellMap =
      [ [CellText "Color", CellText "Year", CellText "Price", CellText "Count"],
        [CellText "green", CellDouble 2012, CellDouble 12.23, CellDouble 17],
        [CellText "white", CellDouble 2011, CellDouble 73.99, CellDouble 21],
        [CellText "red", CellDouble 2012, CellDouble 10.19, CellDouble 172],
        [CellText "white", CellDouble 2012, CellDouble 34.99, CellDouble 49]
      ]

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions