KickJava   Java API By Example, From Geeks To Geeks.

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


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.DecimalFormat JavaDoc;
23 import java.text.NumberFormat JavaDoc;
24
25 import common.Logger;
26
27 import jxl.NumberCell;
28 import jxl.CellType;
29 import jxl.NumberFormulaCell;
30 import jxl.biff.FormattingRecords;
31 import jxl.biff.FormulaData;
32 import jxl.biff.WorkbookMethods;
33 import jxl.biff.DoubleHelper;
34 import jxl.biff.formula.FormulaParser;
35 import jxl.biff.formula.ExternalSheet;
36 import jxl.biff.formula.FormulaException;
37
38 /**
39  * A formula's last calculated value
40  */

41 class NumberFormulaRecord extends CellValue
42   implements NumberCell, FormulaData, NumberFormulaCell
43 {
44   /**
45    * The logger
46    */

47   private static Logger logger = Logger.getLogger(NumberFormulaRecord.class);
48
49   /**
50    * The last calculated value of the formula
51    */

52   private double value;
53
54   /**
55    * The number format
56    */

57   private NumberFormat JavaDoc format;
58
59   /**
60    * The string format for the double value
61    */

62   private static final DecimalFormat JavaDoc defaultFormat =
63     new DecimalFormat JavaDoc("#.###");
64
65   /**
66    * The formula as an excel string
67    */

68   private String JavaDoc formulaString;
69
70   /**
71    * A handle to the class needed to access external sheets
72    */

73   private ExternalSheet externalSheet;
74
75   /**
76    * A handle to the name table
77    */

78   private WorkbookMethods nameTable;
79
80   /**
81    * The raw data
82    */

83   private byte[] data;
84
85   /**
86    * Constructs this object from the raw data
87    *
88    * @param t the raw data
89    * @param fr the formatting record
90    * @param es the external sheet
91    * @param nt the name table
92    * @param si the sheet
93    */

94   public NumberFormulaRecord(Record t, FormattingRecords fr,
95                              ExternalSheet es, WorkbookMethods nt,
96                              SheetImpl si)
97   {
98     super(t, fr, si);
99
100     externalSheet = es;
101     nameTable = nt;
102     data = getRecord().getData();
103
104     format = fr.getNumberFormat(getXFIndex());
105
106     if (format == null)
107     {
108       format = defaultFormat;
109     }
110
111     value = DoubleHelper.getIEEEDouble(data, 6);
112   }
113
114   /**
115    * Interface method which returns the value
116    *
117    * @return the last calculated value of the formula
118    */

119   public double getValue()
120   {
121     return value;
122   }
123
124   /**
125    * Returns the numerical value as a string
126    *
127    * @return The numerical value of the formula as a string
128    */

129   public String JavaDoc getContents()
130   {
131     return format.format(value);
132   }
133
134   /**
135    * Returns the cell type
136    *
137    * @return The cell type
138    */

139   public CellType getType()
140   {
141     return CellType.NUMBER_FORMULA;
142   }
143
144   /**
145    * Gets the raw bytes for the formula. This will include the
146    * parsed tokens array. Used when copying spreadsheets
147    *
148    * @return the raw record data
149    */

150   public byte[] getFormulaData() throws FormulaException
151   {
152     if (!getSheet().getWorkbookBof().isBiff8())
153     {
154       throw new FormulaException(FormulaException.biff8Supported);
155     }
156
157     // Lop off the standard information
158
byte[] d = new byte[data.length - 6];
159     System.arraycopy(data, 6, d, 0, data.length - 6);
160
161     return d;
162   }
163
164   /**
165    * Gets the formula as an excel string
166    *
167    * @return the formula as an excel string
168    * @exception FormulaException
169    */

170   public String JavaDoc getFormula() throws FormulaException
171   {
172     if (formulaString == null)
173     {
174       byte[] tokens = new byte[data.length - 22];
175       System.arraycopy(data, 22, tokens, 0, tokens.length);
176       FormulaParser fp = new FormulaParser
177         (tokens, this, externalSheet, nameTable,
178          getSheet().getWorkbook().getSettings());
179       fp.parse();
180       formulaString = fp.getFormula();
181     }
182
183     return formulaString;
184   }
185
186   /**
187    * Gets the NumberFormat used to format this cell. This is the java
188    * equivalent of the Excel format
189    *
190    * @return the NumberFormat used to format the cell
191    */

192   public NumberFormat JavaDoc getNumberFormat()
193   {
194     return format;
195   }
196 }
197
Popular Tags