色值计算
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, cell string) string {
styleID, err := f.GetCellStyle(sheet, cell)
if err != nil {
return err.Error()
}
fillID := *f.Styles.CellXfs.Xf[styleID].FillID
fgColor := f.Styles.Fills.Fill[fillID].PatternFill.FgColor
if fgColor != nil && f.Theme != nil {
if clrScheme := f.Theme.ThemeElements.ClrScheme; fgColor.Theme != nil {
if val, ok := map[int]*string{
0: &clrScheme.Lt1.SysClr.LastClr,
1: &clrScheme.Dk1.SysClr.LastClr,
2: clrScheme.Lt2.SrgbClr.Val,
3: clrScheme.Dk2.SrgbClr.Val,
4: clrScheme.Accent1.SrgbClr.Val,
5: clrScheme.Accent2.SrgbClr.Val,
6: clrScheme.Accent3.SrgbClr.Val,
7: clrScheme.Accent4.SrgbClr.Val,
8: clrScheme.Accent5.SrgbClr.Val,
9: clrScheme.Accent6.SrgbClr.Val,
}[*fgColor.Theme]; ok && val != nil {
return strings.TrimPrefix(excelize.ThemeColor(*val, fgColor.Tint), "FF")
}
}
return strings.TrimPrefix(fgColor.RGB, "FF")
}
return "FFFFFF"
}