zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) Under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You Under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed Under the License is distributed on an "AS Is" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations Under the License.
==================================================================== */
 
namespace HH.WMS.Utils.NPOI.HSSF.UserModel
{
    using System;
 
    using HH.WMS.Utils.NPOI.HSSF.Util;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.HSSF.Model;
    using HH.WMS.Utils.NPOI.HSSF.Record.Aggregates;
    using HH.WMS.Utils.NPOI.SS.Util;
    using HH.WMS.Utils.NPOI.SS.UserModel;
 
 
    /// <summary>
    /// The Conditional Formatting facet of HSSFSheet
    /// @author Dmitriy Kumshayev
    /// </summary>
    public class HSSFSheetConditionalFormatting : ISheetConditionalFormatting
    {
 
        //private HSSFWorkbook _workbook;
        private HSSFSheet _sheet;
        private ConditionalFormattingTable _conditionalFormattingTable;
 
        /* package */
        //public HSSFSheetConditionalFormatting(HSSFWorkbook workbook, InternalSheet sheet)
        //{
        //    _workbook = workbook;
        //    _conditionalFormattingTable = sheet.ConditionalFormattingTable;
        //}
        public HSSFSheetConditionalFormatting(HSSFSheet sheet)
        {
            _sheet = sheet;
            _conditionalFormattingTable = sheet.Sheet.ConditionalFormattingTable;
        }
        /// <summary>
        /// A factory method allowing to Create a conditional formatting rule
        /// with a cell comparison operator
        /// TODO - formulas containing cell references are currently not Parsed properly
        /// </summary>
        /// <param name="comparisonOperation">a constant value from HSSFConditionalFormattingRule.ComparisonOperator</param>
        /// <param name="formula1">formula for the valued, Compared with the cell</param>
        /// <param name="formula2">second formula (only used with HSSFConditionalFormattingRule#COMPARISON_OPERATOR_BETWEEN 
        /// and HSSFConditionalFormattingRule#COMPARISON_OPERATOR_NOT_BETWEEN operations)</param>
        /// <returns></returns>
        public IConditionalFormattingRule CreateConditionalFormattingRule(
                ComparisonOperator comparisonOperation,
                String formula1,
                String formula2)
        {
 
            HSSFWorkbook wb = (HSSFWorkbook)_sheet.Workbook;
            CFRuleRecord rr = CFRuleRecord.Create(_sheet, (byte)comparisonOperation, formula1, formula2);
            return new HSSFConditionalFormattingRule(wb, rr);
        }
 
        /// <summary>
        /// A factory method allowing to Create a conditional formatting rule with a formula.
        /// The formatting rules are applied by Excel when the value of the formula not equal to 0.
        /// TODO - formulas containing cell references are currently not Parsed properly
        /// </summary>
        /// <param name="formula">formula for the valued, Compared with the cell</param>
        /// <returns></returns>
        public IConditionalFormattingRule CreateConditionalFormattingRule(String formula)
        {
            HSSFWorkbook wb = (HSSFWorkbook)_sheet.Workbook;
            CFRuleRecord rr = CFRuleRecord.Create(_sheet, formula);
            return new HSSFConditionalFormattingRule(wb, rr);
        }
        public IConditionalFormattingRule CreateConditionalFormattingRule(
            ComparisonOperator comparisonOperation,
            String formula1)
        {
            HSSFWorkbook wb = (HSSFWorkbook)_sheet.Workbook;
            CFRuleRecord rr = CFRuleRecord.Create(_sheet, (byte)comparisonOperation, formula1, null);
            return new HSSFConditionalFormattingRule(wb, rr);
        }
        /// <summary>
        /// Adds a copy of HSSFConditionalFormatting object to the sheet
        /// This method could be used to copy HSSFConditionalFormatting object
        /// from one sheet to another.
        /// </summary>
        /// <param name="cf">HSSFConditionalFormatting object</param>
        /// <returns>index of the new Conditional Formatting object</returns>
        /// <example>
        /// HSSFConditionalFormatting cf = sheet.GetConditionalFormattingAt(index);
        /// newSheet.AddConditionalFormatting(cf);
        /// </example>
        public int AddConditionalFormatting(IConditionalFormatting cf)
        {
            CFRecordsAggregate cfraClone = ((HSSFConditionalFormatting)cf).CFRecordsAggregate.CloneCFAggregate();
 
            return _conditionalFormattingTable.Add(cfraClone);
        }
 
