使用 GlobalizationSettings 类自定义小计标签和饼图的其他标签

可能的使用场景

Aspose.Cells API暴露了全球化设置类以处理用户希望在电子表格中为小计使用自定义标签的场景。此外,全球化设置类也可以用来修改其他呈现工作表或图表时饼图的标签。

GlobalizationSettings 类简介

全球化设置类目前提供以下 3 种方法,可以在自定义类中重写这些方法以获得小计所需的标签或呈现自定义文本其他饼图的标签。

  1. GlobalizationSettings.GetTotalName:获取函数的总名称。
  2. GlobalizationSettings.GetGrandTotalName:获取函数的总计名称。
  3. GlobalizationSettings.GetOtherName:获取饼图“其他”标签的名称。

小计的自定义标签

全球化设置类可用于通过覆盖自定义小计标签GlobalizationSettings.GetTotalName & GlobalizationSettings.GetGrandTotalName前面演示的方法。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Defines a custom class derived from GlobalizationSettings class
class CustomSettings : GlobalizationSettings
{
// Overrides the GetTotalName method
public override string GetTotalName(ConsolidationFunction functionType)
{
// Checks the function type used to add the subtotals
switch (functionType)
{
// Returns custom value based on the function type used to add the subtotals
case ConsolidationFunction.Average:
return "AVG";
// Handle other cases as per requirement
default:
return base.GetTotalName(functionType);
}
}
// Overrides the GetGrandTotalName method
public override string GetGrandTotalName(ConsolidationFunction functionType)
{
// Checks the function type used to add the subtotals
switch (functionType)
{
// Returns custom value based on the function type used to add the subtotals
case ConsolidationFunction.Average:
return "GRD AVG";
// Handle other cases as per requirement
default:
return base.GetGrandTotalName(functionType);
}
}
}

为了注入自定义标签,需要分配WorkbookSettings.GlobalizationSettings的一个实例的属性自定义设置在将小计添加到工作表之前在上面定义的类。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Loads an existing spreadsheet containing some data
Workbook book = new Workbook(dataDir + "sample.xlsx");
// Assigns the GlobalizationSettings property of the WorkbookSettings class to the class created in first step
book.Settings.GlobalizationSettings = new CustomSettings();
// Accesses the 1st worksheet from the collection which contains data resides in the cell range A2:B9
Worksheet sheet = book.Worksheets[0];
// Adds Subtotal of type Average to the worksheet
sheet.Cells.Subtotal(CellArea.CreateCellArea("A2", "B9"), 0, ConsolidationFunction.Average, new int[] { 1 });
// Calculates Formulas
book.CalculateFormula();
// Auto fits all columns
sheet.AutoFitColumns();
// Saves the workbook on disc
book.Save(dataDir + "output_out.xlsx");

饼图其他标签的自定义文本

全球化设置课程优惠获取其他名称为饼图的“其他”标签提供自定义值很有用的方法。以下代码段定义了一个自定义类并覆盖了获取其他名称根据系统的文化标识符获取自定义标签的方法。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Defines a custom class inherited by GlobalizationSettings class
class GlobalCustomSettings : ChartGlobalizationSettings
{
// Overrides the GetOtherName method
public override string GetOtherName()
{
// Gets the culture identifier for the current system
int lcid = System.Globalization.CultureInfo.CurrentCulture.LCID;
switch (lcid)
{
// Handles case for English
case 1033:
return "Other";
// Handles case for French
case 1036:
return "Autre";
// Handles case for German
case 1031:
return "Andere";
// Handle other cases
default:
return base.GetOtherName();
}
}
}

以下代码片段加载包含饼图的现有电子表格,并在利用自定义设置上面创建的类。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Loads an existing spreadsheet containing a pie chart
Workbook book = new Workbook(dataDir + "sample.xlsx");
// Assigns the GlobalizationSettings property of the WorkbookSettings class to the class created in first step
book.Settings.GlobalizationSettings.ChartSettings = new GlobalCustomSettings();
// Accesses the 1st worksheet from the collection which contains pie chart
Worksheet sheet = book.Worksheets[0];
// Accesses the 1st chart from the collection
Chart chart = sheet.Charts[0];
// Refreshes the chart
chart.Calculate();
// Renders the chart to image
chart.ToImage(dataDir + "output_out.png", new ImageOrPrintOptions());