Skip to content

Commit

Permalink
naming adjustments and docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Mar 15, 2024
1 parent 1586c5a commit 02baf70
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 46 deletions.
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.ToXlsxBytes(?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.ToXlsxBytes(s)
| None -> self.ToXlsxBytes()
| 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 toXlsxBytes(workbook: FsWorkbook,?Separator : char) =
static member toCsvBytes(workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToXlsxBytes(s)
| None -> workbook.ToXlsxBytes()
| Some s -> workbook.ToCsvBytes(s)
| None -> workbook.ToCsvBytes()

static member toXlsxFile(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 toXlsxBytes(workbook: FsWorkbook,?Separator : char) =
static member toCsvBytes(workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToXlsxBytes(s)
| None -> workbook.ToXlsxBytes()
| 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)
8 changes: 4 additions & 4 deletions src/FsSpreadsheet.Net/ZipArchiveReader.fs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ module FsWorkbook =
let fromZipArchive (wb : ZipArchive) =
parseWorkbook wb

let fromStream (stream : Stream) =
let fromXlsxStream (stream : Stream) =
use zip = new ZipArchive(stream)
fromZipArchive zip

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

let fromFile (path : string) =
let fromXlsxFile (path : string) =
use fs = File.OpenRead(path)
fromStream fs
fromXlsxStream fs
6 changes: 3 additions & 3 deletions src/FsSpreadsheet.Py/FsExtension.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type FsWorkbook with
static member toXlsxFile(path: string) (wb:FsWorkbook) : unit =
Xlsx.toXlsxFile path wb

//static member toStream(stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
//static member toXlsxStream(stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
// PyWorkbook.fromFsWorkbook wb
// |> fun wb -> Xlsx.writeBuffer(wb,stream)

Expand All @@ -30,8 +30,8 @@ type FsWorkbook with
member this.ToXlsxFile(path: string) : unit =
FsWorkbook.toXlsxFile path this

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

member this.ToXlsxBytes() : byte [] =
FsWorkbook.toXlsxBytes this
Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet.Py/Xlsx.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Xlsx =
PyWorkbook.fromFsWorkbook wb
|> fun wb -> Xlsx.writeFile(wb,path)

//static member toStream(stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
//static member toXlsxStream(stream: System.IO.Stream) (wb:FsWorkbook) : Promise<unit> =
// PyWorkbook.fromFsWorkbook wb
// |> fun wb -> Xlsx.writeBuffer(wb,stream)

Expand Down
6 changes: 3 additions & 3 deletions tests/FsSpreadsheet.Net.Tests/ZipArchiveReader.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ let tests_Read = testList "Read" [
let readFromTestFile (testFile: DefaultTestObject.TestFiles) =
try
let p = testFile.asRelativePath
FsWorkbook.fromFile(p)
FsWorkbook.fromXlsxFile(p)
with
| err ->
printfn "Could not read file from default path: %s" err.Message
let p = $"{DefaultTestObject.testFolder}/{testFile.asFileName}"
FsWorkbook.fromFile(p)
FsWorkbook.fromXlsxFile(p)

testCase "FsCell equality" <| fun _ ->
let c1 = FsCell(1, DataType.Number, FsAddress("A2"))
Expand Down Expand Up @@ -42,7 +42,7 @@ open FsSpreadsheet.Net

let performanceTest = testList "Performance" [
testCase "BigFile" <| fun _ ->
let readF() = FsWorkbook.fromFile(DefaultTestObject.BigFile.asRelativePath) |> ignore
let readF() = FsWorkbook.fromXlsxFile(DefaultTestObject.BigFile.asRelativePath) |> ignore
let refReadF() = FsWorkbook.fromXlsxFile(DefaultTestObject.BigFile.asRelativePath) |> ignore
Expect.isFasterThan readF refReadF "ZipArchiveReader should be faster than standard reader"
//Expect.equal (wb.GetWorksheetAt(1).Rows.Count) 153991 "Row count should be equal"
Expand Down
8 changes: 4 additions & 4 deletions tests/Speedtest/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ let main argv =
let zipArchiveReader() =


let readAssay() = ZipArchiveReader.FsWorkbook.fromFile assayPath
let readStudy() = ZipArchiveReader.FsWorkbook.fromFile studyPath
let readInvestigation() = ZipArchiveReader.FsWorkbook.fromFile investigationPath
let bigFile() = ZipArchiveReader.FsWorkbook.fromFile @"C:\Users\HLWei\source\repos\IO\FsSpreadsheet\tests\TestUtils\TestFiles\BigFile.xlsx"
let readAssay() = ZipArchiveReader.FsWorkbook.fromXlsxFile assayPath
let readStudy() = ZipArchiveReader.FsWorkbook.fromXlsxFile studyPath
let readInvestigation() = ZipArchiveReader.FsWorkbook.fromXlsxFile investigationPath
let bigFile() = ZipArchiveReader.FsWorkbook.fromXlsxFile @"C:\Users\HLWei\source\repos\IO\FsSpreadsheet\tests\TestUtils\TestFiles\BigFile.xlsx"


readInvestigation() |> ignore
Expand Down

0 comments on commit 02baf70

Please sign in to comment.