diff --git a/docs/index.md b/docs/index.md index 30e2a1f..b8905a5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -31,7 +31,7 @@ npm install @fslab/fsspreadsheet pip install fsspreadsheet ``` -## Usage_IO +## Usage_Xlsx_IO ### F# @@ -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 @@ -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) ``` diff --git a/src/FsSpreadsheet.CsvIO/FsExtension.fs b/src/FsSpreadsheet.CsvIO/FsExtension.fs index 911775c..d1b84bb 100644 --- a/src/FsSpreadsheet.CsvIO/FsExtension.fs +++ b/src/FsSpreadsheet.CsvIO/FsExtension.fs @@ -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) \ No newline at end of file + | Some s -> workbook.ToCsvFile(path,s) + | None -> workbook.ToCsvFile(path) \ No newline at end of file diff --git a/src/FsSpreadsheet.Net/ZipArchiveReader.fs b/src/FsSpreadsheet.Net/ZipArchiveReader.fs index 6a8ab58..e0f31fc 100644 --- a/src/FsSpreadsheet.Net/ZipArchiveReader.fs +++ b/src/FsSpreadsheet.Net/ZipArchiveReader.fs @@ -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 \ No newline at end of file + fromXlsxStream fs \ No newline at end of file diff --git a/src/FsSpreadsheet.Py/FsExtension.fs b/src/FsSpreadsheet.Py/FsExtension.fs index 7cd24a9..eadbf41 100644 --- a/src/FsSpreadsheet.Py/FsExtension.fs +++ b/src/FsSpreadsheet.Py/FsExtension.fs @@ -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 = + //static member toXlsxStream(stream: System.IO.Stream) (wb:FsWorkbook) : Promise = // PyWorkbook.fromFsWorkbook wb // |> fun wb -> Xlsx.writeBuffer(wb,stream) @@ -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 diff --git a/src/FsSpreadsheet.Py/Xlsx.fs b/src/FsSpreadsheet.Py/Xlsx.fs index 4b412b1..9014b43 100644 --- a/src/FsSpreadsheet.Py/Xlsx.fs +++ b/src/FsSpreadsheet.Py/Xlsx.fs @@ -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 = + //static member toXlsxStream(stream: System.IO.Stream) (wb:FsWorkbook) : Promise = // PyWorkbook.fromFsWorkbook wb // |> fun wb -> Xlsx.writeBuffer(wb,stream) diff --git a/tests/FsSpreadsheet.Net.Tests/ZipArchiveReader.fs b/tests/FsSpreadsheet.Net.Tests/ZipArchiveReader.fs index 8bff1d9..70710e9 100644 --- a/tests/FsSpreadsheet.Net.Tests/ZipArchiveReader.fs +++ b/tests/FsSpreadsheet.Net.Tests/ZipArchiveReader.fs @@ -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")) @@ -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" diff --git a/tests/Speedtest/Program.fs b/tests/Speedtest/Program.fs index 5c3d005..0ef75a9 100644 --- a/tests/Speedtest/Program.fs +++ b/tests/Speedtest/Program.fs @@ -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