zhao
2021-07-02 23ee356c6f260ecc1a48bbb8bd60932b979e4698
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
/* ====================================================================
   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.Record
{
    using System;
    using System.Text;
    using HH.WMS.Utils.NPOI.SS.Formula;
    using System.Collections;
    using HH.WMS.Utils.NPOI.Util;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
    using HH.WMS.Utils.NPOI.SS.Util;
    using HH.WMS.Utils.NPOI.SS;
 
    /**
     * Title:        SharedFormulaRecord
     * Description:  Primarily used as an excel optimization so that multiple similar formulas
     *                   are not written out too many times.  We should recognize this record and
     *               Serialize as Is since this Is used when Reading templates.
     * 
     * Note: the documentation says that the SID Is BC where biffviewer reports 4BC.  The hex dump shows
     * that the two byte sid representation to be 'BC 04' that Is consistent with the other high byte
     * record types.
     * @author Danny Mui at apache dot org
     */
    public class SharedFormulaRecord : SharedValueRecordBase
    {
        public const short sid = 0x4BC;
 
 
        private int field_5_reserved;
        private HH.WMS.Utils.NPOI.SS.Formula.Formula field_7_parsed_expr;
 
        public SharedFormulaRecord()
            : this(new CellRangeAddress8Bit(0, 0, 0, 0))
        {
            //field_7_parsed_expr = HH.WMS.Utils.NPOI.SS.Formula.Formula.Create(Ptg.EMPTY_PTG_ARRAY);
        }
        private SharedFormulaRecord(CellRangeAddress8Bit range):
            base(range)
        {
            field_7_parsed_expr = Formula.Create(Ptg.EMPTY_PTG_ARRAY);
        }
        /**
         * @param in the RecordInputstream to Read the record from
         */
 
        public SharedFormulaRecord(RecordInputStream in1)
            : base(in1)
        {
            field_5_reserved = in1.ReadShort();
            int field_6_expression_len = in1.ReadShort();
            int nAvailableBytes = in1.Available();
            field_7_parsed_expr = HH.WMS.Utils.NPOI.SS.Formula.Formula.Read(field_6_expression_len, in1, nAvailableBytes);
        }
        protected override int ExtraDataSize
        {
            get
            {
                //Because this record is converted to individual Formula records, this method is not required.
                return 2 + field_7_parsed_expr.EncodedSize;
            }
 
        }
 
        /**
         * print a sort of string representation ([SHARED FORMULA RECORD] id = x [/SHARED FORMULA RECORD])
         */
 
        public override String ToString()
        {
            StringBuilder buffer = new StringBuilder();
 
            buffer.Append("[SHARED FORMULA (").Append(HexDump.IntToHex(sid)).Append("]\n");
            buffer.Append("    .range      = ").Append(Range.ToString()).Append("\n");
            buffer.Append("    .reserved    = ").Append(HexDump.ShortToHex(field_5_reserved)).Append("\n");
 
            Ptg[] ptgs = field_7_parsed_expr.Tokens;
            for (int k = 0; k < ptgs.Length; k++)
            {
                buffer.Append("Formula[").Append(k).Append("]");
                Ptg ptg = ptgs[k];
                buffer.Append(ptg.ToString()).Append(ptg.RVAType).Append("\n");
            }
 
            buffer.Append("[/SHARED FORMULA]\n");
            return buffer.ToString();
        }
 
        public override short Sid
        {
            get { return sid; }
        }
        public override Object Clone()
        {
            SharedFormulaRecord result = new SharedFormulaRecord(Range);
            result.field_5_reserved = field_5_reserved;
            result.field_7_parsed_expr = field_7_parsed_expr.Copy();
            return result;
        }
        
        protected override void SerializeExtraData(ILittleEndianOutput out1)
        {
            out1.WriteShort(field_5_reserved);
            field_7_parsed_expr.Serialize(out1);
        }
        /**
 * @return the equivalent {@link Ptg} array that the formula would have, were it not shared.
 */
        public Ptg[] GetFormulaTokens(FormulaRecord formula)
        {
            int formulaRow = formula.Row;
            int formulaColumn = formula.Column;
            //Sanity checks
            if (!IsInRange(formulaRow, formulaColumn))
            {
                throw new Exception("Shared Formula Conversion: Coding Error");
            }
            SharedFormula sf = new SharedFormula(SpreadsheetVersion.EXCEL97);
            return sf.ConvertSharedFormulas(field_7_parsed_expr.Tokens, formulaRow, formulaColumn);
            //return ConvertSharedFormulas(field_7_parsed_expr.Tokens, formulaRow, formulaColumn);
        }
 
        
        public bool IsFormulaSame(SharedFormulaRecord other)
        {
            return field_7_parsed_expr.IsSame(other.field_7_parsed_expr);
        }
    }
}