Skip to content

Commit

Permalink
Start refactoring to resizeArray 🔨#53
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Jul 28, 2023
1 parent 53b109c commit 58b57ae
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 47 deletions.
8 changes: 7 additions & 1 deletion FsSpreadsheet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FsSpreadsheet.Interactive.T
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FsSpreadsheet.Exceljs", "src\FsSpreadsheet.Exceljs\FsSpreadsheet.Exceljs.fsproj", "{E72A14FF-5026-463B-B0FA-2DA104D67B0C}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FsSpreadsheet.Exceljs.Tests", "tests\FsSpreadsheet.Exceljs.Tests\FsSpreadsheet.Exceljs.Tests.fsproj", "{96E12F19-B25A-415E-B965-F9DE8D713C67}"
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FsSpreadsheet.Exceljs.Tests", "tests\FsSpreadsheet.Exceljs.Tests\FsSpreadsheet.Exceljs.Tests.fsproj", "{96E12F19-B25A-415E-B965-F9DE8D713C67}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "JS", "JS", "{ADCF7D08-F2EE-4DFD-A96A-7E0134A1546F}"
ProjectSection(SolutionItems) = preProject
tests\JS\Exceljs.js = tests\JS\Exceljs.js
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -187,6 +192,7 @@ Global
{7AC6357C-BDA0-40AE-955E-953C4BFF5BB0} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
{E72A14FF-5026-463B-B0FA-2DA104D67B0C} = {1CF1274C-DE28-4446-9B4E-5884E797B87B}
{96E12F19-B25A-415E-B965-F9DE8D713C67} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
{ADCF7D08-F2EE-4DFD-A96A-7E0134A1546F} = {F77AD108-C6B4-46BB-B7BC-13573F45F876}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0EDE6697-0F13-4DB1-AC56-12C15A72D395}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"testexceljs": "mocha tests/FsSpreadsheet.Exceljs.Tests/js",
"clean-testjs": "cd tests/JS/FsSpreadsheet.Exceljs & dotnet fable clean --extension .fs.js --yes",
"pretestjs": "npm run clean-testjs && dotnet fable src/FsSpreadsheet.Exceljs -o tests/JS/FsSpreadsheet.Exceljs --extension .fs.js",
"testjs": ""
"testjs": "mocha tests/JS"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet.CsvIO/FsExtension.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module FsExtensions =

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

