MCPcopy
hub / github.com/qax-os/excelize / SetConditionalFormat

Method SetConditionalFormat

styles.go:2813–2878  ·  view source on GitHub ↗

SetConditionalFormat provides a function to create conditional formatting rule for cell value. Conditional formatting is a feature of Excel which allows you to apply a format to a cell or a range of cells based on certain criteria. The type option is a required parameter and it has no default value

(sheet, rangeRef string, opts []ConditionalFormatOptions)

Source from the content-addressed store, hash-verified

2811// cells. When this parameter is set then subsequent rules are not evaluated
2812// if the current rule is true.
2813func (f *File) SetConditionalFormat(sheet, rangeRef string, opts []ConditionalFormatOptions) error {
2814 ws, err := f.workSheetReader(sheet)
2815 if err != nil {
2816 return err
2817 }
2818 SQRef, mastCell, err := prepareConditionalFormatRange(rangeRef)
2819 if err != nil {
2820 return err
2821 }
2822 // Create a pseudo GUID for each unique rule.
2823 var rules int
2824 for _, cf := range ws.ConditionalFormatting {
2825 rules += len(cf.CfRule)
2826 }
2827 var (
2828 cfRule []*xlsxCfRule
2829 noCriteriaTypes = []string{
2830 "containsBlanks",
2831 "notContainsBlanks",
2832 "containsErrors",
2833 "notContainsErrors",
2834 "expression",
2835 "iconSet",
2836 }
2837 )
2838 for i, opt := range opts {
2839 var vt, ct string
2840 var ok bool
2841 // "type" is a required parameter, check for valid validation types.
2842 vt, ok = validType[opt.Type]
2843 if ok {
2844 // Check for valid criteria types.
2845 ct, ok = criteriaType[opt.Criteria]
2846 if ok || inStrSlice(noCriteriaTypes, vt, true) != -1 {
2847 drawFunc, ok := drawContFmtFunc[vt]
2848 if ok {
2849 priority := rules + i
2850 rule, x14rule := drawFunc(priority, ct, mastCell,
2851 fmt.Sprintf("{00000000-0000-0000-%04X-%012X}", f.getSheetID(sheet), priority), &opt)
2852 if rule == nil && x14rule == nil {
2853 return ErrParameterInvalid
2854 }
2855 if rule != nil {
2856 cfRule = append(cfRule, rule)
2857 }
2858 if x14rule != nil {
2859 if err = f.appendCfRule(ws, x14rule, SQRef); err != nil {
2860 return err
2861 }
2862 f.addSheetNameSpace(sheet, NameSpaceSpreadSheetX14)
2863 }
2864 continue
2865 }
2866 }
2867 return ErrParameterInvalid
2868 }
2869 return ErrParameterInvalid
2870 }

Callers 6

TestSetConditionalFormatFunction · 0.95
TestDuplicateRowFunction · 0.95
TestConditionalFormatFunction · 0.95

Calls 6

workSheetReaderMethod · 0.95
getSheetIDMethod · 0.95
appendCfRuleMethod · 0.95
addSheetNameSpaceMethod · 0.95
inStrSliceFunction · 0.85

Tested by 6

TestSetConditionalFormatFunction · 0.76
TestDuplicateRowFunction · 0.76
TestConditionalFormatFunction · 0.76