Excelize是一个用于操作Excel文件的Go语言库。

huangapple go评论67阅读模式
英文:

Excelize pkg.go Golang

问题

我正在使用excelize包来操作Excel文件。我在使用setcellformula函数时遇到了问题,它没有应用公式。我粘贴了一个基本示例,我只是尝试着使用它。

func main() {

    f := excelize.NewFile()

    f.SetCellValue("Sheet1", "A1", "ID")
    f.SetCellValue("Sheet1", "B1", "Nome")
    f.SetCellValue("Sheet1", "D1", "Cognome")
    f.SetCellValue("Sheet1", "C1", "Email")
    f.SetCellValue("Sheet1", "D1", "IDENTITY_CARD_EXPIRE_DATE")
    f.SetCellValue("Sheet1", "E1", "TOTAL")

    f.SetCellValue("Sheet1", "E2", "1")
    f.SetCellValue("Sheet1", "E3", "5")
    f.SetCellValue("Sheet1", "E4", "10")

    // 公式
    f.SetCellFormula("Sheet1", "E6", "=SUBTOTALE(9;E2:E8)")

    f.SetColWidth("Sheet1", "A", "D", 30)
    if err := f.SaveAs("Personal_Data.xlsx"); err != nil {
        log.Fatal(err)
    }
}

谢谢大家。

英文:

I'm using the excelize packaging to manipulate excel files. I am having a problem with the setcellformula func, it does not apply the formula. I paste a basic example, where I was just trying

func main() {

	f := excelize.NewFile()

	f.SetCellValue("Sheet1", "A1", "ID")
	f.SetCellValue("Sheet1", "B1", "Nome")
	f.SetCellValue("Sheet1", "D1", "Cognome")
	f.SetCellValue("Sheet1", "C1", "Email")
	f.SetCellValue("Sheet1", "D1", "IDENTITY_CARD_EXPIRE_DATE")
	f.SetCellValue("Sheet1", "E1", "TOTAL")

	f.SetCellValue("Sheet1", "E2", "1")
	f.SetCellValue("Sheet1", "E3", "5")
	f.SetCellValue("Sheet1", "E4", "10")

	//formula
	f.SetCellFormula("Sheet1", "E6", "=SUBTOTALE(9;E2:E8)")

	f.SetColWidth("Sheet1", "A", "D", 30)
	if err := f.SaveAs("Personal_Data.xlsx"); err != nil {
		log.Fatal(err)
	}
}

Thank you all

答案1

得分: 3

你的代码有三个问题:

首先,你将数字值作为字符串相加。你应该使用整数作为第三个参数:

f.SetCellValue("Sheet1", "E2", 1)
f.SetCellValue("Sheet1", "E3", 5)
f.SetCellValue("Sheet1", "E4", 10)

其次,在公式中,你不应该添加等号,而应该使用逗号代替分号,并且必须使用英文函数名:

f.SetCellFormula("Sheet1", "E6", "SUBTOTAL(9,E2:E4)")

此外,你的公式中存在循环引用的问题,因为它在E6单元格中,但你的示例范围是E2:E8,其中包含了E6。所以你还需要进行修改。

英文:

You have three problems with your code:

First, you add the numeric values as strings. You should use integers as the
third parameter:

f.SetCellValue("Sheet1", "E2", 1)
f.SetCellValue("Sheet1", "E3", 5)
f.SetCellValue("Sheet1", "E4", 10)

Second, in the formula, you shouldn't add equal sign, you must use comma instead of semicolon and you must use the english function name:

f.SetCellFormula("Sheet1", "E6", "SUBTOTAL(9,E2:E4)")

Furthermore you have a circular reference in your formula, because it is in E6 cell, but the range is E2:E8 in you example, which has E6 in it. So you have to change that too.

huangapple
  • 本文由 发表于 2022年6月9日 16:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/72557403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定