KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > read > biff > SharedFormulaRecord


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.read.biff;
21
22 import java.text.NumberFormat JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import common.Logger;
26
27 import jxl.Cell;
28 import jxl.CellType;
29 import jxl.biff.FormattingRecords;
30 import jxl.biff.IntegerHelper;
31 import jxl.biff.WorkbookMethods;
32 import jxl.biff.formula.ExternalSheet;
33
34 /**
35  * A shared formula
36  */

37 class SharedFormulaRecord
38 {
39   /**
40    * The logger
41    */

42   private static Logger logger = Logger.getLogger(SharedFormulaRecord.class);
43
44   /**
45    * The first row to which this shared formula applies
46    */

47   private int firstRow;
48
49   /**
50    * The last row to which this shared formula applies
51    */

52   private int lastRow;
53
54   /**
55    * The first column to which this shared formula applies
56    */

57   private int firstCol;
58
59   /**
60    * The last column to which this shared formula applies
61    */

62   private int lastCol;
63
64   /**
65    * The first (template) formula comprising this group
66    */

67   private BaseSharedFormulaRecord templateFormula;
68
69   /**
70    * The rest of the cells comprising this shared formula
71    */

72   private ArrayList JavaDoc formulas;
73
74   /**
75    * The token data
76    */

77   private byte[] tokens;
78
79   /**
80    * A handle to the external sheet
81    */

82   private ExternalSheet externalSheet;
83
84   /**
85    * A handle to the sheet
86    */

87   private SheetImpl sheet;
88
89
90   /**
91    * Constructs this object from the raw data. Creates either a
92    * NumberFormulaRecord or a StringFormulaRecord depending on whether
93    * this formula represents a numerical calculation or not
94    *
95    * @param t the raw data
96    * @param fr the base shared formula
97    * @param es the workbook, which contains the external sheet references
98    * @param nt the workbook
99    * @param si the sheet
100    */

101   public SharedFormulaRecord(Record t, BaseSharedFormulaRecord fr,
102                              ExternalSheet es, WorkbookMethods nt,
103                              SheetImpl si)
104   {
105     sheet = si;
106     byte[] data = t.getData();
107
108     firstRow = IntegerHelper.getInt(data[0], data[1]);
109     lastRow = IntegerHelper.getInt(data[2], data[3]);
110     firstCol = (int) (data[4] & 0xff);
111     lastCol = (int) (data[5] & 0xff);
112
113     formulas = new ArrayList JavaDoc();
114
115     templateFormula = fr;
116
117     tokens = new byte[data.length - 10];
118     System.arraycopy(data, 10, tokens, 0, tokens.length);
119   }
120
121   /**
122    * Adds this formula to the list of formulas, if it falls within
123    * the bounds
124    *
125    * @param fr the formula record to test for membership of this group
126    * @return TRUE if the formulas was added, FALSE otherwise
127    */

128   public boolean add(BaseSharedFormulaRecord fr)
129   {
130     if (fr.getRow() >= firstRow && fr.getRow() <= lastRow &&
131         fr.getColumn() >= firstCol && fr.getColumn() <= lastCol)
132     {
133       formulas.add(fr);
134       return true;
135     }
136
137     return false;
138   }
139
140   /**
141    * Manufactures individual cell formulas out the whole shared formula
142    * debacle
143    *
144    * @param fr the formatting records
145    * @param nf flag indicating whether this uses the 1904 date system
146    * @return an array of formulas to be added to the sheet
147    */

148   Cell[] getFormulas(FormattingRecords fr, boolean nf)
149   {
150     Cell[] sfs = new Cell[formulas.size() + 1];
151
152     // This can happen if there are many identical formulas in the
153
// sheet and excel has not sliced and diced them exclusively
154
if (templateFormula == null)
155     {
156       logger.warn("Shared formula template formula is null");
157       return new Cell[0];
158     }
159
160     templateFormula.setTokens(tokens);
161     NumberFormat JavaDoc templateNumberFormat = null;
162
163     // See if the template formula evaluates to date
164
if (templateFormula.getType() == CellType.NUMBER_FORMULA)
165     {
166       SharedNumberFormulaRecord snfr = (SharedNumberFormulaRecord)
167         templateFormula;
168       templateNumberFormat = snfr.getNumberFormat();
169
170       if (fr.isDate(templateFormula.getXFIndex()))
171       {
172         templateFormula = new SharedDateFormulaRecord(snfr, fr, nf, sheet,
173                                                       snfr.getFilePos());
174         templateFormula.setTokens(snfr.getTokens());
175       }
176     }
177
178     sfs[0] = templateFormula;
179
180     BaseSharedFormulaRecord f = null;
181
182     for (int i = 0; i < formulas.size(); i++)
183     {
184       f = (BaseSharedFormulaRecord) formulas.get(i);
185
186       // See if the formula evaluates to date
187
if (f.getType() == CellType.NUMBER_FORMULA)
188       {
189         SharedNumberFormulaRecord snfr = (SharedNumberFormulaRecord) f;
190
191         if (fr.isDate(f.getXFIndex()))
192         {
193           f = new SharedDateFormulaRecord(snfr, fr, nf, sheet,
194                                           snfr.getFilePos());
195         }
196         else
197         {
198           snfr.setNumberFormat(templateNumberFormat);
199         }
200       }
201
202       f.setTokens(tokens);
203       sfs[i + 1] = f;
204     }
205
206     return sfs;
207   }
208
209   /**
210    * Accessor for the template formula. Called when a shared formula has,
211    * for some reason, specified an inappropriate range and it is necessary
212    * to retrieve the template from a previously available shared formula
213    */

214   BaseSharedFormulaRecord getTemplateFormula()
215   {
216     return templateFormula;
217   }
218 }
219
220
221
222
223
224
225
226
Popular Tags