[]
        
(Showing Draft Content)

ICalcContext

Interface ICalcContext


public interface ICalcContext
Represents the context of the calculation.

Provides information about the cell currently being calculated, including its containing worksheet, row index, and column index. This interface is typically used in custom function evaluation logic to determine where the calculation is occurring.


 CustomFunction function = new CustomFunction("CELLPOS", FunctionValueType.Text) {
     public Object evaluate(Object[] arguments, ICalcContext context) {
         IWorksheet sheet = context.getWorksheet();
         int row = context.getRow();
         int column = context.getColumn();
         return sheet.getName() + "!" + row + "," + column;
     }
 };
 Workbook.AddCustomFunction(function);
 worksheet.getRange("B2").setFormula("=CELLPOS()");
 Object cellPosition = worksheet.getRange("B2").getValue();
 
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Get the column index of the cell which is calculating.
    int
    Get the row index of the cell which is calculating.
    Get the IWorksheet that contains the cell which is calculating.
  • Method Details

    • getWorksheet

      IWorksheet getWorksheet()
      Get the IWorksheet that contains the cell which is calculating.

      Use this method in a custom function to access the worksheet that contains the cell whose formula is being evaluated.

      
       CustomFunction function = new CustomFunction("CAPTURESHEET", FunctionValueType.Text) {
           public Object evaluate(Object[] arguments, ICalcContext context) {
               IWorksheet sheet = context.getWorksheet();
               return sheet.getName();
           }
       };
       Workbook.AddCustomFunction(function);
       worksheet.getRange("B2").setFormula("=CAPTURESHEET()");
       Object sheetName = worksheet.getRange("B2").getValue();
       
      Returns:
      The worksheet that contains the cell being calculated.
    • getRow

      int getRow()
      Get the row index of the cell which is calculating.

      Use this method in a custom function to identify the zero-based row position of the cell whose formula is currently being evaluated.

      
       CustomFunction function = new CustomFunction("CAPTUREROW", FunctionValueType.Number) {
           public Object evaluate(Object[] arguments, ICalcContext context) {
               int row = context.getRow();
               return (double) row;
           }
       };
       Workbook.AddCustomFunction(function);
       worksheet.getRange("C3").setFormula("=CAPTUREROW()");
       Object row = worksheet.getRange("C3").getValue();
       
      Returns:
      The zero-based row index of the cell being calculated.
    • getColumn

      int getColumn()
      Get the column index of the cell which is calculating.

      Use this method in a custom function to identify the zero-based column position of the cell whose formula is currently being evaluated.

      
       CustomFunction function = new CustomFunction("CAPTURECOLUMN", FunctionValueType.Number) {
           public Object evaluate(Object[] arguments, ICalcContext context) {
               int column = context.getColumn();
               return (double) column;
           }
       };
       Workbook.AddCustomFunction(function);
       worksheet.getRange("D4").setFormula("=CAPTURECOLUMN()");
       Object column = worksheet.getRange("D4").getValue();
       
      Returns:
      The zero-based column index of the cell being calculated.