Utilizzo della classe GlobalizationSettings per le etichette di totale parziale personalizzate e altre etichette del grafico a torta
Possibili scenari di utilizzo
Aspose.Cells API hanno esposto il fileImpostazioni di globalizzazione class per gestire gli scenari in cui l’utente desidera utilizzare etichette personalizzate per i subtotali in un foglio di calcolo. Inoltre, ilImpostazioni di globalizzazione class può essere utilizzata anche per modificare il fileAltro etichetta per il grafico a torta durante il rendering del foglio di lavoro o del grafico.
Introduzione alla classe GlobalizationSettings
IlImpostazioni di globalizzazione class attualmente offre i seguenti 3 metodi che possono essere sovrascritti in una classe personalizzata per ottenere le etichette desiderate per i subtotali o per rendere il testo personalizzato per ilAltro etichetta di un grafico a torta.
- Impostazioni di globalizzazione.getTotalName: Ottiene il nome totale della funzione.
- GlobalizationSettings.getGrandTotalName: Ottiene il nome del totale complessivo della funzione.
- GlobalizationSettings.getOtherName: Ottiene il nome delle etichette “Altro” per i grafici a torta.
Etichette personalizzate per subtotali
IlImpostazioni di globalizzazionepuò essere utilizzata per personalizzare le etichette Subtotale sovrascrivendo ilImpostazioni di globalizzazione.getTotalName & GlobalizationSettings.getGrandTotalName metodi come dimostrato in precedenza.
// 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, è necessario assegnare il fileWorkbookSettings.GlobalizationSettings proprietà a un’istanza diImpostazioni personalizzateclass definita sopra 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 altra etichetta del grafico a torta
IlImpostazioni di globalizzazione classe offre ilgetAltroNome utile per assegnare un valore personalizzato all’etichetta “Altro” dei grafici a torta. Il frammento di codice seguente definisce una classe personalizzata ed esegue l’override digetAltroNome per ottenere un’etichetta personalizzata basata sulla lingua predefinita impostata per 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 visualizza il grafico in un’immagine mentre utilizza il fileImpostazioni personalizzateclasse creata sopra.
// 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 locale della macchina è impostata su Francia. Come puoi vedere, l’etichetta “Altro” è stata tradotta in “Autre” come definito inImpostazioni personalizzateclasse.