zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* ====================================================================
   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 System.IO;
    using System.Collections;
    using System.Text;
 
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.Util;
    using HH.WMS.Utils.NPOI.HSSF.Model;
    using HH.WMS.Utils.NPOI.HSSF.Util;
    using HH.WMS.Utils.NPOI.SS.Formula;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
 
 
    /// <summary>
    /// High Level Represantion of Named Range
    /// </summary>
    /// <remarks>@author Libin Roman (Vista Portal LDT. Developer)</remarks>
    public class HSSFName:HH.WMS.Utils.NPOI.SS.UserModel.IName
    {
        private HSSFWorkbook book;
        private NameRecord _definedNameRec;
        private NameCommentRecord _commentRec;
        /* package */
        internal HSSFName(HSSFWorkbook book, NameRecord name):this(book, name, null)
        {
            
        }
        /// <summary>
        /// Creates new HSSFName   - called by HSSFWorkbook to Create a sheet from
        /// scratch.
        /// </summary>
        /// <param name="book">lowlevel Workbook object associated with the sheet.</param>
        /// <param name="name">the Name Record</param>
        /// <param name="comment"></param>
        internal HSSFName(HSSFWorkbook book, NameRecord name, NameCommentRecord comment)
        {
            this.book = book;
            this._definedNameRec = name;
            _commentRec = comment;
        }
 
        /// <summary>
        /// Gets or sets the sheets name which this named range is referenced to
        /// </summary>
        /// <value>sheet name, which this named range refered to</value>
        public String SheetName
        {
            get
            {
                String result;
                int indexToExternSheet = _definedNameRec.ExternSheetNumber;
 
                result = book.Workbook.FindSheetNameFromExternSheet(indexToExternSheet);
 
                return result;
            }
            //set
            //{
            //    int sheetNumber = book.GetSheetIndex(value);
 
            //    int externSheetNumber = book.GetExternalSheetIndex(sheetNumber);
            //    name.ExternSheetNumber = externSheetNumber;
            //}
        }
 
        /// <summary>
        /// Gets or sets the name of the named range
        /// </summary>
        /// <value>named range name</value>
        public String NameName
        {
            get
            {
                String result = _definedNameRec.NameText;
 
                return result;
            }
            set
            {
                ValidateName(value);
                _definedNameRec.NameText = value;
                InternalWorkbook wb = book.Workbook;
                int sheetNumber = _definedNameRec.SheetNumber;
 
                //Check to Ensure no other names have the same case-insensitive name
                for (int i = wb.NumNames- 1; i >= 0; i--)
                {
                    NameRecord rec = wb.GetNameRecord(i);
                    if (rec != _definedNameRec)
                    {
                        if (rec.NameText.Equals(NameName, StringComparison.OrdinalIgnoreCase) && sheetNumber == rec.SheetNumber)
                        {
                            String msg = "The " + (sheetNumber == 0 ? "workbook" : "sheet") + " already contains this name: " + value;
                            _definedNameRec.NameText = (value + "(2)");
                            throw new ArgumentException(msg);
                        }
                    }
                }
                // Update our comment, if there is one
                if (_commentRec != null)
                {
                    String oldName = _commentRec.NameText;
                    _commentRec.NameText=value;
                    book.Workbook.UpdateNameCommentRecordCache(_commentRec);
                }
            }
        }
        private void ValidateName(String name)
        {
            if (name.Length == 0) throw new ArgumentException("Name cannot be blank");
 
            char c = name[0];
            if (!(c == '_' || Char.IsLetter(c)) || name.IndexOf(' ') != -1)
            {
                throw new ArgumentException("Invalid name: '" + name + "'; Names must begin with a letter or underscore and not contain spaces");
            }
        }
 
        public String RefersToFormula
        {
            get
            {
                if (_definedNameRec.IsFunctionName)
                {
                    throw new InvalidOperationException("Only applicable to named ranges");
                }
                Ptg[] ptgs = _definedNameRec.NameDefinition;
                if (ptgs.Length < 1)
                {
                    // 'refersToFormula' has not been set yet
                    return null;
                }
                return HSSFFormulaParser.ToFormulaString(book, ptgs);
            }
            set
            {
                Ptg[] ptgs = HSSFFormulaParser.Parse(value, book, HH.WMS.Utils.NPOI.SS.Formula.FormulaType.NAMEDRANGE, SheetIndex);
                _definedNameRec.NameDefinition = ptgs;
            }
        }
        /**
         * Returns the sheet index this name applies to.
         *
         * @return the sheet index this name applies to, -1 if this name applies to the entire workbook
         */
        public int SheetIndex
        {
            get
            {
                return _definedNameRec.SheetNumber - 1;
            }
            set 
            {
                int lastSheetIx = book.NumberOfSheets - 1;
                if (value < -1 || value > lastSheetIx)
                {
                    throw new ArgumentException("Sheet index (" + value + ") is out of range" +
                            (lastSheetIx == -1 ? "" : (" (0.." + lastSheetIx + ")")));
                }
 
                _definedNameRec.SheetNumber = (value + 1);
            }
        }
        public string Comment
        {
            get 
            {
                if (_commentRec != null)
                {
                    // Prefer the comment record if it has text in it
                    if (_commentRec.CommentText != null &&
                          _commentRec.CommentText.Length > 0)
                    {
                        return _commentRec.CommentText;
                    }
                }
                return _definedNameRec.DescriptionText; 
            }
            set { _definedNameRec.DescriptionText = value; }
        }
 
        /// <summary>
        /// Tests if this name points to a cell that no longer exists
        /// </summary>
        /// <value>
        ///     <c>true</c> if the name refers to a deleted cell; otherwise, <c>false</c>.
        /// </value>
        public bool IsDeleted
        {
            get
            {
                Ptg[] ptgs = _definedNameRec.NameDefinition;
                return Ptg.DoesFormulaReferToDeletedCell(ptgs);
            }
        }
 
        /// <summary>
        /// Gets a value indicating whether this instance is function name.
        /// </summary>
        /// <value>
        ///     <c>true</c> if this instance is function name; otherwise, <c>false</c>.
        /// </value>
        public bool IsFunctionName
        {
            get
            {
                return _definedNameRec.IsFunctionName;
            }
        }
        /**
     * Indicates that the defined name refers to a user-defined function.
     * This attribute is used when there is an add-in or other code project associated with the file.
     *
     * @param value <c>true</c> indicates the name refers to a function.
     */
        public void SetFunction(bool value)
        {
            _definedNameRec.SetFunction(value);
        }
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder(64);
            sb.Append(GetType().Name).Append(" [");
            sb.Append(_definedNameRec.NameText);
            sb.Append("]");
            return sb.ToString();
        }
    }
}