简体   繁体   English

如何在计算出的像元(Aspose Cells)中防止“#Div / 0!”?

[英]How can I prevent “#Div/0!” in calculated cells (Aspose Cells)?

I am generating a spreadsheet with this calculated field: 我正在使用此计算字段生成电子表格:

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

It works like a charm most of the time, but once in awhile there are 0-values, and thus produce a "#Div/0!", such as for the second and last items shown here: 多数情况下,它的工作方式就像一个超级按钮,但偶尔会有0值,因此会产生“#Div / 0!”,例如此处显示的第二项和最后一项:

在此处输入图片说明

How can I prevent that? 我该如何预防?

Here is the PivotTable creation code in full, for more context: 以下是完整的数据透视表创建代码,以获取更多上下文:

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;
}

Check whether TotalQty is zero before dividing by it: 除以之前,请检查TotalQty是否为零:

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

The third argument is the value to display if TotalQty is zero. 第三个参数是TotalQty为零时要显示的值。

You could also try: 您也可以尝试:

IFERROR(TotalPrice/TotalQty,"")

The double quotes can be a zero if you wish. 如果需要,双引号可以为零。

simply use 只需使用

if(ISBLANK((range of cell),"",your function here) if(ISBLANK((单元格的范围),“”,您的函数在这里)

"" is used to show as empty cell (blank as if nothing is written here) in case the range of cell doesn't consist of anything. 如果单元格的范围不包含任何内容,则“”用于显示为空单元格(空白处好像没有写任何内容)。 Otherwise your function will be run if any value is found in the cells 否则,如果在单元格中找到任何值,则将运行您的函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何提供基于Aspose.Cells数据透视表中其他两个值的计算值? - How can I provide a calculated value that is based on two other values in an Aspose.Cells PivotTable? 将图像放在电子表格(Aspose Cells)上时,如何防止图像尺寸改变? - How can I prevent my image from changing size when placing it on a spreadsheet (Aspose Cells)? 如何在电子表格范围(Aspose Cells)周围添加边框? - How can I add borders around a spreadsheet range (Aspose Cells)? 如何确定数据透视表生成多少行(Aspose Cells)? - How can I determine how many rows a PivotTable generates (Aspose Cells)? 如何覆盖数据透视表(设置单元格)上的列标签文本? - How can I override the column label text on a Pivot Table (Aspose Cells)? 如何将电子表格中的列宽设置为特定的像素数(Aspose Cells)? - How can I set the column width in a spreadsheet to a specific pixel count (Aspose Cells)? 如何用不同的值代替自动生成的总计值(Aspose Cells)? - How can I replace automatically-generated grand total values with different ones (Aspose Cells)? 如何使在多个Excel电子表格中使用的图像始终以全尺寸显示(Aspose Cells)? - How can I make an image used on multiple Excel spreadsheets always display at its full size (Aspose Cells)? 如何为所有相关的行和列着色,而不仅仅是它们的一部分(Aspose Cells)? - How can I color all the related rows and columns, rather than just a subset of them (Aspose Cells)? 如何设置aspose单元格以从OS中获取日期格式的语言环境? - How can I set aspose cells to take the locale for date format from OS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM