[]
        
(Showing Draft Content)

GrapeCity.Documents.Excel.ICalculatedFields.Add

Add Method

Add(string, string)

Creates a new calculated field. Returns a IPivotField object.

Use this method to add a custom formula-based field to a PivotTable when built-in summary functions are not sufficient. The formula corresponds to the Formula value of the created field.

Declaration
IPivotField Add(string Name, string Formula)
Function Add(Name As String, Formula As String) As IPivotField
Parameters
Type Name Description
string Name

The name of the calculated field.

string Formula

The formula for the calculated field.

Returns
Type Description
IPivotField

The calculated field.

Examples
worksheet.Range["A1:B4"].Value = new object[,] {
    {"Product", "Amount"},
    {"A", 1200},
    {"B", 800},
    {"A", 600}
};
IPivotCache pivotCache = workbook.PivotCaches.Create(worksheet.Range["A1:B4"]);
IPivotTable pivotTable = worksheet.PivotTables.Add(pivotCache, worksheet.Range["D1"]);
IPivotField calculatedField = pivotTable.CalculatedFields.Add("Tax", "=IF(Amount > 1000, 3% * Amount, 0)");
calculatedField.Orientation = PivotFieldOrientation.DataField;