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 per gestire gli scenari in cui l’utente desidera utilizzare etichette personalizzate per le subtotali in un foglio di calcolo. Inoltre, la classe GlobalizationSettings può anche essere utilizzata per modificare l’etichetta Altro per il grafico a torta durante la visualizzazione del foglio di lavoro o del grafico.

Introduzione alla classe GlobalizationSettings

La classe GlobalizationSettings offre attualmente i seguenti 3 metodi che possono essere sovrascritti in una classe personalizzata per ottenere etichette desiderate per le subtotali o per creare testo personalizzato per l’etichetta Altro di un grafico a torta.

  1. GlobalizationSettings.GetTotalName: Ottiene il nome totale della funzione.
  2. GlobalizationSettings.GetGrandTotalName: Ottiene il nome del totale generale della funzione.
  3. GlobalizationSettings.GetOtherName: Ottiene 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 delle subtotali sovra-ridendo i metodi GlobalizationSettings.GetTotalName e GlobalizationSettings.GetGrandTotalName come dimostrato di seguito.

// 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);
}
}
}

Per iniettare etichette personalizzate, è necessario assegnare la proprietà WorkbookSettings.GlobalizationSettings a un’istanza della classe CustomSettings definita sopra prima di aggiungere le subtotali al foglio di lavoro.

// 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");

Testo personalizzato per l’etichetta Altro del grafico a torta

La classe GlobalizationSettings offre il metodo GetOtherName che è utile per dare un valore personalizzato all’etichetta “Altro” dei grafici a torta. Il seguente snippet definisce una classe personalizzata e sovrascrive il metodo GetOtherName per ottenere un’etichetta personalizzata in base all’identificatore della cultura del sistema.

// 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();
}
}
}

Il seguente snippet carica un foglio di calcolo esistente contenente un grafico a torta e visualizza il grafico come 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-.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());