英文:
Bad formula when using excelize Golang
问题
当我使用这样的公式[file.SetCellFormula(sheetName, "A6", "=IF(C6<>\"\";IF(OR((F6-E6)>0;G6<>\"\";J6>0);\"O\";\"X\");\"\")")]
时,我遇到了一个错误。我确定公式是正确的,我是从一个正常工作的Excel文件中复制的。
func excel() error {
sheetName := "022022"
if IsFileExists("./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx") {
err := os.Remove("./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx")
if err != nil {
return err
}
}
file, err := excelize.OpenFile("./excels/Rikai_TimeSheet_202202_SonNguyen_template.xlsx")
if err != nil {
return err
}
defer func() {
_ = file.Close()
}()
err = file.SetCellFormula(sheetName, "A6", "=IF(C6<>\"\";IF(OR((F6-E6)>0;G6<>\"\";J6>0);\"O\";\"X\");\"\")")
if err != nil {
return err
}
f, err := os.OpenFile("./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return err
}
err = file.Write(f)
if err != nil {
return err
}
return nil
}
英文:
I get an error when using a formula like this [file.SetCellFormula(sheetName, "A6", "=IF(C6<>\"\";IF(OR((F6-E6)>0;G6<>\"\";J6>0);\"O\";\"X\");\"\")")]
, I'm sure the formula is correct, I have copied this formula from a working excel file
func excel() error {
sheetName := "022022"
if IsFileExists("./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx") {
err := os.Remove("./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx")
if err != nil {
return err
}
}
file, err := excelize.OpenFile("./excels/Rikai_TimeSheet_202202_SonNguyen_template.xlsx")
if err != nil {
return err
}
defer func() {
_ = file.Close()
}()
err = file.SetCellFormula(sheetName, "A6", "=IF(C6<>\"\";IF(OR((F6-E6)>0;G6<>\"\";J6>0);\"O\";\"X\");\"\")")
if err != nil {
return err
}
f, err := os.OpenFile("./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return err
}
err = file.Write(f)
if err != nil {
return err
}
return nil
}
答案1
得分: 1
尝试将那些分号(;)替换为逗号(,)。
(作为一个一般的建议,始终使用美国标准,而不是你们当地的标准)
英文:
Try replacing those semicolons (;) by commas (,).
(As a general idea, always use the US standards, not your local one)
答案2
得分: 0
请尝试使用反引号 ` 而不是转义引号:
err = file.SetCellFormula(sheetName, "A6", `=IF(C6<>"",IF(OR((F6-E6)>0,G6<>"",J6>0),"O","X"),"")`)
英文:
Try to use backticks ` instead of escaping quotes:
err = file.SetCellFormula(sheetName, "A6", `=IF(C6<>"";IF(OR((F6-E6)>0;G6<>"";J6>0);"O";"X");"")`)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论