FsExcel icon indicating copy to clipboard operation
FsExcel copied to clipboard

Proposal: support constructing worksheets with lists of lists

Open kojo12228 opened this issue 2 years ago • 4 comments

Since F# London, I've been giving FsExcel a bit of thought and came to the conclusion that it might be nicer to work with if it supported using a list of lists.

Thus:

Render.AsFile': path: string -> items: Item list list -> unit

To be consistent with the current API, an Item list would be a row of cells, and an Item list list would be the grid of cells (collection of rows).

So taking examples from the README:

Multiple Cells

[
    [
        for i in 1..10 ->
            Cell [ Integer i ]
    ]
]
|> Render.AsFile' (Path.Combine(savePath, "MultipleCells.xlsx"))

Vertical Movement

[
    for m in 1..12 do
        let monthName = CultureInfo.GetCultureInfoByIetfLanguageTag("en-GB").DateTimeFormat.GetMonthName(m)
        [ Cell [ String monthName ] ]
]
|> Render.AsFile' (Path.Combine(savePath, "VerticalMovement.xlsx"))
[
    for m in 1..12 do
        [
            let monthName = CultureInfo.GetCultureInfoByIetfLanguageTag("en-GB").DateTimeFormat.GetMonthName(m)
            Cell [ String monthName ]
            Cell [ Integer monthName.Length ]
        ]
]
|> Render.AsFile' (Path.Combine(savePath, "Rows.xlsx"))

Absolute Positioning

For this example, I'm going to make another proposal for EmptyCell as a type of Item, by Go should still work in this system, it's probably need to be separated into distinct GoRow and GoCol. Go (RC(_, _)) wouldn't easily translate:

[
    [
        for i in 1..6 do
            if i = 3
            then Cell [ String "Col 3"]
            else EmptyCell
    ]
    []
    []
    [
        for i in 1..6 do
            if i = 4
            then Cell [ String "Row 4"]
            else EmptyCell
    ]
    []
    [
        for i in 1..6 do
            if i = 5
            then Cell [ String "R6C5"]
            elif i = 6
            then Cell [ String "R6C6"]
            else EmptyCell
    ]
]
|> Render.AsFile' (Path.Combine(savePath, "AbsolutePositioning.xlsx"))

For better type safety, AsFile' could be Render.AsFile': path: string -> items: Item [,] -> unit, but Array2D is definitely not as nice to work with compared to list comprehension.

This is pretty big change, so thought best to create an issue first. I would be happy to create a PR for this, just for the sake of playing around with the library more than anything.

kojo12228 avatar Mar 27 '22 13:03 kojo12228

Oh this is very interesting. Let me have a think...

misterspeedy avatar Mar 27 '22 20:03 misterspeedy

Ok, @kojo12228, I've had a think about this and I love it! I've made a branch called ListOfLists.

Feel free to dive in straight away. Meanwhile I'll add a few 'getting started' tips in case you haven't worked it all out already.

Thanks for this.

misterspeedy avatar Mar 28 '22 07:03 misterspeedy

@kojo12228 I've now added a Contributing.md which outlines the setup and developer workflow:

https://github.com/misterspeedy/FsExcel/blob/main/Contributing.md

Honestly, I feel I've created a bit of a monster here so perhaps we should move away from having the tutorial generate the regression tests. Anyway, for now 'it is what it is'.

Hope you are able to try out that list-of-lists feature, it feels like it could be a big improvement.

Thankyou!

misterspeedy avatar Mar 28 '22 19:03 misterspeedy

Thanks @misterspeedy, I'm going to give it a try over the weekend hopefully.

kojo12228 avatar Mar 28 '22 19:03 kojo12228