zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
using System;
using System.Collections.Generic;
using System.Text;
using HH.WMS.Utils.NPOI.Util;
using HH.WMS.Utils.NPOI.HSSF.Record;
 
using HH.WMS.Utils.NPOI.SS.Formula;
 
namespace HH.WMS.Utils.NPOI.SS.Util
{
    public class CellRangeAddress : CellRangeAddressBase
    {
        public static int ENCODED_SIZE = 8;
 
        public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
            : base(firstRow, lastRow, firstCol, lastCol)
        {
 
        }
 
        public CellRangeAddress(RecordInputStream in1)
            : base(ReadUShortAndCheck(in1), in1.ReadUShort(), in1.ReadUShort(), in1.ReadUShort())
        {
 
        }
 
        private static int ReadUShortAndCheck(RecordInputStream in1)
        {
            if (in1.Remaining < ENCODED_SIZE)
            {
                // Ran out of data
                throw new Exception("Ran out of data readin1g CellRangeAddress");
            }
            return in1.ReadUShort();
        }
        [Obsolete]
        public int Serialize(int offset, byte[] data)
        {
            LittleEndian.PutUShort(data, offset + 0, FirstRow);
            LittleEndian.PutUShort(data, offset + 2, LastRow);
            LittleEndian.PutUShort(data, offset + 4, FirstColumn);
            LittleEndian.PutUShort(data, offset + 6, LastColumn);
            return ENCODED_SIZE;
        }
        public void Serialize(ILittleEndianOutput out1)
        {
            out1.WriteShort(FirstRow);
            out1.WriteShort(LastRow);
            out1.WriteShort(FirstColumn);
            out1.WriteShort(LastColumn);
        }
        public String FormatAsString()
        {
            return FormatAsString(null, false);
        }
        /**
     * @return the text format of this range using specified sheet name.
     */
        public String FormatAsString(String sheetName, bool useAbsoluteAddress)
        {
            StringBuilder sb = new StringBuilder();
            if (sheetName != null)
            {
                sb.Append(SheetNameFormatter.Format(sheetName));
                sb.Append("!");
            }
            CellReference cellRefFrom = new CellReference(FirstRow, FirstColumn,
                    useAbsoluteAddress, useAbsoluteAddress);
            CellReference cellRefTo = new CellReference(LastRow, LastColumn,
                    useAbsoluteAddress, useAbsoluteAddress);
            sb.Append(cellRefFrom.FormatAsString());
 
            //for a single-cell reference return A1 instead of A1:A1
            if (!cellRefFrom.Equals(cellRefTo))
            {
                sb.Append(':');
                sb.Append(cellRefTo.FormatAsString());
            }
            return sb.ToString();
        }
        public CellRangeAddress Copy()
        {
            return new CellRangeAddress(FirstRow, LastRow, FirstColumn, LastColumn);
        }
 
        public static int GetEncodedSize(int numberOfItems)
        {
            return numberOfItems * ENCODED_SIZE;
        }
 
            /**
     * @param ref usually a standard area ref (e.g. "B1:D8").  May be a single cell
     *            ref (e.g. "B5") in which case the result is a 1 x 1 cell range.
     */
        public static CellRangeAddress ValueOf(String reference)
        {
            int sep = reference.IndexOf(":", StringComparison.Ordinal);
            CellReference a;
            CellReference b;
            if (sep == -1)
            {
                a = new CellReference(reference);
                b = a;
            }
            else
            {
                a = new CellReference(reference.Substring(0, sep));
                b = new CellReference(reference.Substring(sep + 1));
            }
            return new CellRangeAddress(a.Row, b.Row, a.Col, b.Col);
        }
    }
}