Utilizzo della classe GlobalizationSettings per etichette subtotali personalizzate e altre etichette del grafico a torta
Possibili Scenari di Utilizzo
Le API di Aspose.Cells hanno esposto la classe GlobalizationSettings al fine di gestire gli scenari in cui l’utente desidera utilizzare etichette personalizzate per i subtotale in un foglio di calcolo. Inoltre, la classe GlobalizationSettings può essere utilizzata anche per modificare l’etichetta Altro per il grafico a torta durante il rendering del foglio di lavoro o del grafico.
Introduzione alla classe GlobalizationSettings
La classe GlobalizationSettings attualmente offre i seguenti 3 metodi che possono essere sovrascritti in una classe personalizzata per ottenere etichette desiderate per i subtotale o per rendere del testo personalizzato per l’etichetta Altro di un grafico a torta.
- GlobalizationSettings.getTotalName: Restituisce il nome totale della funzione.
- GlobalizationSettings.getGrandTotalName: Restituisce il nome totale generale della funzione.
- GlobalizationSettings.getOtherName: Restituisce il nome delle etichette “Altro” per i grafici a torta.
Etichette personalizzate per le subtotali
La classe GlobalizationSettings può essere utilizzata per personalizzare le etichette di subtotale sovra scrivendo i metodi GlobalizationSettings.getTotalName & GlobalizationSettings.getGrandTotalName come dimostrato in seguito.
// 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(); | |
} | |
} |
Per iniettare etichette personalizzate, è richiesto assegnare la proprietà WorkbookSettings.GlobalizationSettings a un’istanza della classe CustomSettings definita precedentemente prima di aggiungere i subtotali al foglio di lavoro.
// 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"); |
Testo personalizzato per l’etichetta Altro del grafico a torta
La classe GlobalizationSettings offre il metodo getOtherName: utile per dare all’etichetta “Altro” dei grafici a torta un valore personalizzato. Il seguente snippet definisce una classe personalizzata e sovrascrive il metodo getOtherName per ottenere un’etichetta personalizzata in base alla lingua predefinita impostata per la 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(); | |
} | |
} |
Il seguente frammento carica un foglio di calcolo esistente contenente un grafico a torta e renderizza il grafico in un’immagine utilizzando la classe CustomSettings creata in precedenza.
// 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()); |
Di seguito è riportata l’immagine risultante quando la localizzazione della macchina è impostata su Francia. Come puoi vedere, l’etichetta “Altro” è stata tradotta in “Autre” come definito nella classe CustomSettings.