在使用Excelize库的Golang时遇到了错误的公式。

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

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, &quot;A6&quot;, &quot;=IF(C6&lt;&gt;\&quot;\&quot;;IF(OR((F6-E6)&gt;0;G6&lt;&gt;\&quot;\&quot;;J6&gt;0);\&quot;O\&quot;;\&quot;X\&quot;);\&quot;\&quot;)&quot;)], I'm sure the formula is correct, I have copied this formula from a working excel file

func excel() error {
	sheetName := &quot;022022&quot;

	if IsFileExists(&quot;./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx&quot;) {
		err := os.Remove(&quot;./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx&quot;)
		if err != nil {
			return err
		}
	}

	file, err := excelize.OpenFile(&quot;./excels/Rikai_TimeSheet_202202_SonNguyen_template.xlsx&quot;)
	if err != nil {
		return err
	}
	defer func() {
		_ = file.Close()
	}()
	

	err = file.SetCellFormula(sheetName, &quot;A6&quot;, &quot;=IF(C6&lt;&gt;\&quot;\&quot;;IF(OR((F6-E6)&gt;0;G6&lt;&gt;\&quot;\&quot;;J6&gt;0);\&quot;O\&quot;;\&quot;X\&quot;);\&quot;\&quot;)&quot;)
	if err != nil {
		return err
	}

	f, err := os.OpenFile(&quot;./excels/Rikai_TimeSheet_202202_SonNguyen.xlsx&quot;, 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
}

Error popup when open the file

答案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, &quot;A6&quot;, `=IF(C6&lt;&gt;&quot;&quot;;IF(OR((F6-E6)&gt;0;G6&lt;&gt;&quot;&quot;;J6&gt;0);&quot;O&quot;;&quot;X&quot;);&quot;&quot;)`)

huangapple
  • 本文由 发表于 2022年2月4日 20:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/70986640.html
匿名

发表评论

匿名网友

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

确定