Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified way to set cell formulas #232

Open
adknudson opened this issue Jun 18, 2023 · 0 comments
Open

Simplified way to set cell formulas #232

adknudson opened this issue Jun 18, 2023 · 0 comments

Comments

@adknudson
Copy link

I'd like to propose a simplified way to setting a cell formula. I see that in #120 the proposed way to set a cell formula is to do

sheet["A1"] = ""
(XLSX.getcell(sheet, "A1")).formula = "=ROUND(3.14159, 2)"

I see that sheet["A1"] = "" is necessary because sheet["A1"] can return an EmptyCell which has no fields.

I also see that there is the AbstractFormula type (which isn't yet being used in the Cell type in v0.9.0). Is this going to be used in future versions? If so, I can imagine an interface that looks like this:

abstract type AbstractFormula end

formula(f::AbstractFormula) = f.formula

struct FormulaA1 <: AbstractFormula 
    formula::String
end

Base.convert(::Type{FormulaA1}, v::FormulaA1) = v

function Base.convert(::Type{FormulaR1C1}, v::FormulaA1)
    # convert A1 to R1C1
end

struct FormulaR1C1 <: AbstractFormula 
    formula::String
end

function Base.convert(::Type{FormulaA1}, v::FormulaR1C1)
    # convert R1C1 to A1
end

Base.convert(::Type{FormulaR1C1}, v::FormulaR1C1) = v


function setformula!(ws::XLSX.Worksheet, ref::XLSX.CellRef, v::AbstractFormula)
    f = formula(convert(::FormulaA1, v))

    cell = XLSX.getcell(ws, ref)
    
    if cell isa XLSX.EmptyCell
        cell = XLSX.Cell(ref, "", "", "", f)
        XLSX.setdata!(ws, cell)
    else
        cell.formula = f
    end
    
    return cell
end

I'm not yet too familiar with internals of this package, but with some direction I'd be willing to prototype this out and open a PR. Let me know what you think, and thanks for this package!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant