Skip to content

Commit

Permalink
Merge pull request #499 from nfdi4plants/datamap_fix
Browse files Browse the repository at this point in the history
Fix datamap reader failing for empty datamap files
  • Loading branch information
HLWeil authored Mar 6, 2025
2 parents 3a6ea11 + e4f9b91 commit 180a10c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Spreadsheet/DataMap.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ open FsSpreadsheet
/// Reads an datamap from a spreadsheet
let fromFsWorkbook (doc : FsWorkbook) =
try
let worksheets = doc.GetWorksheets()
let sheetIsEmpty (sheet : FsWorksheet) = sheet.CellCollection.Count = 0
let dataMapTable =
doc.GetWorksheets()
worksheets
|> Seq.tryPick DataMapTable.tryFromFsWorksheet
match dataMapTable with
| Some table -> table
| None -> failwith "No DataMapTable found in workbook"
| None ->
if worksheets |> Seq.forall sheetIsEmpty then
DataMap.init()
else
failwith "No DataMapTable was found in any of the sheets of the workbook"
with
| err -> failwithf "Could not parse datamap: \n%s" err.Message

Expand Down
14 changes: 12 additions & 2 deletions tests/Spreadsheet/DataMapTests.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module DataMapTests
module DataMapTests


open ARCtrl
Expand Down Expand Up @@ -102,7 +102,7 @@ let private emptyTable =
testCase "Write" (fun () ->
let sheet = DataMapTable.toFsWorksheet t
Expect.equal "isa_datamap" sheet.Name "Worksheet name did not match"
Expect.equal 0 sheet.Rows.Count "Row count should be 0"
Expect.equal 0 sheet.Rows.Count "Row count should be 0"
)
testCase "Read" (fun () ->
let sheet = DataMapTable.toFsWorksheet t
Expand Down Expand Up @@ -168,10 +168,20 @@ let private simpleFile =
)
]

let private emptyDatamap =
testList "emptyDatamap" [
testCase "WriteAndRead" (fun () ->
let t = DataMap.init()
let wb = DataMap.toFsWorkbook t
let datamap = DataMap.fromFsWorkbook wb
Expect.equal datamap t "DataMap was not correctly written and read"
)
]
let main =
testList "DataMapTableTests" [
simpleTable
valuelessTable
emptyTable
simpleFile
emptyDatamap
]

0 comments on commit 180a10c

Please sign in to comment.