qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.64k stars 1.69k forks source link

Parentheses in formulas get cut out when inserting new columns or rows #1861

Closed losdmi closed 3 months ago

losdmi commented 3 months ago

Description

When adding columns or rows and formulas need to be adjusted, the inner parentheses got cut out therefore resulting in corrupted formula

Steps to reproduce the issue: Heres example programm

package main

import (
    "fmt"

    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    // Create a new sheet.
    index, err := f.NewSheet("Sheet2")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Set value of a cell.
    f.SetCellFormula("Sheet2", "A2", "(1-0.5)/2")
    f.InsertCols("Sheet2", "A", 1)
    // Set active sheet of the workbook.
    f.SetActiveSheet(index)
    // Save spreadsheet by the given path.
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

Describe the results you received: =1-0,5/2 Describe the results you expected: =(1-0,5)/2 Output of go version:

go version go1.21.3 darwin/arm64

Excelize version or commit ID:

2.8.1

Environment details (OS, Microsoft Excel™ version, physical, etc.): MacOS

xuri commented 3 months ago

Thanks for your issue. This bug has been fixed. Please upgrade to the master branch code by go get -u github.com/xuri/excelize/v2@master, and this patch will be released in the next version.