Closed EP-Toushirou closed 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")
}
Ok,It's work. Thank you very much and hope you can update the documentation.😄
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:
is this method can apply to fg color or any other palce for color convert ?
this code is not not working man I have same issue also plz help mr
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/amd64Environment details (OS, Microsoft Excel™ version, physical, etc.): win10、office2019