我怎样才能防止“#Div/0!”在计算单元格(Aspose Cells)中?

How can I prevent "#Div/0!" in calculated cells (Aspose Cells)?

我正在生成包含此计算字段的电子表格:

pivotTable.AddCalculatedField("Average Price", "=TotalPrice/TotalQty", true);

它大部分时间都像魅力一样工作,但偶尔会有 0 值,因此会产生“#Div/0!”,例如此处显示的第二个和最后一个项目:

我该如何预防?

这里是完整的数据透视表创建代码,更多上下文:

private void PopulatePivotTableSheet()
{
    int DESCRIPTION_COLUMN = 1;
    int MONTHYR_COLUMN = 3;
    int TOTALQTY_COLUMN = 4;
    int TOTALPRICE_COLUMN = 5;
    int PERCENTOFTOTAL_COLUMN = 7;
    int MONTHLY_PERCENTAGE_COLUMN = 8; 
    int AVGPRICE_COLUMN = 10;
    int COLUMNS_IN_DATA_SHEET = 11;
    int HEADER_ROW = 8;

    AddPreDataSectionToPivotTableSheet();

    PivotTableCollection pivotTables = pivotTableSheet.PivotTables;
    int colcount = COLUMNS_IN_DATA_SHEET;
    string lastColAsStr = ReportRunnerConstsAndUtils.GetExcelColumnName(colcount);
    int rowcount = sourceDataSheet.Cells.Rows.Count;
    string sourceDataArg = string.Format("sourceDataSheet!A1:{0}{1}", lastColAsStr, rowcount);
    int index = pivotTableSheet.PivotTables.Add(sourceDataArg, "A6", "PivotTableSheet");
    PivotTable pivotTable = pivotTables[index];

    pivotTable.RowGrand = true;
    pivotTable.ColumnGrand = true;

    pivotTable.DisplayNullString = true;
    pivotTable.NullString = "0";

    pivotTable.AddFieldToArea(PivotFieldType.Row, DESCRIPTION_COLUMN);
    pivotTable.RowHeaderCaption = "Description";

    pivotTable.AddFieldToArea(PivotFieldType.Column, MONTHYR_COLUMN);
    pivotTable.ColumnHeaderCaption = "Months";

    pivotTable.AddFieldToArea(PivotFieldType.Data, TOTALQTY_COLUMN);
    pivotTable.DataFields[0].DisplayName = "Total Packages";

    pivotTable.AddFieldToArea(PivotFieldType.Data, TOTALPRICE_COLUMN);
    pivotTable.DataFields[1].DisplayName = "Total Purchases";

    pivotTable.AddCalculatedField("Average Price", "=TotalPrice/TotalQty", true);

    pivotTable.AddCalculatedField("PercentOfTotal", "=TotalPrice", true);
    pivotTable.DataFields[3].DisplayName = "Percentage of Total";
    pivotTable.DataFields[3].DataDisplayFormat = PivotFieldDataDisplayFormat.PercentageOfColumn;
    pivotTable.RowFields[0].IsAutoSubtotals = false;

    PivotField field = pivotTable.RowFields[0];
    field.IsAutoSort = true;
    field.IsAscendSort = false;
    field.AutoSortField = 1;
    pivotTable.PivotTableStyleType = PivotTableStyleType.PivotTableStyleLight16;

    pivotTable.RefreshDataFlag = true;
    pivotTable.RefreshData();
    pivotTable.CalculateData();
    pivotTable.RefreshDataFlag = false;

    List<String> contractItemDescs = GetContractItemDescriptions();
    ColorizeContractItemBlocks(contractItemDescs);
    HideItemsWithFewerThan1PercentOfSales();
    FreezePanePivotTable(HEADER_ROW, 2); 
    FormatPivotTableNumbers();

    Style style = workBook.CreateStyle();
    style.Font.Name = "Calibri";
    style.Font.Size = 12;
    pivotTable.FormatAll(style);

    sourceDataSheet.IsVisible = false;
}

在除以它之前检查 TotalQty 是否为零:

"=IF(TotalQty<>0,TotalPrice/TotalQty,0)"

第三个参数是 TotalQty 为零时要显示的值。

您也可以试试:

IFERROR(TotalPrice/TotalQty,"")

如果您愿意,双引号可以是零。

只需使用

if(ISBLANK((range of cell),"",your function here)

"" 用于显示为空单元格(空白,好像这里什么都没写),以防单元格范围不包含任何内容。否则,如果在单元格

中找到任何值,您的函数将为 运行