Skip to content

Commit

Permalink
Merge pull request #88 from fslaborg/json
Browse files Browse the repository at this point in the history
Added json IO
  • Loading branch information
HLWeil committed Mar 19, 2024
2 parents 8bb742f + 5e71fb9 commit ed6795c
Show file tree
Hide file tree
Showing 45 changed files with 713 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ output/
/tests/FsSpreadsheet.JsNativeTests/fable/**/*.js
/tests/FsSpreadsheet.Net.Tests/TestFiles/WRITE_*.xlsx
/tests/JS/TestFiles/WRITE_*.xlsx
/tests/TestUtils/TestFiles/TestWorkbook_FsSpreadsheet_WRITE.*.xlsx
/tests/TestUtils/TestFiles/TestWorkbook_FsSpreadsheet_WRITE.**
/js
**/py/**
/.venv
Expand Down
54 changes: 50 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ npm install @fslab/fsspreadsheet
pip install fsspreadsheet
```

## Usage_IO
## Usage_Xlsx_IO

### F#

Expand Down Expand Up @@ -59,7 +59,7 @@ const wb = Xlsx.fromXlsxFile(path)

const newPath = "path/to/new/spreadsheet.xlsx"

Xlsx.toFile(newPath,wb)
Xlsx.toXlsxFile(newPath,wb)
```

### Python
Expand All @@ -69,9 +69,55 @@ from fsspreadsheet.xlsx import Xlsx

path = "path/to/spreadsheet.xlsx"

wb = Xlsx.fromXlsxFile(path)
wb = Xlsx.from_xlsx_file(path)

newPath = "path/to/new/spreadsheet.xlsx"

Xlsx.to_file(newPath,wb)
Xlsx.to_xlsx_file(newPath,wb)
```


## Usage_Json_IO

### F#

```fsharp
open FsSpreadsheet
open FsSpreadsheet.Net
let path = "path/to/spreadsheet.json"
let wb = FsWorkbook.fromJsonFile(path)
let newPath = "path/to/new/spreadsheet.json"
wb.ToJsonFile(newPath)
```

### Javascript

```javascript
import { Json } from '@fslab/fsspreadsheet/Json.js';

const path = "path/to/spreadsheet.json"

const wb = Json.fromJsonFile(path)

const newPath = "path/to/new/spreadsheet.json"

Json.toJsonFile(newPath,wb)
```

### Python

```python
from fsspreadsheet.json import Json

path = "path/to/spreadsheet.json"

wb = Json.from_json_file(path)

newPath = "path/to/new/spreadsheet.json"

Json.to_json_file(newPath,wb)
```
54 changes: 27 additions & 27 deletions src/FsSpreadsheet.CsvIO/FsExtension.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,55 +58,55 @@ module FsExtensions =

type FsWorkbook with

member self.ToStream(stream : System.IO.MemoryStream,?Separator : char) =
member self.ToCsvStream(stream : System.IO.MemoryStream,?Separator : char) =
let streamWriter = new System.IO.StreamWriter(stream)
self.GetWorksheets().[0].ToTableString(Separator)
|> streamWriter.Write
streamWriter.Flush()

member self.ToBytes(?Separator : char) =
member self.ToCsvBytes(?Separator : char) =
use memoryStream = new System.IO.MemoryStream()
match Separator with
| Some s -> self.ToStream(memoryStream,s)
| None -> self.ToStream(memoryStream)
| Some s -> self.ToCsvStream(memoryStream,s)
| None -> self.ToCsvStream(memoryStream)
memoryStream.ToArray()

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

static member toStream(stream : System.IO.MemoryStream,workbook : FsWorkbook,?Separator : char) =
static member toCsvStream(stream : System.IO.MemoryStream,workbook : FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToStream(stream,s)
| None -> workbook.ToStream(stream)
workbook.ToStream(stream)
| Some s -> workbook.ToCsvStream(stream,s)
| None -> workbook.ToCsvStream(stream)
workbook.ToCsvStream(stream)

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

static member toFile(path,workbook: FsWorkbook,?Separator : char) =
static member toCsvFile(path,workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToFile(path,s)
| None -> workbook.ToFile(path)
| Some s -> workbook.ToCsvFile(path,s)
| None -> workbook.ToCsvFile(path)

type Writer =

static member toStream(stream : System.IO.MemoryStream,workbook : FsWorkbook,?Separator : char) =
static member toCsvStream(stream : System.IO.MemoryStream,workbook : FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToStream(stream,s)
| None -> workbook.ToStream(stream)
workbook.ToStream(stream)
| Some s -> workbook.ToCsvStream(stream,s)
| None -> workbook.ToCsvStream(stream)
workbook.ToCsvStream(stream)

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

static member toFile(path,workbook: FsWorkbook,?Separator : char) =
static member toCsvFile(path,workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToFile(path,s)
| None -> workbook.ToFile(path)
| Some s -> workbook.ToCsvFile(path,s)
| None -> workbook.ToCsvFile(path)
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
42 changes: 42 additions & 0 deletions src/FsSpreadsheet.Js/Json.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 tryFromJsonString (json:string) : Result<FsWorkbook, string> =
match Thoth.Json.JavaScript.Decode.fromString FsSpreadsheet.Json.Workbook.decode json with
| Ok wb -> Ok wb
| Error e -> Error e

static member fromJsonString (json:string) : FsWorkbook =
match Json.tryFromJsonString 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
31 changes: 27 additions & 4 deletions src/FsSpreadsheet.Js/Xlsx.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,57 @@ type Xlsx =
return fswb
}

[<System.Obsolete("Use fromXlsxFile instead")>]
static member fromFile (path:string) : Promise<FsWorkbook> =
Xlsx.fromXlsxFile path

static member fromXlsxStream (stream:System.IO.Stream) : Promise<FsWorkbook> =
promise {
let wb = ExcelJs.Excel.Workbook()
do! wb.xlsx.read stream
return JsWorkbook.readToFsWorkbook wb
}

static member fromBytes (bytes: byte []) : Promise<FsWorkbook> =
[<System.Obsolete("Use fromXlsxStream instead")>]
static member fromStream (stream:System.IO.Stream) : Promise<FsWorkbook> =
Xlsx.fromXlsxStream stream

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> =
[<System.Obsolete("Use fromXlsxBytes instead")>]
static member fromBytes (bytes: byte []) : Promise<FsWorkbook> =
Xlsx.fromXlsxBytes bytes

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> =
[<System.Obsolete("Use toXlsxFile instead")>]
static member toFile (path: string) (wb:FsWorkbook) : Promise<unit> =
Xlsx.toXlsxFile path wb

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 []> =
[<System.Obsolete("Use toXlsxStream instead")>]
static member toStream (stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
Xlsx.toXlsxStream stream wb

static member toXlsxBytes (wb:FsWorkbook) : Promise<byte []> =
promise {
let jswb = JsWorkbook.writeFromFsWorkbook wb
let buffer = jswb.xlsx.writeBuffer()
return !!buffer
}

[<System.Obsolete("Use toXlsxBytes instead")>]
static member toBytes (wb:FsWorkbook) : Promise<byte []> =
Xlsx.toXlsxBytes wb

0 comments on commit ed6795c

Please sign in to comment.