Expand Down
7 changes: 2 additions & 5 deletions src/FsSpreadsheet.ExcelIO/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module FsExtensions =
/// </summary>
member self.GetWorksheetOfTable(workbook : FsWorkbook) =
workbook.GetWorksheets()
|> List.find (
|> Seq.find (
fun s ->
s.Tables
|> List.exists (fun t -> t.Name = self.Name)
Expand Down Expand Up @@ -238,17 +238,14 @@ module FsExtensions =

let workbookPart = Spreadsheet.initWorkbookPart doc

self.GetWorksheets()
|> List.iter (fun worksheet ->

for worksheet in self.GetWorksheets() do
let worksheetPart =
WorkbookPart.appendWorksheet worksheet.Name (worksheet.ToXlsxWorksheet()) workbookPart
|> WorkbookPart.getOrInitWorksheetPartByName worksheet.Name

worksheet.AppendTablesToWorksheetPart(workbookPart,worksheetPart)
//Worksheet.setSheetData sheetData sheet |> ignore
//WorkbookPart.appendWorksheet worksheet.Name sheet workbookPart |> ignore
)

Spreadsheet.close doc

Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet.Exceljs/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ open Fable.Core.JsInterop
// If you want to use these bindings from js, you should use the ones in `Xlsx.fs`
type FsWorkbook with

static member fromXlsxFile(path:string) : Async<FsWorkbook> =
static member fromXlsxFile(path:string) : Promise<FsWorkbook> =
Xlsx.fromXlsxFile(path)

static member fromXlsxStream(stream:System.IO.Stream) : Async<FsWorkbook> =
Expand Down
3 changes: 2 additions & 1 deletion src/FsSpreadsheet.Exceljs/FsSpreadsheet.Exceljs.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fable.Exceljs" Version="1.3.6" />
<PackageReference Include="Fable.Exceljs" Version="1.3.6" />
<PackageReference Include="Fable.Promise" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/FsSpreadsheet.Exceljs/Worksheet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ module JsWorksheet =
ws.addTable(table)
|> ignore


let addJsWorksheet (wb: FsWorkbook) (jsws: Worksheet) : unit =
let fsws = FsWorksheet(jsws.name)
for row in jsws.rows do
jsws.eachRow(fun (row, rowIndex) ->
row.eachCell(fun (c, rowIndex) ->
if c.value.IsSome then
let t = enum<Unions.ValueType>(c.``type``)
Expand All @@ -69,6 +68,7 @@ module JsWorksheet =
vTemp |> createFscell
fsws.AddCell(fscell) |> ignore
)
)
for jstableref in jsws.getTables() do
let table = jstableref.table.Value
let tableRef = table.tableRef |> FsRangeAddress
Expand Down
10 changes: 8 additions & 2 deletions src/FsSpreadsheet.Exceljs/Xlsx.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ open FsSpreadsheet
open Fable.ExcelJs
open Fable.Core
open Fable.Core.JsInterop
open Fable.Core.JS


/// This does currently not correctly work if you want to use this from js
/// https://github.com/fable-compiler/Fable/issues/3498
[<AttachMembers>]
type Xlsx =
static member fromXlsxFile (path:string) : Async<FsWorkbook> =
static member fromXlsxFile (path:string) : Promise<FsWorkbook> =
async {
let wb = ExcelJs.Excel.Workbook()
do! wb.xlsx.readFile(path)
return JsWorkbook.toFsWorkbook wb
let fswb = JsWorkbook.toFsWorkbook wb
return fswb
}
|> Async.StartAsPromise

static member fromXlsxStream (stream:System.IO.Stream) : Async<FsWorkbook> =
async {
Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet.Interactive/Formatters.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Formatters =
let formatWorkbook (workbook: FsWorkbook) =
div [] [
style [] [str ".fs-table, .fs-th, .fs-td { border: 1px solid black !important; text-align: left; border-collapse: collapse;}"]
h3 [] [ str $"FsWorkbook with {workbook.GetWorksheets().Length} worksheets:" ]
h3 [] [ str $"FsWorkbook with {workbook.GetWorksheets().Count} worksheets:" ]
table [_class "fs-table"] [
thead [] [
tr [] [
Expand Down
31 changes: 16 additions & 15 deletions src/FsSpreadsheet/FsWorkbook.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open Fable.Core
[<AttachMembers>]
type FsWorkbook() =

let mutable _worksheets = []
let _worksheets = ResizeArray()

interface System.IDisposable with
member self.Dispose() = ()
Expand All @@ -27,7 +27,7 @@ type FsWorkbook() =
/// Creates a deep copy of this FsWorkbook.
/// </summary>
member self.Copy() =
let shts = self.GetWorksheets() |> List.map (fun (s : FsWorksheet) -> s.Copy())
let shts = self.GetWorksheets().ToArray() |> Array.map (fun (s : FsWorksheet) -> s.Copy())
let wb = new FsWorkbook()
wb.AddWorksheets shts
wb
Expand All @@ -43,7 +43,7 @@ type FsWorkbook() =
/// </summary>
member self.InitWorksheet(name : string) =
let sheet = FsWorksheet name
_worksheets <- List.append _worksheets [sheet]
_worksheets.Add(sheet)
sheet

/// <summary>
Expand All @@ -57,10 +57,10 @@ type FsWorkbook() =
/// Adds a given FsWorksheet to the FsWorkbook.
/// </summary>
member self.AddWorksheet(sheet : FsWorksheet) =
if _worksheets |> List.exists (fun ws -> ws.Name = sheet.Name) then
if _worksheets |> Seq.exists (fun ws -> ws.Name = sheet.Name) then
failwithf "Could not add worksheet with name \"%s\" to workbook as it already contains a worksheet with the same name" sheet.Name
else
_worksheets <- List.append _worksheets [sheet]
_worksheets.Add(sheet)

/// <summary>
/// Adds an FsWorksheet to an FsWorkbook.
Expand All @@ -86,7 +86,7 @@ type FsWorkbook() =
/// <summary>
/// Returns all FsWorksheets.
/// </summary>
member self.GetWorksheets() =
member self.GetWorksheets() : ResizeArray<FsWorksheet> =
_worksheets

/// <summary>
Expand All @@ -99,7 +99,7 @@ type FsWorkbook() =
/// Returns the FsWorksheet with the given 1 based index if it exists. Else returns None.
/// </summary>
member self.TryGetWorksheetAt(index : int) =
_worksheets |> List.tryItem (index - 1)
_worksheets |> Seq.tryItem (index - 1)

/// <summary>
/// Returns the FsWorksheet with the given 1 based index if it exists in a given FsWorkbook. Else returns None.
Expand Down Expand Up @@ -127,7 +127,7 @@ type FsWorkbook() =
/// Returns the FsWorksheet with the given name if it exists in the FsWorkbook. Else returns None.
/// </summary>
member self.TryGetWorksheetByName(sheetName) =
_worksheets |> List.tryFind (fun w -> w.Name = sheetName)
_worksheets |> Seq.tryFind (fun w -> w.Name = sheetName)

/// <summary>
/// Returns the FsWorksheet with the given name if it exists in a given FsWorkbook. Else returns None.
Expand Down Expand Up @@ -155,11 +155,12 @@ type FsWorkbook() =
/// </summary>
/// <exception cref="System.Exception">if FsWorksheet with given name is not present in the FsWorkkbook.</exception>
member self.RemoveWorksheet(name : string) =
let filteredWorksheets =
match _worksheets |> List.tryFind (fun ws -> ws.Name = name) with
| Some w -> _worksheets |> List.filter (fun ws -> ws.Name <> name)
| None -> failwith $"FsWorksheet with name {name} was not found in FsWorkbook."
_worksheets <- filteredWorksheets
let ws =
try
_worksheets.Find(fun ws -> ws.Name = name)
with
| _ -> failwith $"FsWorksheet with name {name} was not found in FsWorkbook."
_worksheets.Remove(ws) |> ignore

/// <summary>
/// Removes an FsWorksheet with given name from an FsWorkbook.
Expand All @@ -172,8 +173,8 @@ type FsWorkbook() =
/// Returns all FsTables from the FsWorkbook.
/// </summary>
member self.GetTables() =
self.GetWorksheets()
|> List.collect (fun s -> s.Tables)
self.GetWorksheets().ToArray()
|> Array.collect (fun s -> s.Tables |> Array.ofSeq)

/// <summary>
/// Returns all FsTables from an FsWorkbook.
Expand Down
6 changes: 3 additions & 3 deletions tests/FsSpreadsheet.ExcelIO.Tests/FsWorkbook.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ let writeAndReadBytes =
let wb = new FsWorkbook()
let bytes = wb.ToBytes()
let wb2 = FsWorkbook.fromBytes(bytes)
Expect.equal (wb.GetWorksheets() |> List.length) (wb2.GetWorksheets() |> List.length) "Worksheet count should be equal"
Expect.equal (wb.GetWorksheets().Count) (wb2.GetWorksheets().Count) "Worksheet count should be equal"
)
testCase "SingleWorksheet" (fun () ->
let wb = new FsWorkbook()
let ws = TestObjects.sheet1()
wb.AddWorksheet(ws)
let bytes = wb.ToBytes()
let wb2 = FsWorkbook.fromBytes(bytes)
Expect.equal (wb.GetWorksheets() |> List.length) (wb2.GetWorksheets() |> List.length) "Worksheet count should be equal"
Expect.equal (wb.GetWorksheets().Count) (wb2.GetWorksheets().Count) "Worksheet count should be equal"
Expect.workSheetEqual (wb.GetWorksheetByName(TestObjects.sheet1Name)) (wb2.GetWorksheetByName(TestObjects.sheet1Name)) "Worksheet did not match"
)
testCase "MultipleWorksheets" (fun () ->
Expand All @@ -29,7 +29,7 @@ let writeAndReadBytes =
wb.AddWorksheet(TestObjects.sheet2())
let bytes = wb.ToBytes()
let wb2 = FsWorkbook.fromBytes(bytes)
Expect.equal (wb.GetWorksheets() |> List.length) (wb2.GetWorksheets() |> List.length) "Worksheet count should be equal"
Expect.equal (wb.GetWorksheets().Count) (wb2.GetWorksheets().Count) "Worksheet count should be equal"
Expect.workSheetEqual (wb.GetWorksheetByName(TestObjects.sheet1Name)) (wb2.GetWorksheetByName(TestObjects.sheet1Name)) "First Worksheet did not match"
Expect.workSheetEqual (wb.GetWorksheetByName(TestObjects.sheet2Name)) (wb2.GetWorksheetByName(TestObjects.sheet2Name)) "Second Worksheet did not match"
)
Expand Down
18 changes: 9 additions & 9 deletions tests/FsSpreadsheet.Exceljs.Tests/Workbook.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let private tests_toFsWorkbook = testList "toFsWorkbook" [
Expect.passWithMsg "Convert to fswb"
let fswsList = fswb.GetWorksheets()
let jswsList = jswb.worksheets
Expect.equal fswsList.Length jswsList.Length "Both no worksheet"
Expect.equal fswsList.Count jswsList.Length "Both no worksheet"
testCase "worksheet" <| fun _ ->
let jswb = ExcelJs.Excel.Workbook()
let _ = jswb.addWorksheet("My Awesome Worksheet")
Expand All @@ -31,8 +31,8 @@ let private tests_toFsWorkbook = testList "toFsWorkbook" [
Expect.passWithMsg "Convert to fswb"
let jswsList = jswb.worksheets
let fswsList = fswb.GetWorksheets()
Expect.equal fswsList.Length jswsList.Length "Both 1 worksheet"
Expect.equal fswsList.Head.Name "My Awesome Worksheet" "Correct worksheet name"
Expect.equal fswsList.Count jswsList.Length "Both 1 worksheet"
Expect.equal fswsList.[0].Name "My Awesome Worksheet" "Correct worksheet name"
testCase "worksheets" <| fun _ ->
let jswb = ExcelJs.Excel.Workbook()
let _ = jswb.addWorksheet("My Awesome Worksheet")
Expand All @@ -43,7 +43,7 @@ let private tests_toFsWorkbook = testList "toFsWorkbook" [
Expect.passWithMsg "Convert to fswb"
let jswsList = jswb.worksheets
let fswsList = fswb.GetWorksheets()
Expect.equal fswsList.Length jswsList.Length "Both 3 worksheets"
Expect.equal fswsList.Count jswsList.Length "Both 3 worksheets"
let testWs (index:int) =
let fsws = fswsList.Item index
let jsws = jswsList.[index]
Expand All @@ -59,7 +59,7 @@ let private tests_toFsWorkbook = testList "toFsWorkbook" [
Expect.passWithMsg "Create jswb"
let fswb = JsWorkbook.toFsWorkbook jswb
Expect.passWithMsg "Convert to fswb"
let fsTables_a = fswb.GetWorksheets().Head.Tables
let fsTables_a = fswb.GetWorksheets().[0].Tables
let fsTables_b = fswb.GetTables()
Expect.hasLength (jsws.getTables()) 1 "js table count"
Expect.hasLength fsTables_a 1 "fs table count (a)"
Expand Down Expand Up @@ -92,7 +92,7 @@ let private tests_toFsWorkbook = testList "toFsWorkbook" [
let fsTables = fswb.GetTables()
Expect.hasLength (jsws.getTables()) 1 "js table count"
Expect.hasLength fsTables 1 "fs table count "
let fsTable = fsTables.Head
let fsTable = fsTables.[0]
Expect.equal fsTable.Name "My Awesome Table" "table name"
Expect.isTrue (fsTable.ShowHeaderRow) "show header row"
Expect.equal (fsTable.RangeAddress.Range) ("B1:D3") "RangeAddress"
Expand Down Expand Up @@ -136,7 +136,7 @@ let tests_toJsWorkbook = testList "toJsWorkbook" [
Expect.passWithMsg "Convert to jswb"
let fswsList = fswb.GetWorksheets()
let jswsList = jswb.worksheets
Expect.equal fswsList.Length jswsList.Length "Both no worksheet"
Expect.equal fswsList.Count jswsList.Length "Both no worksheet"
testCase "worksheet" <| fun _ ->
let fswb = new FsWorkbook()
let _ = fswb.InitWorksheet("My Awesome Worksheet")
Expand All @@ -145,7 +145,7 @@ let tests_toJsWorkbook = testList "toJsWorkbook" [
Expect.passWithMsg "Convert to jswb"
let fswsList = fswb.GetWorksheets()
let jswsList = jswb.worksheets
Expect.equal fswsList.Length jswsList.Length "Both no worksheet"
Expect.equal fswsList.Count jswsList.Length "Both no worksheet"
Expect.hasLength jswsList 1 "worksheet count"
Expect.equal jswsList.[0].name "My Awesome Worksheet" "worksheet name"
testCase "worksheets" <| fun _ ->
Expand All @@ -158,7 +158,7 @@ let tests_toJsWorkbook = testList "toJsWorkbook" [
Expect.passWithMsg "Convert to jswb"
let fswsList = fswb.GetWorksheets()
let jswsList = jswb.worksheets
Expect.equal fswsList.Length jswsList.Length "Both no worksheet"
Expect.equal fswsList.Count jswsList.Length "Both no worksheet"
Expect.hasLength jswsList 3 "worksheet count"
Expect.equal jswsList.[0].name "My Awesome Worksheet" "1 worksheet name"
Expect.equal jswsList.[1].name "My cool Worksheet" "2 worksheet name"
Expand Down
8 changes: 4 additions & 4 deletions tests/FsSpreadsheet.Tests/FsWorkbook.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ let dummyWorkbook = new FsWorkbook()
let dummyWorksheet1 = FsWorksheet("dummyWorksheet1")
let dummyWorksheet2 = FsWorksheet("dummyWorksheet2")
let dummyWorksheetList = [dummyWorksheet1; dummyWorksheet2]
let dummyTables = [
let dummyTables = [|
FsTable("dummyTable1", FsRangeAddress("A1:B2"))
FsTable("dummyTable2", FsRangeAddress("C3:F5"))
]
|]
dummyWorkbook.AddWorksheet dummyWorksheet1 |> ignore
dummyWorkbook.AddWorksheet dummyWorksheet2 |> ignore
dummyWorksheet1.AddTable dummyTables[0] |> ignore
Expand All @@ -27,7 +27,7 @@ let main =
testList "GetWorksheets" [
testCase "empty" <| fun _ ->
let wb = new FsWorkbook()
Expect.equal 0 (wb.GetWorksheets().Length) "Should be empty"
Expect.equal 0 (wb.GetWorksheets().Count) "Should be empty"
]
testList "TryGetWorksheetByName" [
let testWorksheet = dummyWorkbook.TryGetWorksheetByName "dummyWorksheet1"
Expand All @@ -50,7 +50,7 @@ let main =
testCase "adds all FsWorksheets correctly" <| fun _ ->
let testWorkbook = new FsWorkbook()
testWorkbook.AddWorksheets [dummyWorksheet1; dummyWorksheet2] |> ignore
let testWorkbookWorksheetNames = testWorkbook.GetWorksheets() |> List.map (fun ws -> ws.Name)
let testWorkbookWorksheetNames = testWorkbook.GetWorksheets() |> Seq.map (fun ws -> ws.Name)
let dummyWorksheetNames = dummyWorksheetList |> List.map (fun ws -> ws.Name)
Expect.containsAll testWorkbookWorksheetNames dummyWorksheetNames "Does not contain all FsWorksheets"
]
Expand Down
2 changes: 1 addition & 1 deletion tests/JS/Main.js → tests/JS/Core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { equal } from 'assert';

describe('Mocha native', function () {
describe('FsSpreadsheet', function () {
describe('subtestlist', function () {
it('should return -1 when the value is not present', function () {
equal([1, 2, 3].indexOf(4), -1);
Expand Down
19 changes: 19 additions & 0 deletions tests/JS/Exceljs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { equal } from 'assert';
import { Xlsx } from './FsSpreadsheet.Exceljs/Xlsx.fs.js';
import { Excel } from './FsSpreadsheet.Exceljs/fable_modules/Fable.Exceljs.1.3.6/ExcelJs.fs.js';
import { toJsWorkbook, toFsWorkbook } from "./FsSpreadsheet.Exceljs/Workbook.fs.js";

describe('FsSpreadsheet.Exceljs', function () {
describe('read', function () {
it('table', async () => {
//const wb = await Xlsx.fromXlsxFile(path)
const path = "C:/Users/Kevin/source/repos/FsSpreadsheet/tests/JS/TestFiles/ReadTable.xlsx";
const wb = new Excel.Workbook();
await wb.xlsx.readFile(path)
const fswb = toFsWorkbook(wb)
console.log(fswb)
let worksheets = fswb.GetWorksheets()
equal(worksheets, 1)
});
});
});
Binary file added tests/JS/TestFiles/ReadTable.xlsx
Binary file not shown.

0 comments on commit 58b57ae

Please sign in to comment.