Skip to content

Commit

Permalink
finish first version of json io functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Mar 14, 2024
1 parent 3f1fb55 commit 13dbca2
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 67 deletions.
20 changes: 10 additions & 10 deletions src/FsSpreadsheet.CsvIO/FsExtension.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module FsExtensions =
|> streamWriter.Write
streamWriter.Flush()

member self.ToBytes(?Separator : char) =
member self.ToXlsxBytes(?Separator : char) =
use memoryStream = new System.IO.MemoryStream()
match Separator with
| Some s -> self.ToStream(memoryStream,s)
Expand All @@ -73,8 +73,8 @@ module FsExtensions =

member self.ToFile(path,?Separator : char) =
match Separator with
| Some s -> self.ToBytes(s)
| None -> self.ToBytes()
| Some s -> self.ToXlsxBytes(s)
| None -> self.ToXlsxBytes()
|> fun bytes -> System.IO.File.WriteAllBytes (path, bytes)

static member toStream(stream : System.IO.MemoryStream,workbook : FsWorkbook,?Separator : char) =
Expand All @@ -83,12 +83,12 @@ module FsExtensions =
| None -> workbook.ToStream(stream)
workbook.ToStream(stream)

static member toBytes(workbook: FsWorkbook,?Separator : char) =
static member toXlsxBytes(workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToBytes(s)
| None -> workbook.ToBytes()
| Some s -> workbook.ToXlsxBytes(s)
| None -> workbook.ToXlsxBytes()

static member toFile(path,workbook: FsWorkbook,?Separator : char) =
static member toXlsxFile(path,workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToFile(path,s)
| None -> workbook.ToFile(path)
Expand All @@ -101,10 +101,10 @@ type Writer =
| None -> workbook.ToStream(stream)
workbook.ToStream(stream)

static member toBytes(workbook: FsWorkbook,?Separator : char) =
static member toXlsxBytes(workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToBytes(s)
| None -> workbook.ToBytes()
| Some s -> workbook.ToXlsxBytes(s)
| None -> workbook.ToXlsxBytes()

static member toFile(path,workbook: FsWorkbook,?Separator : char) =
match Separator with
Expand Down
48 changes: 34 additions & 14 deletions src/FsSpreadsheet.Js/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,43 @@ type FsWorkbook with
static member fromXlsxStream(stream:System.IO.Stream) : Promise<FsWorkbook> =
Xlsx.fromXlsxStream stream

static member fromBytes(bytes: byte []) : Promise<FsWorkbook> =
Xlsx.fromBytes bytes
static member fromXlsxBytes(bytes: byte []) : Promise<FsWorkbook> =
Xlsx.fromXlsxBytes bytes

static member toFile(path: string) (wb:FsWorkbook) : Promise<unit> =
Xlsx.toFile path wb
static member toXlsxFile(path: string) (wb:FsWorkbook) : Promise<unit> =
Xlsx.toXlsxFile path wb

static member toStream(stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
Xlsx.toStream stream wb
static member toXlsxStream(stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
Xlsx.toXlsxStream stream wb

static member toBytes(wb:FsWorkbook) : Promise<byte []> =
Xlsx.toBytes wb
static member toXlsxBytes(wb:FsWorkbook) : Promise<byte []> =
Xlsx.toXlsxBytes wb

member this.ToFile(path: string) : Promise<unit> =
FsWorkbook.toFile path this
member this.ToXlsxFile(path: string) : Promise<unit> =
FsWorkbook.toXlsxFile path this

member this.ToStream(stream: System.IO.Stream) : Promise<unit> =
FsWorkbook.toStream stream this
member this.ToXlsxStream(stream: System.IO.Stream) : Promise<unit> =
FsWorkbook.toXlsxStream stream this

member this.ToBytes() : Promise<byte []> =
FsWorkbook.toBytes this
member this.ToXlsxBytes() : Promise<byte []> =
FsWorkbook.toXlsxBytes this



static member fromJsonString (json:string) : FsWorkbook =
Json.fromJsonString json

static member toJsonString (wb:FsWorkbook) : string =
Json.toJsonString wb

//static member fromJsonFile (path:string) : Promise<FsWorkbook> =
// Json.fromJsonFile path

//static member toJsonFile (path:string) (wb:FsWorkbook) : Promise<unit> =
// Json.toJsonFile path wb

//member this.ToJsonFile(path: string) : Promise<unit> =
// FsWorkbook.toJsonFile path this

member this.ToJsonString() : string =
FsWorkbook.toJsonString this
6 changes: 3 additions & 3 deletions src/FsSpreadsheet.Js/FsSpreadsheet.Js.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
<Compile Include="Worksheet.fs" />
<Compile Include="Workbook.fs" />
<Compile Include="Xlsx.fs" />
<Compile Include="Json.fs" />
<Compile Include="FsExtensions.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fable.Exceljs" Version="1.6.0" />
<PackageReference Include="Fable.Promise" Version="3.2.0" />
<PackageReference Include="Thoth.Json.JavaScript" Version="0.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FsSpreadsheet\FsSpreadsheet.fsproj" />
</ItemGroup>
<ItemGroup>
<Content Include="*.fsproj; **\*.fs; **\*.fsi" PackagePath="fable\" />
</ItemGroup>
<ItemGroup />

<PropertyGroup>
<NpmDependencies>
Expand Down
37 changes: 37 additions & 0 deletions src/FsSpreadsheet.Js/Json.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace FsSpreadsheet.Js

open FsSpreadsheet
open Fable.ExcelJs
open Fable.Core
open Fable.Core.JsInterop
open Fable.Core.JS

open Thoth.Json.JavaScript




/// This does currently not correctly work if you want to use this from js
/// https://github.com/fable-compiler/Fable/issues/3498
[<AttachMembers>]
type Json =

static member fromJsonString (json:string) : FsWorkbook =
match Thoth.Json.JavaScript.Decode.fromString FsSpreadsheet.Json.Workbook.decode json with
| Ok wb -> wb
| Error e -> failwithf "Could not deserialize json Workbook: \n%s" e

static member toJsonString (wb:FsWorkbook, ?spaces) : string =
let spaces = defaultArg spaces 2
FsSpreadsheet.Json.Workbook.encode wb
|> Thoth.Json.JavaScript.Encode.toString spaces

//static member fromJsonFile (path:string) : Promise<FsWorkbook> =
// promise {
// let! json = Fable.Core.JS. path
// return Json.fromJsonString json
// }

//static member toJsonFile (path:string) (wb:FsWorkbook) : Promise<unit> =
// let json = Json.toJsonString wb
// Fable.Core.JS.writeFile path json
8 changes: 4 additions & 4 deletions src/FsSpreadsheet.Js/Xlsx.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ type Xlsx =
return JsWorkbook.readToFsWorkbook wb
}

static member fromBytes (bytes: byte []) : Promise<FsWorkbook> =
static member fromXlsxBytes (bytes: byte []) : Promise<FsWorkbook> =
promise {
let wb = ExcelJs.Excel.Workbook()
let uint8 = Fable.Core.JS.Constructors.Uint8Array.Create bytes
do! wb.xlsx.load(uint8.buffer)
return JsWorkbook.readToFsWorkbook wb
}

static member toFile (path: string) (wb:FsWorkbook) : Promise<unit> =
static member toXlsxFile (path: string) (wb:FsWorkbook) : Promise<unit> =
let jswb = JsWorkbook.writeFromFsWorkbook wb
jswb.xlsx.writeFile(path)

static member toStream (stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
static member toXlsxStream (stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
let jswb = JsWorkbook.writeFromFsWorkbook wb
jswb.xlsx.write(stream)

static member toBytes (wb:FsWorkbook) : Promise<byte []> =
static member toXlsxBytes (wb:FsWorkbook) : Promise<byte []> =
promise {
let jswb = JsWorkbook.writeFromFsWorkbook wb
let buffer = jswb.xlsx.writeBuffer()
Expand Down
64 changes: 48 additions & 16 deletions src/FsSpreadsheet.Net/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ module FsExtensions =
/// <summary>
/// Creates an FsWorkbook from a given Stream to an XlsxFile.
/// </summary>
static member fromBytes (bytes : byte []) =
static member fromXlsxBytes (bytes : byte []) =
let stream = new MemoryStream(bytes,writable = true)
FsWorkbook.fromXlsxStream stream

Expand All @@ -317,8 +317,17 @@ module FsExtensions =
/// </summary>
static member fromXlsxFile (filePath : string) =
let bytes = File.ReadAllBytes filePath
FsWorkbook.fromBytes bytes
FsWorkbook.fromXlsxBytes bytes

static member fromJsonString (json : string) =
match Thoth.Json.Newtonsoft.Decode.fromString FsSpreadsheet.Json.Workbook.decode json with
| Ok wb -> wb
| Error e -> failwithf "Could not deserialize json Workbook: \n%s" e


static member fromJsonFile (filePath : string) =
let json = File.ReadAllText filePath
FsWorkbook.fromJsonString json

member self.ToEmptySpreadsheet(doc : Packaging.SpreadsheetDocument) =

Expand All @@ -334,7 +343,7 @@ module FsExtensions =
/// <summary>
/// Writes the FsWorkbook into a given MemoryStream.
/// </summary>
member self.ToStream(stream : MemoryStream) =
member self.ToXlsxStream(stream : MemoryStream) =
if self.GetWorksheets() |> Seq.isEmpty then
failwith "Cannot write an empty workbook to a stream. Workbook did not contain any Worksheets."
let doc = Spreadsheet.initEmptyOnStream stream
Expand All @@ -348,28 +357,28 @@ module FsExtensions =
/// <summary>
/// Writes an FsWorkbook into a given MemoryStream.
/// </summary>
static member toStream stream (workbook : FsWorkbook) =
workbook.ToStream stream
static member toXlsxStream stream (workbook : FsWorkbook) =
workbook.ToXlsxStream stream

/// <summary>
/// Returns the FsWorkbook in the form of a byte array.
/// </summary>
member self.ToBytes() =
member self.ToXlsxBytes() =
use memoryStream = new MemoryStream()
self.ToStream(memoryStream)
self.ToXlsxStream(memoryStream)
memoryStream.ToArray()

/// <summary>
/// Returns an FsWorkbook in the form of a byte array.
/// </summary>
static member toBytes (workbook: FsWorkbook) =
workbook.ToBytes()
static member toXlsxBytes (workbook: FsWorkbook) =
workbook.ToXlsxBytes()

/// <summary>
/// Writes the FsWorkbook into a binary file at the given path.
/// </summary>
member self.ToXlsxFile(path) =
self.ToBytes()
self.ToXlsxBytes()
|> fun bytes -> File.WriteAllBytes (path, bytes)

/// <summary>
Expand All @@ -378,25 +387,48 @@ module FsExtensions =
static member toXlsxFile path (workbook : FsWorkbook) =
workbook.ToXlsxFile(path)

static member toJsonString (workbook : FsWorkbook, ?spaces) =
let spaces = defaultArg spaces 2
FsSpreadsheet.Json.Workbook.encode workbook
|> Thoth.Json.Newtonsoft.Encode.toString spaces

static member toJsonFile (path, ?spaces) =
fun workbook ->
let json = FsWorkbook.toJsonString (workbook,?spaces = spaces)
File.WriteAllText(path,json)

member this.ToJsonString(?spaces) =
FsWorkbook.toJsonString(this, ?spaces = spaces)

member this.ToJsonFile(path: string, ?spaces) =
FsWorkbook.toJsonFile(path, ?spaces = spaces) this

type Writer =


/// <summary>
/// Writes an FsWorkbook into a given MemoryStream.
/// </summary>
static member toStream(stream : MemoryStream, workbook : FsWorkbook) =
workbook.ToStream(stream)
static member toXlsxStream(stream : MemoryStream, workbook : FsWorkbook) =
workbook.ToXlsxStream(stream)


/// <summary>
/// Returns an FsWorkbook in the form of a byte array.
/// </summary>
static member toBytes(workbook: FsWorkbook) =
workbook.ToBytes()
static member toXlsxBytes(workbook: FsWorkbook) =
workbook.ToXlsxBytes()


/// <summary>
/// Writes an FsWorkbook into a binary file at the given path.
/// </summary>
static member toFile(path,workbook: FsWorkbook) =
workbook.ToXlsxFile(path)
static member toXlsxFile(path,workbook: FsWorkbook) =
workbook.ToXlsxFile(path)


static member toJsonString (workbook : FsWorkbook, ?spaces) =
FsWorkbook.toJsonString(workbook, ?spaces = spaces)

static member toJsonFile (path, ?spaces) =
fun workbook -> FsWorkbook.toJsonFile (path, ?spaces = spaces) workbook
1 change: 1 addition & 0 deletions src/FsSpreadsheet.Net/FsSpreadsheet.Net.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.16.0" />
<PackageReference Include="Thoth.Json.Newtonsoft" Version="0.1.0" />
<None Include="..\..\docs\img\logo.png" Pack="true" PackagePath="\" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet.Net/ZipArchiveReader.fs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ module FsWorkbook =
use zip = new ZipArchive(stream)
fromZipArchive zip

let fromBytes (bytes : byte []) =
let fromXlsxBytes (bytes : byte []) =
use ms = new MemoryStream(bytes)
fromStream ms

Expand Down
Loading

0 comments on commit 13dbca2

Please sign in to comment.