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

可能的使用场景

Aspose.Cells API 中已公开了 GlobalizationSettings 类,以处理用户希望在电子表格中使用自定义标签进行小计的情况。此外,当渲染工作表或图表时,还可以使用 GlobalizationSettings 类修改饼图的 Other 标签。

GlobalizationSettings类简介

全局化设置(GlobalizationSettings)类目前提供以下3种方法,可以重写为自定义类,以获取所需的小计标签或为饼图的“其他”标签呈现自定义文本。

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

自定义小计标签

全局化设置(GlobalizationSettings)类可通过重写GlobalizationSettings.getTotalName和GlobalizationSettings.getGrandTotalName方法来自定义小计标签,如以下所示。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
public String getTotalName(int functionType) {
switch (functionType) {
case ConsolidationFunction.AVERAGE:
return "AVG";
// Handle other cases
default:
return super.getTotalName(functionType);
}
}
public String getGrandTotalName(int functionType) {
switch (functionType) {
case ConsolidationFunction.AVERAGE:
return "GRAND AVG";
// Handle other cases
default:
return super.getGrandTotalName(functionType);
}
}
public String getOtherName()
{
String language = Locale.getDefault().getLanguage();
System.out.println(language);
switch (language)
{
case "en":
return "Other";
case "fr":
return "Autre";
case "de":
return "Andere";
//Handle other cases as per requirement
default:
return super.getOtherName();
}
}

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CustomLabelsforSubtotals.class) + "articles/";
// 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.getSettings().setGlobalizationSettings(new CustomSettings());
// Accesses the 1st worksheet from the collection which contains data
// Data resides in the cell range A2:B9
Worksheet sheet = book.getWorksheets().get(0);
// Adds SubTotal of type Average to the worksheet
sheet.getCells().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 + "CustomLabelsforSubtotals_out.xlsx");

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

全局化设置(GlobalizationSettings)类提供getOtherName方法,可用于为饼图的“其他”标签提供自定义值。以下代码片段定义一个自定义类,并重写getOtherName方法以根据为JVM设置的默认语言获取自定义标签。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
public String getTotalName(int functionType) {
switch (functionType) {
case ConsolidationFunction.AVERAGE:
return "AVG";
// Handle other cases
default:
return super.getTotalName(functionType);
}
}
public String getGrandTotalName(int functionType) {
switch (functionType) {
case ConsolidationFunction.AVERAGE:
return "GRAND AVG";
// Handle other cases
default:
return super.getGrandTotalName(functionType);
}
}
public String getOtherName()
{
String language = Locale.getDefault().getLanguage();
System.out.println(language);
switch (language)
{
case "en":
return "Other";
case "fr":
return "Autre";
case "de":
return "Andere";
//Handle other cases as per requirement
default:
return super.getOtherName();
}
}

以下代码片段加载包含饼图的现有电子表格,并在利用上面创建的CustomSettings类的情况下将图表呈现为图像。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CustomTextforOtherLabelofPieChart.class) + "articles/";
//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.getSettings().setGlobalizationSettings(new CustomSettings());
//Accesses the 1st worksheet from the collection which contains pie chart
Worksheet sheet = book.getWorksheets().get(0);
//Accesses the 1st chart from the collection
Chart chart = sheet.getCharts().get(0);
//Refreshes the chart
chart.calculate();
//Renders the chart to image
chart.toImage(dataDir + "CustomTextforOtherLabelofPieChart_out.png", new ImageOrPrintOptions());

当计算机的区域设置为法国时,以下是生成的图像。正如您所见,“其他”标签已根据CustomSettings类中定义的内容翻译为“Autre”。

todo:image_alt_text