色值计算
func ThemeColor(baseColor string, tint float64) string
通过给定的 RGB 格式色值与色调参数,计算出最终颜色。例如,获取名为 Sheet1
的工作表 A1
单元格的背景颜色:
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"))
if err = f.Close(); err != nil {
fmt.Println(err)
}
}
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")
}