1 19 20 package jxl.write.biff; 21 22 import common.Assert; 23 import common.Logger; 24 25 import jxl.CellType; 26 import jxl.Sheet; 27 import jxl.FormulaCell; 28 import jxl.WorkbookSettings; 29 import jxl.CellReferenceHelper; 30 import jxl.write.WritableCell; 31 import jxl.format.CellFormat; 32 import jxl.biff.Type; 33 import jxl.biff.IntegerHelper; 34 import jxl.biff.FormattingRecords; 35 import jxl.biff.WorkbookMethods; 36 import jxl.biff.formula.FormulaException; 37 import jxl.biff.formula.ExternalSheet; 38 import jxl.biff.FormulaData; 39 import jxl.biff.FormattingRecords; 40 import jxl.biff.formula.FormulaParser; 41 import jxl.biff.formula.FormulaException; 42 43 47 public class FormulaRecord extends CellValue implements FormulaData 48 { 49 52 private static Logger logger = Logger.getLogger(FormulaRecord.class); 53 54 57 private String formulaToParse; 58 59 62 private FormulaParser parser; 63 64 67 private String formulaString; 68 69 72 private byte[] formulaBytes; 73 74 78 private CellValue copiedFrom; 79 80 85 public FormulaRecord(int c, int r, String f) 86 { 87 super(Type.FORMULA2, c, r); 88 formulaToParse = f; 89 copiedFrom = null; 90 } 91 92 97 public FormulaRecord(int c, int r, String f, CellFormat st) 98 { 99 super(Type.FORMULA, c, r, st); 100 formulaToParse = f; 101 copiedFrom = null; 102 } 103 104 111 protected FormulaRecord(int c, int r, FormulaRecord fr) 112 { 113 super(Type.FORMULA, c, r, fr); 114 copiedFrom = fr; 115 formulaBytes = new byte[fr.formulaBytes.length]; 116 System.arraycopy(fr.formulaBytes, 0, formulaBytes, 0, formulaBytes.length); 117 } 118 119 126 protected FormulaRecord(int c, int r, ReadFormulaRecord rfr) 127 { 128 super(Type.FORMULA, c, r, rfr); 129 try 130 { 131 copiedFrom = rfr; 132 byte[] readFormulaData = rfr.getFormulaData(); 133 formulaBytes = new byte[readFormulaData.length - 16]; 134 System.arraycopy(readFormulaData, 16, formulaBytes, 0, 135 formulaBytes.length); 136 } 137 catch (FormulaException e) 138 { 139 logger.error("", e); 141 } 142 } 143 144 153 private void initialize(WorkbookSettings ws, ExternalSheet es, 154 WorkbookMethods nt) 155 { 156 if (copiedFrom != null) 157 { 158 initializeCopiedFormula(ws, es, nt); 159 return; 160 } 161 162 parser = new FormulaParser(formulaToParse, es, nt, ws); 163 164 try 165 { 166 parser.parse(); 167 formulaString = parser.getFormula(); 168 formulaBytes = parser.getBytes(); 169 } 170 catch (FormulaException e) 171 { 172 logger.warn 173 (e.getMessage() + 174 " when parsing formula " + formulaToParse + " in cell " + 175 getSheet().getName() + "!" + 176 CellReferenceHelper.getCellReference(getColumn(), getRow())); 177 178 try 179 { 180 formulaToParse = "ERROR(1)"; 182 parser = new FormulaParser(formulaToParse, es, nt, ws); 183 parser.parse(); 184 formulaString = parser.getFormula(); 185 formulaBytes = parser.getBytes(); 186 } 187 catch (FormulaException e2) 188 { 189 logger.error("",e2); 191 } 192 } 193 } 194 195 203 private void initializeCopiedFormula(WorkbookSettings ws, 204 ExternalSheet es, WorkbookMethods nt) 205 { 206 try 207 { 208 parser = new FormulaParser(formulaBytes, this, es, nt, ws); 209 parser.parse(); 210 parser.adjustRelativeCellReferences 211 (getColumn() - copiedFrom.getColumn(), 212 getRow() - copiedFrom.getRow()); 213 formulaString = parser.getFormula(); 214 formulaBytes = parser.getBytes(); 215 } 216 catch (FormulaException e) 217 { 218 try 219 { 220 formulaToParse = "ERROR(1)"; 222 parser = new FormulaParser(formulaToParse, es, nt, ws); 223 parser.parse(); 224 formulaString = parser.getFormula(); 225 formulaBytes = parser.getBytes(); 226 227 } 228 catch (FormulaException e2) 229 { 230 logger.error("", e2); 232 } 233 } 234 } 235 236 245 void setCellDetails(FormattingRecords fr, SharedStrings ss, 246 WritableSheetImpl s) 247 { 248 super.setCellDetails(fr, ss, s); 249 initialize(s.getWorkbookSettings(), s.getWorkbook(), s.getWorkbook()); 250 s.getWorkbook().addRCIRCell(this); 251 } 252 253 258 public byte[] getData() 259 { 260 byte[] celldata = super.getData(); 261 byte[] formulaData = getFormulaData(); 262 byte[] data = new byte[formulaData.length + celldata.length]; 263 System.arraycopy(celldata, 0, data, 0, celldata.length); 264 System.arraycopy(formulaData, 0, data, celldata.length, 265 formulaData.length); 266 return data; 267 } 268 269 274 public CellType getType() 275 { 276 return CellType.ERROR; 277 } 278 279 286 public String getContents() 287 { 288 return formulaString; 289 } 290 291 297 public byte[] getFormulaData() 298 { 299 byte[] data = new byte[formulaBytes.length + 16]; 300 System.arraycopy(formulaBytes, 0, data, 16, formulaBytes.length); 301 302 data[6] = (byte) 0x10; 303 data[7] = (byte) 0x40; 304 data[12] = (byte) 0xe0; 305 data[13] = (byte) 0xfc; 306 data[8] |= 0x02; 308 309 IntegerHelper.getTwoBytes(formulaBytes.length, data, 14); 311 312 return data; 313 } 314 315 323 public WritableCell copyTo(int col, int row) 324 { 325 Assert.verify(false); 326 return null; 327 } 328 329 337 void columnInserted(Sheet s, int sheetIndex, int col) 338 { 339 parser.columnInserted(sheetIndex, col, s == getSheet()); 340 formulaBytes = parser.getBytes(); 341 } 342 343 351 void columnRemoved(Sheet s, int sheetIndex, int col) 352 { 353 parser.columnRemoved(sheetIndex, col, s == getSheet()); 354 formulaBytes = parser.getBytes(); 355 } 356 357 365 void rowInserted(Sheet s, int sheetIndex, int row) 366 { 367 parser.rowInserted(sheetIndex, row, s == getSheet()); 368 formulaBytes = parser.getBytes(); 369 } 370 371 379 void rowRemoved(Sheet s, int sheetIndex, int row) 380 { 381 parser.rowRemoved(sheetIndex, row, s == getSheet()); 382 formulaBytes = parser.getBytes(); 383 } 384 } 385 | Popular Tags |