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
18.01k stars 1.71k forks source link

getCellBgColor error #526

Closed EP-Toushirou closed 4 years ago

EP-Toushirou commented 4 years ago

Description hi, i'm finding a way to get the cell color, but there's a problem:

https://xuri.me/excelize/zh-hans/utils.html#ThemeColor ( I changed the origin code "C1" to "A1" when i test the code and corrected first sentence to styleID, _ := f.GetCellStyle(sheet, axix) )

I create a new excel and set "A1" black color and then run the code,it caused an error.

error: panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x0 pc=0x510bc0]

then I set "A1“ yellow, it printed FFFFFF00 , but yellow is #FFFF00.

please provide a correct example, thank you.

Output of go version: go version go1.13.4 windows/amd64


**Excelize version or commit ID:**
neweset master

Environment details (OS, Microsoft Excel™ version, physical, etc.): win10、office2019

xuri commented 4 years ago

Hi @GZLHZ, please try to get cell background color like this:

package main

import (
    "fmt"
    "strings"

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

func main() {
    f, err := excelize.OpenFile("Book1.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(getCellBgColor(f, "Sheet1", "A1"))
}

func getCellBgColor(f *excelize.File, sheet, axix string) string {
    styleID, err := f.GetCellStyle(sheet, axix)
    if err != nil {
        return err.Error()
    }
    fillID := f.Styles.CellXfs.Xf[styleID].FillID
    fgColor := f.Styles.Fills.Fill[fillID].PatternFill.FgColor
    if fgColor.Theme != nil {
        children := f.Theme.ThemeElements.ClrScheme.Children
        if *fgColor.Theme < 4 {
            dklt := map[int]string{
                0: children[1].SysClr.LastClr,
                1: children[0].SysClr.LastClr,
                2: children[3].SrgbClr.Val,
                3: children[2].SrgbClr.Val,
            }
            return strings.TrimPrefix(excelize.ThemeColor(dklt[*fgColor.Theme], fgColor.Tint), "FF")
        }
        srgbClr := children[*fgColor.Theme].SrgbClr.Val
        return strings.TrimPrefix(excelize.ThemeColor(srgbClr, fgColor.Tint), "FF")
    }
    return strings.TrimPrefix(fgColor.RGB, "FF")
}
EP-Toushirou commented 4 years ago

Ok,It's work. Thank you very much and hope you can update the documentation.😄

wangzheng1234 commented 3 years ago

why not define dklt := map[int]string{ 0: children[0].SysClr.LastClr, 1: children[1].SysClr.LastClr, 2: children[2].SrgbClr.Val, 3: children[3].SrgbClr.Val, } fgColor.Theme define look like: color

is this method can apply to fg color or any other palce for color convert ?

go-shafaq commented 11 months ago

this code is not not working man I have same issue also plz help mr

xuri commented 11 months ago

Hi @go-shafaq, this library has added a new function GetStyle support to get style definitions since v2.8.0, please use the GetStyle function instead of getCellBgColor in this example.

go-shafaq commented 7 months ago

Hi @go-shafaq, this library has added a new function GetStyle support to get style definitions since v2.8.0, please use the GetStyle function instead of getCellBgColor in this example.

thank you