/** * @param firstCell as extracted from the {@link ExpPtg} from the cell's formula. * @return never <code>null</code> */ public SharedFormulaRecord LinkSharedFormulaRecord(CellReference firstCell, FormulaRecordAggregate agg) { SharedFormulaGroup result = FindFormulaGroup(GetGroups(), firstCell); result.Add(agg); return(result.SFR); }
/** * Gets the {@link SharedValueRecordBase} record if it should be encoded immediately after the * formula record Contained in the specified {@link FormulaRecordAggregate} agg. Note - the * shared value record always appears after the first formula record in the group. For arrays * and tables the first formula is always the in the top left cell. However, since shared * formula groups can be sparse and/or overlap, the first formula may not actually be in the * top left cell. * * @return the SHRFMLA, TABLE or ARRAY record for the formula cell, if it is the first cell of * a table or array region. <code>null</code> if the formula cell is not shared/array/table, * or if the specified formula is not the the first in the group. */ public SharedValueRecordBase GetRecordForFirstCell(FormulaRecordAggregate agg) { CellReference firstCell = agg.FormulaRecord.Formula.ExpReference; // perhaps this could be optimised by consulting the (somewhat unreliable) isShared flag // and/or distinguishing between tExp and tTbl. if (firstCell == null) { // not a shared/array/table formula return(null); } int row = firstCell.Row; int column = firstCell.Col; if (agg.Row != row || agg.Column != column) { // not the first formula cell in the group return(null); } SharedFormulaGroup[] groups = GetGroups(); for (int i = 0; i < groups.Length; i++) { // note - logic for Finding correct shared formula group is slightly // more complicated since the first cell SharedFormulaGroup sfg = groups[i]; if (sfg.IsFirstCell(row, column)) { return(sfg.SFR); } } // Since arrays and tables cannot be sparse (all cells in range participate) // The first cell will be the top left in the range. So we can match the // ARRAY/TABLE record directly. for (int i = 0; i < _tableRecords.Count; i++) { TableRecord tr = _tableRecords[i]; if (tr.IsFirstCell(row, column)) { return(tr); } } foreach (ArrayRecord ar in _arrayRecords) { if (ar.IsFirstCell(row, column)) { return(ar); } } return(null); }
public void Add(FormulaRecordAggregate agg) { if (_numberOfFormulas == 0) { if (_firstCell.Row != agg.Row || _firstCell.Col != agg.Column) { throw new InvalidOperationException("shared formula coding error"); } } if (_numberOfFormulas >= _frAggs.Length) { throw new Exception("Too many formula records for shared formula group"); } _frAggs[_numberOfFormulas++] = agg; }