protected RefPtgBase(CellReference c) { Row = (c.Row); Column = (c.Col); IsColRelative = (!c.IsColAbsolute); IsRowRelative = (!c.IsRowAbsolute); }
private void buildWorkbook(IWorkbook wb) { ISheet sh = wb.CreateSheet(); IRow row1 = sh.CreateRow(0); IRow row2 = sh.CreateRow(1); row3 = sh.CreateRow(2); row1.CreateCell(0, CellType.Numeric); row1.CreateCell(1, CellType.Numeric); row2.CreateCell(0, CellType.Numeric); row2.CreateCell(1, CellType.Numeric); row3.CreateCell(0); row3.CreateCell(1); CellReference a1 = new CellReference("A1"); CellReference a2 = new CellReference("A2"); CellReference b1 = new CellReference("B1"); CellReference b2 = new CellReference("B2"); sh.GetRow(a1.Row).GetCell(a1.Col).SetCellValue(35); sh.GetRow(a2.Row).GetCell(a2.Col).SetCellValue(0); sh.GetRow(b1.Row).GetCell(b1.Col).CellFormula = (/*setter*/"A1/A2"); sh.GetRow(b2.Row).GetCell(b2.Col).CellFormula = (/*setter*/"NA()"); Evaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); }
public void TestEvaluateColumnGreaterThan255() { XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.OpenSampleWorkbook("50096.xlsx"); IFormulaEvaluator Evaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); /** * The first row simply Contains the numbers 1 - 300. * The second row simply refers to the cell value above in the first row by a simple formula. */ for (int i = 245; i < 265; i++) { ICell cell_noformula = wb.GetSheetAt(0).GetRow(0).GetCell(i); ICell cell_formula = wb.GetSheetAt(0).GetRow(1).GetCell(i); CellReference ref_noformula = new CellReference(cell_noformula.RowIndex, cell_noformula.ColumnIndex); CellReference ref_formula = new CellReference(cell_noformula.RowIndex, cell_noformula.ColumnIndex); String fmla = cell_formula.CellFormula; // assure that the formula refers to the cell above. // the check below is 'deep' and involves conversion of the shared formula: // in the sample file a shared formula in GN1 is spanned in the range GN2:IY2, Assert.AreEqual(ref_noformula.FormatAsString(), fmla); CellValue cv_noformula = Evaluator.Evaluate(cell_noformula); CellValue cv_formula = Evaluator.Evaluate(cell_formula); Assert.AreEqual(cv_noformula.NumberValue, cv_formula.NumberValue, "Wrong Evaluation result in " + ref_formula.FormatAsString()); } }
/** * @return the text format of this range using specified sheet name. */ public String FormatAsString(String sheetName, bool useAbsoluteAddress) { StringBuilder sb = new StringBuilder(); if (sheetName != null) { sb.Append(SheetNameFormatter.Format(sheetName)); sb.Append("!"); } CellReference cellRefFrom = new CellReference(FirstRow, FirstColumn, useAbsoluteAddress, useAbsoluteAddress); CellReference cellRefTo = new CellReference(LastRow, LastColumn, useAbsoluteAddress, useAbsoluteAddress); sb.Append(cellRefFrom.FormatAsString()); //for a single-cell reference return A1 instead of A1:A1 //for full-column ranges or full-row ranges return A:A instead of A, //and 1:1 instead of 1 if (!cellRefFrom.Equals(cellRefTo) || IsFullColumnRange || IsFullRowRange) { sb.Append(':'); sb.Append(cellRefTo.FormatAsString()); } return sb.ToString(); }
private static void Process(IRow row, HSSFFormulaEvaluator eval) { IEnumerator it = row.GetEnumerator(); while (it.MoveNext()) { ICell cell = (ICell)it.Current; if (cell.CellType != NPOI.SS.UserModel.CellType.FORMULA) { continue; } FormulaRecordAggregate record = (FormulaRecordAggregate)((HSSFCell)cell).CellValueRecord; FormulaRecord r = record.FormulaRecord; Ptg[] ptgs = r.ParsedExpression; String cellRef = new CellReference(row.RowNum, cell.ColumnIndex, false, false).FormatAsString(); #if !HIDE_UNREACHABLE_CODE if (false && cellRef.Equals("BP24")) { Console.Write(cellRef); Console.WriteLine(" - has " + ptgs.Length + " ptgs:"); for (int i = 0; i < ptgs.Length; i++) { String c = ptgs[i].GetType().ToString(); Console.WriteLine("\t" + c.Substring(c.LastIndexOf('.') + 1)); } Console.WriteLine("-> " + cell.CellFormula); } #endif NPOI.SS.UserModel.CellValue evalResult = eval.Evaluate(cell); Assert.IsNotNull(evalResult); } }
public void TestReadCalcSheet() { try { HSSFWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("XRefCalc.xls"); Assert.AreEqual("Sheet1!$A$2", wb.GetName("QUANT").RefersToFormula); Assert.AreEqual("Sheet1!$B$2", wb.GetName("PART").RefersToFormula); Assert.AreEqual("x123", wb.GetSheet("Sheet1").GetRow(1).GetCell(1).StringCellValue); Assert.AreEqual("Sheet1!$C$2", wb.GetName("UNITCOST").RefersToFormula); CellReference cellRef = new CellReference(wb.GetName("UNITCOST").RefersToFormula); ICell cell = wb.GetSheet(cellRef.SheetName).GetRow(cellRef.Row).GetCell((int)cellRef.Col); Assert.AreEqual("VLOOKUP(PART,COSTS,2,FALSE)", cell.CellFormula); Assert.AreEqual("Sheet1!$D$2", wb.GetName("COST").RefersToFormula); cellRef = new CellReference(wb.GetName("COST").RefersToFormula); cell = wb.GetSheet(cellRef.SheetName).GetRow(cellRef.Row).GetCell((int)cellRef.Col); Assert.AreEqual("UNITCOST*Quant", cell.CellFormula); Assert.AreEqual("Sheet1!$E$2", wb.GetName("TOTALCOST").RefersToFormula); cellRef = new CellReference(wb.GetName("TOTALCOST").RefersToFormula); cell = wb.GetSheet(cellRef.SheetName).GetRow(cellRef.Row).GetCell((int)cellRef.Col); Assert.AreEqual("Cost*Markup_Cost", cell.CellFormula); } catch (Exception e) { Assert.Fail(e.Message); } }
public void TestEvaluate() { HSSFWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("XRefCalc.xls"); HSSFWorkbook wb2 = HSSFTestDataSamples.OpenSampleWorkbook("XRefCalcData.xls"); CellReference cellRef = new CellReference(wb.GetName("QUANT").RefersToFormula); ICell cell = wb.GetSheet(cellRef.SheetName).GetRow(cellRef.Row).GetCell((int)cellRef.Col); cell.SetCellValue(NEW_QUANT); cell = wb2.GetSheet("CostSheet").GetRow(1).GetCell(1); cell.SetCellValue(NEW_PART_COST); HSSFFormulaEvaluator Evaluator = new HSSFFormulaEvaluator(wb); HSSFFormulaEvaluator EvaluatorCost = new HSSFFormulaEvaluator(wb2); String[] bookNames = { "XRefCalc.xls", "XRefCalcData.xls" }; HSSFFormulaEvaluator[] Evaluators = { Evaluator, EvaluatorCost, }; HSSFFormulaEvaluator.SetupEnvironment(bookNames, Evaluators); cellRef = new CellReference(wb.GetName("UNITCOST").RefersToFormula); ICell uccell = wb.GetSheet(cellRef.SheetName).GetRow(cellRef.Row).GetCell((int)cellRef.Col); cellRef = new CellReference(wb.GetName("COST").RefersToFormula); ICell ccell = wb.GetSheet(cellRef.SheetName).GetRow(cellRef.Row).GetCell((int)cellRef.Col); cellRef = new CellReference(wb.GetName("TOTALCOST").RefersToFormula); ICell tccell = wb.GetSheet(cellRef.SheetName).GetRow(cellRef.Row).GetCell((int)cellRef.Col); Evaluator.EvaluateFormulaCell(uccell); Evaluator.EvaluateFormulaCell(ccell); Evaluator.EvaluateFormulaCell(tccell); Assert.AreEqual(NEW_PART_COST, uccell.NumericCellValue); Assert.AreEqual(NEW_PART_COST * NEW_QUANT, ccell.NumericCellValue); Assert.AreEqual(NEW_PART_COST * NEW_QUANT * MARKUP_COST_2, tccell.NumericCellValue); }
/** * Takes in a String representation of a cell reference and Fills out the * numeric fields. */ protected RefPtgBase(String cellref) { CellReference c = new CellReference(cellref); Row = c.Row; Column = c.Col; IsColRelative = !c.IsColAbsolute; IsRowRelative = !c.IsRowAbsolute; }
public Ref3DPtg(String cellref, int externIdx) { CellReference c = new CellReference(cellref); Row=c.Row; Column=c.Col; IsColRelative=!c.IsColAbsolute; IsRowRelative=!c.IsRowAbsolute; ExternSheetIndex=externIdx; }
public String FormatAsString() { StringBuilder sb = new StringBuilder(); CellReference cellRefFrom = new CellReference(FirstRow, FirstColumn); CellReference cellRefTo = new CellReference(LastRow, LastColumn); sb.Append(cellRefFrom.FormatAsString()); sb.Append(':'); sb.Append(cellRefTo.FormatAsString()); return sb.ToString(); }
/** * Create an area ref from a string representation. Sheet names containing special Chars should be * delimited and escaped as per normal syntax rules for formulas.<br/> * The area reference must be contiguous (i.e. represent a single rectangle, not a Union of rectangles) */ public AreaReference(String reference) { if (!IsContiguous(reference)) { throw new ArgumentException( "References passed to the AreaReference must be contiguous, " + "use generateContiguous(ref) if you have non-contiguous references"); } String[] parts = SeparateAreaRefs(reference); String part0 = parts[0]; if (parts.Length == 1) { // TODO - probably shouldn't initialize area ref when text is really a cell ref // Need to fix some named range stuff to get rid of this _firstCell = new CellReference(part0); _lastCell = _firstCell; _isSingleCell = true; return; } if (parts.Length != 2) { throw new ArgumentException("Bad area ref '" + reference + "'"); } String part1 = parts[1]; if (IsPlainColumn(part0)) { if (!IsPlainColumn(part1)) { throw new Exception("Bad area ref '" + reference + "'"); } // Special handling for whole-column references // Represented internally as x$1 to x$65536 // which is the maximum range of rows bool firstIsAbs = CellReference.IsPartAbsolute(part0); bool lastIsAbs = CellReference.IsPartAbsolute(part1); int col0 = CellReference.ConvertColStringToIndex(part0); int col1 = CellReference.ConvertColStringToIndex(part1); _firstCell = new CellReference(0, col0, true, firstIsAbs); _lastCell = new CellReference(0xFFFF, col1, true, lastIsAbs); _isSingleCell = false; // TODO - whole row refs } else { _firstCell = new CellReference(part0); _lastCell = new CellReference(part1); _isSingleCell = part0.Equals(part1); } }
public override String ToString() { CellReference cr = new CellReference(Row, Column); StringBuilder sb = new StringBuilder(); sb.Append(GetType().Name).Append("["); sb.Append(_evaluator.SheetName); sb.Append('!'); sb.Append(cr.FormatAsString()); sb.Append("]"); return sb.ToString(); }
public override String ToString() { CellReference cr = new CellReference(Row, Column, !IsRowRelative, !IsColRelative); StringBuilder sb = new StringBuilder(); sb.Append(GetType().Name); sb.Append(" ["); sb.Append("sheetIx=").Append(ExternSheetIndex); sb.Append(" ! "); sb.Append(cr.FormatAsString()); sb.Append("]"); return sb.ToString(); }
public void TestSpecialSheetNames() { CellReference cf; cf = new CellReference("'profit + loss'!A1"); ConfirmCell(cf, "profit + loss", 0, 0, false, false, "'profit + loss'!A1"); cf = new CellReference("'O''Brien''s Sales'!A1"); ConfirmCell(cf, "O'Brien's Sales", 0, 0, false, false, "'O''Brien''s Sales'!A1"); cf = new CellReference("'Amazing!'!A1"); ConfirmCell(cf, "Amazing!", 0, 0, false, false, "'Amazing!'!A1"); }
public override String ToString() { CellReference crA = new CellReference(FirstRow, FirstColumn); CellReference crB = new CellReference(LastRow, LastColumn); StringBuilder sb = new StringBuilder(); sb.Append(GetType().Name).Append("["); sb.Append(_evaluator.SheetName); sb.Append('!'); sb.Append(crA.FormatAsString()); sb.Append(':'); sb.Append(crB.FormatAsString()); sb.Append("]"); return sb.ToString(); }
public Ref3DPxg(int externalWorkbookNumber, SheetIdentifier sheetName, CellReference c) : base(c) { this.externalWorkbookNumber = externalWorkbookNumber; this.firstSheetName = sheetName.SheetId.Name; if (sheetName is SheetRangeIdentifier) { this.lastSheetName = ((SheetRangeIdentifier)sheetName).LastSheetIdentifier.Name; } else { this.lastSheetName = null; } }
public void TestEvaluate() { IWorkbook wb = new HSSFWorkbook(); ISheet sh = wb.CreateSheet(); IRow row1 = sh.CreateRow(0); IRow row2 = sh.CreateRow(1); // Create cells row1.CreateCell(0, CellType.Numeric); row1.CreateCell(1, CellType.Numeric); row1.CreateCell(2, CellType.Numeric); row2.CreateCell(0, CellType.Numeric); row2.CreateCell(1, CellType.Numeric); // Create references CellReference a1 = new CellReference("A1"); CellReference a2 = new CellReference("A2"); CellReference b1 = new CellReference("B1"); CellReference b2 = new CellReference("B2"); CellReference c1 = new CellReference("C1"); // Set values sh.GetRow(a1.Row).GetCell(a1.Col).SetCellValue(210); sh.GetRow(a2.Row).GetCell(a2.Col).SetCellValue(55); sh.GetRow(b1.Row).GetCell(b1.Col).SetCellValue(35); sh.GetRow(b2.Row).GetCell(b2.Col).SetCellValue(0); sh.GetRow(c1.Row).GetCell(c1.Col).SetCellFormula("A1/B2"); ICell cell1 = sh.CreateRow(3).CreateCell(0); cell1.SetCellFormula("IFERROR(A1/B1,\"Error in calculation\")"); ICell cell2 = sh.CreateRow(3).CreateCell(0); cell2.SetCellFormula("IFERROR(A2/B2,\"Error in calculation\")"); ICell cell3 = sh.CreateRow(3).CreateCell(0); cell3.SetCellFormula("IFERROR(C1,\"error\")"); double accuracy = 1E-9; IFormulaEvaluator evaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); Assert.AreEqual(CellType.Numeric, evaluator.Evaluate(cell1).CellType, "Checks that the cell is numeric"); Assert.AreEqual(6.0, evaluator.Evaluate(cell1).NumberValue, accuracy, "Divides 210 by 35 and returns 6.0"); Assert.AreEqual(CellType.String, evaluator.Evaluate(cell2).CellType, "Checks that the cell is numeric"); Assert.AreEqual("Error in calculation", evaluator.Evaluate(cell2).StringValue, "Rounds -10 to a nearest multiple of -3 (-9)"); Assert.AreEqual(CellType.String, evaluator.Evaluate(cell3).CellType, "Check that C1 returns string"); Assert.AreEqual("error", evaluator.Evaluate(cell3).StringValue, "Check that C1 returns string \"error\""); }
public void TestConvertNumToColString() { short col = 702; String collRef = new CellReference(0, col).FormatAsString(); Assert.AreEqual("AAA1", collRef); short col2 = 0; String collRef2 = new CellReference(0, col2).FormatAsString(); Assert.AreEqual("A1", collRef2); short col3 = 27; String collRef3 = new CellReference(0, col3).FormatAsString(); Assert.AreEqual("AB1", collRef3); short col4 = 2080; String collRef4 = new CellReference(0, col4).FormatAsString(); Assert.AreEqual("CBA1", collRef4); }
/** * Gets the XSSFCell referenced by the R attribute or Creates a new one if cell doesn't exists * @return the referenced XSSFCell, null if the cell reference is invalid */ public ICell GetReferencedCell() { ICell cell = null; CellReference cellReference = new CellReference(SingleXmlCell.r); IRow row = parent.GetXSSFSheet().GetRow(cellReference.Row); if (row == null) { row = parent.GetXSSFSheet().CreateRow(cellReference.Row); } cell = row.GetCell(cellReference.Col); if (cell == null) { cell = row.CreateCell(cellReference.Col); } return cell; }
public Ref3DPtg(CellReference cr, int externIdx) : base(cr) { ExternSheetIndex = externIdx; }
/** * Creates an area ref from a pair of Cell References. */ public AreaReference(CellReference topLeft, CellReference botRight) { _firstCell = topLeft; _lastCell = botRight; _isSingleCell = false; }
private double GetCellValueInt(ISheet getSheet, string cellName) { CellReference companyName = new CellReference(cellName); //能源用戶名稱 IRow getRow = getSheet.GetRow(companyName.Row); ICell getCell = getRow.GetCell(companyName.Col); var value = getCell.NumericCellValue; return value; }
private string GetCellValueString(ISheet getSheet, string cellName) { CellReference companyName = new CellReference(cellName); //能源用戶名稱 IRow getRow = getSheet.GetRow(companyName.Row); ICell getCell = getRow.GetCell(companyName.Col); var value = getCell.StringCellValue; return value; }
/** * Creates cacheSource and workSheetSource for pivot table and Sets the source reference as well assets the location of the pivot table * @param source Source for data for pivot table * @param position Position for pivot table in sheet * @param sourceSheet Sheet where the source will be collected from */ protected internal void CreateSourceReferences(AreaReference source, CellReference position, ISheet sourceSheet) { //Get cell one to the right and one down from position, add both to AreaReference and Set pivot table location. AreaReference destination = new AreaReference(position, new CellReference(position.Row + 1, position.Col + 1)); CT_Location location; if (pivotTableDefinition.location == null) { location = pivotTableDefinition.AddNewLocation(); location.firstDataCol = (/*setter*/1); location.firstDataRow = (/*setter*/1); location.firstHeaderRow = (/*setter*/1); } else { location = pivotTableDefinition.location; } location.@ref = (/*setter*/destination.FormatAsString()); pivotTableDefinition.location = (/*setter*/location); //Set source for the pivot table CT_PivotCacheDefinition cacheDef = GetPivotCacheDefinition().GetCTPivotCacheDefInition(); CT_CacheSource cacheSource = cacheDef.AddNewCacheSource(); cacheSource.type = (/*setter*/ST_SourceType.worksheet); CT_WorksheetSource worksheetSource = cacheSource.AddNewWorksheetSource(); worksheetSource.sheet = (/*setter*/sourceSheet.SheetName); SetDataSheet(sourceSheet); String[] firstCell = source.FirstCell.CellRefParts; String[] lastCell = source.LastCell.CellRefParts; worksheetSource.@ref = (/*setter*/firstCell[2] + firstCell[1] + ':' + lastCell[2] + lastCell[1]); }
private static String GetReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) { //windows excel example: Sheet1!$C$3:$E$4 CellReference colRef = new CellReference(sheetName, startR, startC, true, true); CellReference colRef2 = new CellReference(sheetName, endR, endC, true, true); return "$" + colRef.CellRefParts[2] + "$" + colRef.CellRefParts[1] + ":$" + colRef2.CellRefParts[2] + "$" + colRef2.CellRefParts[1]; }
/** * is the reference for a whole-column reference, * such as C:C or D:G ? */ public static bool IsWholeColumnReference(SpreadsheetVersion version, CellReference topLeft, CellReference botRight) { if (null == version) { version = SpreadsheetVersion.EXCEL97; // how the code used to behave. } // These are represented as something like // C$1:C$65535 or D$1:F$0 // i.e. absolute from 1st row to 0th one if (topLeft.Row == 0 && topLeft.IsRowAbsolute && (botRight.Row == version.LastRowIndex) && botRight.IsRowAbsolute) { return(true); } return(false); }
private static String GetReferenceBuiltInRecord(String sheetName, int startC, int endC, int startR, int endR) { //windows excel example for built-in title: 'second sheet'!$E:$F,'second sheet'!$2:$3 CellReference colRef = new CellReference(sheetName, 0, startC, true, true); CellReference colRef2 = new CellReference(sheetName, 0, endC, true, true); String escapedName = SheetNameFormatter.Format(sheetName); String c; if (startC == -1 && endC == -1) c = ""; else c = escapedName + "!$" + colRef.CellRefParts[2] + ":$" + colRef2.CellRefParts[2]; CellReference rowRef = new CellReference(sheetName, startR, 0, true, true); CellReference rowRef2 = new CellReference(sheetName, endR, 0, true, true); String r = ""; if (startR == -1 && endR == -1) r = ""; else { if (!rowRef.CellRefParts[1].Equals("0") && !rowRef2.CellRefParts[1].Equals("0")) { r = escapedName + "!$" + rowRef.CellRefParts[1] + ":$" + rowRef2.CellRefParts[1]; } } StringBuilder rng = new StringBuilder(); rng.Append(c); if (rng.Length > 0 && r.Length > 0) rng.Append(','); rng.Append(r); return rng.ToString(); }
/** * @return The reference for the cell in the bottom-right part of the table * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref) * */ public CellReference GetEndCellReference() { if (endCellReference == null) { String ref1 = ctTable.@ref; String[] boundaries = ref1.Split(new char[] { ':' }); String from = boundaries[1]; endCellReference = new CellReference(from); } return endCellReference; }
/** * Creates a comment. * @param anchor the client anchor describes how this comment is attached * to the sheet. * @return the newly Created comment. */ public IComment CreateCellComment(IClientAnchor anchor) { XSSFClientAnchor ca = (XSSFClientAnchor)anchor; XSSFSheet sheet = (XSSFSheet)GetParent(); //create comments and vmlDrawing parts if they don't exist CommentsTable comments = sheet.GetCommentsTable(true); XSSFVMLDrawing vml = sheet.GetVMLDrawing(true); NPOI.OpenXmlFormats.Vml.CT_Shape vmlShape = vml.newCommentShape(); if (ca.IsSet()) { // convert offsets from emus to pixels since we get a DrawingML-anchor // but create a VML Drawing int dx1Pixels = ca.Dx1 / Units.EMU_PER_PIXEL; int dy1Pixels = ca.Dy1 / Units.EMU_PER_PIXEL; int dx2Pixels = ca.Dx2 / Units.EMU_PER_PIXEL; int dy2Pixels = ca.Dy2 / Units.EMU_PER_PIXEL; String position = ca.Col1 + ", " + dx1Pixels + ", " + ca.Row1 + ", " + dy1Pixels + ", " + ca.Col2 + ", " + dx2Pixels + ", " + ca.Row2 + ", " + dy2Pixels; vmlShape.GetClientDataArray(0).SetAnchorArray(0, position); } String ref1 = new CellReference(ca.Row1, ca.Col1).FormatAsString(); if (comments.FindCellComment(ref1) != null) { throw new ArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref1); } return new XSSFComment(comments, comments.NewComment(ref1), vmlShape); }
/// <summary> /// Called when this cell is modified. /// The purpose of this method is to validate the cell state prior to modification. /// </summary> internal void NotifyArrayFormulaChanging() { CellReference ref1 = new CellReference(this); String msg = "Cell " + ref1.FormatAsString() + " is part of a multi-cell array formula. " + "You cannot change part of an array."; NotifyArrayFormulaChanging(msg); }
public RefPtg(CellReference cr):base(cr) { }
private void SetCellValue(ISheet getSheet, string cellName, double value) { CellReference companyName = new CellReference(cellName); //能源用戶名稱 IRow getRow = getSheet.GetRow(companyName.Row); ICell getCell = getRow.GetCell(companyName.Col); getCell.SetCellValue(value); }