[英]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.