Skip to content

Commit

Permalink
add Assay Metadatasheet manipulation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Nov 3, 2021
1 parent 3fd7fca commit 4105894
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/ISADotNet.XLSX/AssayFile/Assay.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ module Assay =
/// Diesen Block durch JS ersetzen ---->
/// Create a new ISADotNet.XLSX assay file constisting of two sheets. The first has the name of the assayIdentifier and is meant to store parameters used in the assay. The second stores additional assay metadata
let init metadataSheetName assayIdentifier path =
let init metadataSheetName assay persons assayIdentifier path =
Spreadsheet.initWithSst assayIdentifier path
|> MetaData.init metadataSheetName
|> MetaData.init metadataSheetName assay persons
|> Spreadsheet.close

/// Reads an assay from an xlsx spreadsheetdocument
Expand Down
134 changes: 130 additions & 4 deletions src/ISADotNet.XLSX/AssayFile/MetaData.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@ module MetaData =
failwith "emptyInvestigationFile"


///let doc = Spreadsheet.fromFile path true
///
///MetadataSheet.overwriteWithAssayInfo "Investigation" testAssay2 doc
///
///MetadataSheet.overwriteWithPersons "Investigation" [person] doc
///
///MetadataSheet.getPersons "Investigation" doc
///
///MetadataSheet.tryGetAssay "Investigation" doc
///
///doc.Close()
/// Diesen Block durch JS ersetzen ---->
open DocumentFormat.OpenXml.Packaging
open DocumentFormat.OpenXml.Spreadsheet

/// Creates a new row from the given values.
let ofSparseValues rowIndex (vals : 'T option seq) =
let spans = Row.Spans.fromBoundaries 1u (Seq.length vals |> uint)
Expand All @@ -63,14 +78,17 @@ module MetaData =
|> Row.create rowIndex spans

/// Append an assay metadata sheet with the given sheetname to an existing assay file excel spreadsheet
let init sheetName (doc: DocumentFormat.OpenXml.Packaging.SpreadsheetDocument) =
let init sheetName (assay : Assay option) (persons : Person list option) (doc: DocumentFormat.OpenXml.Packaging.SpreadsheetDocument) =

let sheet = SheetData.empty()

let worksheetComment = Comment.make None (Some "Worksheet") None
let personWithComment = Person.make None None None None None None None None None None (Some [worksheetComment])

toRows Assay.empty [personWithComment]
let personWithComment = [Person.make None None None None None None None None None None (Some [worksheetComment])]

let assay = Option.defaultValue Assay.empty assay
let persons = Option.defaultValue personWithComment persons

toRows assay persons
|> Seq.mapi (fun i row ->
row
|> SparseRow.getAllValues
Expand All @@ -88,4 +106,112 @@ module MetaData =

doc


module Worksheet =

let setSheetData (sheetData : SheetData) (worksheet : Worksheet) =
if Worksheet.hasSheetData worksheet then
worksheet.RemoveChild(Worksheet.getSheetData worksheet)
|> ignore
Worksheet.addSheetData sheetData worksheet

/// Replace the sheetdata of the sheet with the given sheetname
let private replaceSheetData (sheetName : string) (data : SheetData) (workbookPart : WorkbookPart) =

let workbook = Workbook.getOrInit workbookPart

let sheets = Sheet.Sheets.getOrInit workbook
let id =
sheets |> Sheet.Sheets.getSheets
|> Seq.find (fun sheet -> Sheet.getName sheet = sheetName)
|> Sheet.getID

WorkbookPart.getWorksheetPartById id workbookPart
|> Worksheet.getOrInit
|> Worksheet.setSheetData data
|> ignore

workbookPart

/// Try get assay from metadatasheet with given sheetName
let tryGetAssay sheetName (doc : SpreadsheetDocument) =
match Spreadsheet.tryGetSheetBySheetName sheetName doc with
| Some sheet ->
sheet
|> SheetData.getRows
|> Seq.map (Row.getIndexedValues None >> Seq.map (fun (i,v) -> (int i) - 1, v))
|> fromRows
|> fun (a,p) ->
a
| None -> failwithf "Metadata sheetname %s could not be found" sheetName

/// Try get persons from metadatasheet with given sheetName
let getPersons sheetName (doc : SpreadsheetDocument) =
match Spreadsheet.tryGetSheetBySheetName sheetName doc with
| Some sheet ->
sheet
|> SheetData.getRows
|> Seq.map (Row.getIndexedValues None >> Seq.map (fun (i,v) -> (int i) - 1, v))
|> fromRows
|> fun (a,p) ->
p
| None -> failwithf "Metadata sheetname %s could not be found" sheetName

/// Replaces assay metadata from metadatasheet with given sheetName
let overwriteWithAssayInfo sheetName assay (doc : SpreadsheetDocument) =

let workBookPart = Spreadsheet.getWorkbookPart doc
let newSheet = SheetData.empty()

match Spreadsheet.tryGetSheetBySheetName sheetName doc with
| Some sheet ->
sheet
|> SheetData.getRows
|> Seq.map (Row.getIndexedValues None >> Seq.map (fun (i,v) -> (int i) - 1, v))
|> fromRows
|> fun (_,p) ->

toRows assay p
|> Seq.mapi (fun i row ->
row
|> SparseRow.getAllValues
|> ofSparseValues (i+1 |> uint)
)
|> Seq.fold (fun s r ->
SheetData.appendRow r s
) newSheet
|> fun s -> replaceSheetData sheetName s workBookPart
| None -> failwithf "Metadata sheetname %s could not be found" sheetName
|> ignore

doc.Save()

/// Replaces persons from metadatasheet with given sheetName
let overwriteWithPersons sheetName persons (doc : SpreadsheetDocument) =

let workBookPart = Spreadsheet.getWorkbookPart doc
let newSheet = SheetData.empty()

match Spreadsheet.tryGetSheetBySheetName sheetName doc with
| Some sheet ->
sheet
|> SheetData.getRows
|> Seq.map (Row.getIndexedValues None >> Seq.map (fun (i,v) -> (int i) - 1, v))
|> fromRows
|> fun (a,_) ->
toRows (Option.defaultValue Assay.empty a) persons
|> Seq.mapi (fun i row ->
row
|> SparseRow.getAllValues
|> ofSparseValues (i+1 |> uint)
)
|> Seq.fold (fun s r ->
SheetData.appendRow r s
) newSheet
|> fun s -> replaceSheetData sheetName s workBookPart
| None -> failwithf "Metadata sheetname %s could not be found" sheetName
|> ignore

doc.Save()

/// ----> Bis hier

0 comments on commit 4105894

Please sign in to comment.