[]
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.
IPivotField Add(string Name, string Formula)
Function Add(Name As String, Formula As String) As IPivotField
| Type | Name | Description |
|---|---|---|
| string | Name | The name of the calculated field. |
| string | Formula | The formula for the calculated field. |
| Type | Description |
|---|---|
| IPivotField | The calculated field. |
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;