        /// <summary>
        /// Allows to Add a new Conditional Formatting Set to the sheet.
        /// </summary>
        /// <param name="regions">list of rectangular regions to apply conditional formatting rules</param>
        /// <param name="cfRules">Set of up to three conditional formatting rules</param>
        /// <returns>index of the newly Created Conditional Formatting object</returns>
        public int AddConditionalFormatting(CellRangeAddress[] regions, IConditionalFormattingRule[] cfRules)
        {
            if (regions == null)
            {
                throw new ArgumentException("regions must not be null");
            }
            if (cfRules == null)
            {
                throw new ArgumentException("cfRules must not be null");
            }
            if (cfRules.Length == 0)
            {
                throw new ArgumentException("cfRules must not be empty");
            }
            if (cfRules.Length > 3)
            {
                throw new ArgumentException("Number of rules must not exceed 3");
            }
 
            CFRuleRecord[] rules = new CFRuleRecord[cfRules.Length];
            for (int i = 0; i != cfRules.Length; i++)
            {
                rules[i] = ((HSSFConditionalFormattingRule)cfRules[i]).CfRuleRecord;
            }
            CFRecordsAggregate cfra = new CFRecordsAggregate(regions, rules);
            return _conditionalFormattingTable.Add(cfra);
        }
        public int AddConditionalFormatting(CellRangeAddress[] regions,
            HSSFConditionalFormattingRule rule1)
        {
            return AddConditionalFormatting(regions,
                    rule1 == null ? null : new HSSFConditionalFormattingRule[]
                {
                    rule1
                });
        }
 
        /// <summary>
        /// Adds the conditional formatting.
        /// </summary>
        /// <param name="regions">The regions.</param>
        /// <param name="rule1">The rule1.</param>
        /// <returns></returns>
        public int AddConditionalFormatting(CellRangeAddress[] regions,
                IConditionalFormattingRule rule1)
        {
            return AddConditionalFormatting(regions, (HSSFConditionalFormattingRule)rule1);
        }
 
        /// <summary>
        /// Adds the conditional formatting.
        /// </summary>
        /// <param name="regions">The regions.</param>
        /// <param name="rule1">The rule1.</param>
        /// <param name="rule2">The rule2.</param>
        /// <returns></returns>
        public int AddConditionalFormatting(CellRangeAddress[] regions,
                IConditionalFormattingRule rule1,
                IConditionalFormattingRule rule2)
        {
            return AddConditionalFormatting(regions,
                    new HSSFConditionalFormattingRule[]
                {
                        (HSSFConditionalFormattingRule)rule1, (HSSFConditionalFormattingRule)rule2
                });
        }
 
        /// <summary>
        /// Gets Conditional Formatting object at a particular index
        /// @param index
        /// of the Conditional Formatting object to fetch
        /// </summary>
        /// <param name="index">Conditional Formatting object</param>
        /// <returns></returns>
        public IConditionalFormatting GetConditionalFormattingAt(int index)
        {
            CFRecordsAggregate cf = _conditionalFormattingTable.Get(index);
            if (cf == null)
            {
                return null;
            }
            return new HSSFConditionalFormatting((HSSFWorkbook)_sheet.Workbook, cf);
        }
 
        /// <summary>
        /// the number of Conditional Formatting objects of the sheet
        /// </summary>
        /// <value>The num conditional formattings.</value>
        public int NumConditionalFormattings
        {
            get
            {
                return _conditionalFormattingTable.Count;
            }
        }
 
        /// <summary>
        /// Removes a Conditional Formatting object by index
        /// </summary>
        /// <param name="index">index of a Conditional Formatting object to Remove</param>
        public void RemoveConditionalFormatting(int index)
        {
            _conditionalFormattingTable.Remove(index);
        }
    }
}