/** * Manufactures individual cell formulas out the whole shared formula * debacle * * @param fr the formatting records * @param nf flag indicating whether this uses the 1904 date system * @return an array of formulas to be added to the sheet */ public Cell[] getFormulas(FormattingRecords fr, bool nf) { Cell[] sfs = new Cell[formulas.Count + 1]; // This can happen if there are many identical formulas in the // sheet and excel has not sliced and diced them exclusively if (templateFormula == null) { //logger.warn("Shared formula template formula is null"); return new Cell[0]; } templateFormula.setTokens(tokens); NumberFormat templateNumberFormat = null; // See if the template formula evaluates to date if (templateFormula.getType() == CellType.NUMBER_FORMULA) { SharedNumberFormulaRecord snfr = (SharedNumberFormulaRecord) templateFormula; templateNumberFormat = snfr.getNumberFormat(); if (fr.isDate(templateFormula.getXFIndex())) { templateFormula = new SharedDateFormulaRecord(snfr,fr,nf,sheet, snfr.getFilePos()); templateFormula.setTokens(snfr.getTokens()); } } sfs[0] = templateFormula; BaseSharedFormulaRecord f = null; for (int i = 0; i < formulas.Count; i++) { f = (BaseSharedFormulaRecord)formulas[i]; // See if the formula evaluates to date if (f.getType() == CellType.NUMBER_FORMULA) { SharedNumberFormulaRecord snfr = (SharedNumberFormulaRecord)f; if (fr.isDate(f.getXFIndex())) { f = new SharedDateFormulaRecord(snfr,fr,nf,sheet, snfr.getFilePos()); } else { ;// snfr.setNumberFormat(templateNumberFormat); } } f.setTokens(tokens); sfs[i + 1] = f; } return sfs; }
/** * Adds this formula to the list of formulas, if it falls within * the bounds * * @param fr the formula record to test for membership of this group * @return TRUE if the formulas was added, FALSE otherwise */ public bool add(BaseSharedFormulaRecord fr) { bool added = false; int r = fr.getRow(); if (r >= firstRow && r <= lastRow) { int c = fr.getColumn(); if (c >= firstCol && c <= lastCol) { formulas.Add(fr); added = true; } } return added; }
/** * Reverts the shared formula passed in to an ordinary formula and adds * it to the list * * @param f the formula * @return the new formula * @exception FormulaException */ private Cell revertSharedFormula(BaseSharedFormulaRecord f) { // string formulas look for a STRING record soon after the formula // occurred. Temporarily the position in the excel file back // to the point immediately after the formula record int pos = excelFile.getPos(); excelFile.setPos(f.getFilePos()); FormulaRecord fr = new FormulaRecord(f.getRecord(), excelFile, formattingRecords, workbook, workbook, FormulaRecord.ignoreSharedFormula, sheet, workbookSettings); try { Cell cell = fr.getFormula(); // See if the formula evaluates to date if (fr.getFormula().getType() == CellType.NUMBER_FORMULA) { NumberFormulaRecord nfr = (NumberFormulaRecord)fr.getFormula(); if (formattingRecords.isDate(fr.getXFIndex())) { cell = new DateFormulaRecord(nfr, formattingRecords, workbook, workbook, nineteenFour, sheet); } } excelFile.setPos(pos); return cell; } catch (FormulaException e) { // Something has gone wrong trying to read the formula data eg. it // might be unsupported biff7 data //logger.warn(CellReferenceHelper.getCellReference(fr.getColumn(), fr.getRow()) + // " " + e.Message); return null; } }
/** * Constructs this object from the raw data. Creates either a * NumberFormulaRecord or a StringFormulaRecord depending on whether * this formula represents a numerical calculation or not * * @param t the raw data * @param fr the base shared formula * @param es the workbook, which contains the external sheet references * @param nt the workbook * @param si the sheet */ public SharedFormulaRecord(Record t,BaseSharedFormulaRecord fr, ExternalSheet es,WorkbookMethods nt, SheetImpl si) { sheet = si; byte[] data = t.getData(); firstRow = IntegerHelper.getInt(data[0],data[1]); lastRow = IntegerHelper.getInt(data[2],data[3]); firstCol = data[4] & 0xff; lastCol = data[5] & 0xff; formulas = new ArrayList(); templateFormula = fr; tokens = new byte[data.Length - 10]; System.Array.Copy(data,10,tokens,0,tokens.Length); }
/** * Sees if the shared formula belongs to any of the shared formula * groups * * @param fr the candidate shared formula * @return TRUE if the formula was added, FALSE otherwise */ private bool addToSharedFormulas(BaseSharedFormulaRecord fr) { bool added = false; SharedFormulaRecord sfr = null; for (int i = 0, size = sharedFormulas.Count; i < size && !added; ++i) { sfr = (SharedFormulaRecord)sharedFormulas[i]; added = sfr.add(fr); } return added; }