Usa la classe GlobalizationSettings per etichette di subtotale personalizzate e altre etichette del grafico a torta con Node.js via C++
Possibili Scenari di Utilizzo
Le API di Aspose.Cells hanno esposto la classe GlobalizationSettings per gestire scenari in cui l’utente desidera usare etichette personalizzate per i Subtotali in un foglio di calcolo. Inoltre, la classe GlobalizationSettings può essere usata anche per modificare l’etichetta Other del grafico a torta durante il rendering del foglio 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 le etichette desiderate per i Subtotali o per rendere testi personalizzati per l’etichetta Other di un grafico a torta.
- GlobalizationSettings.getTotalName(ConsolidationFunction): Ottiene il nome totale della funzione.
- GlobalizationSettings.getGrandTotalName(ConsolidationFunction): Ottiene il nome del totale generale della funzione.
Etichette personalizzate per le subtotali
La classe GlobalizationSettings può essere usata per personalizzare le etichette di subtotale sovrascrivendo i metodi GlobalizationSettings.getTotalName(ConsolidationFunction) & GlobalizationSettings.getGrandTotalName(ConsolidationFunction) come dimostrato in avanti.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// Defines a custom class derived from GlobalizationSettings class
class CustomSettings extends AsposeCells.GlobalizationSettings {
// Overrides the GetTotalName method
getTotalName(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 AsposeCells.ConsolidationFunction.Average:
return "AVG";
// Handle other cases as per requirement
default:
return super.getTotalName(functionType);
}
}
// Overrides the GetGrandTotalName method
getGrandTotalName(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 AsposeCells.ConsolidationFunction.Average:
return "GRD AVG";
// Handle other cases as per requirement
default:
return super.getGrandTotalName(functionType);
}
}
}
Per aggiungere etichette personalizzate, è necessario assegnare la proprietà WorkbookSettings.getGlobalizationSettings() a un’istanza della classe CustomSettings definita sopra prima di aggiungere i Subtotali al foglio di calcolo.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// Defines a custom class derived from GlobalizationSettings class
class CustomSettings extends AsposeCells.GlobalizationSettings {
// Overrides the GetTotalName method
getTotalName(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 AsposeCells.ConsolidationFunction.Average:
return "AVG";
// Handle other cases as per requirement
default:
return super.getTotalName(functionType);
}
}
// Overrides the GetGrandTotalName method
getGrandTotalName(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 AsposeCells.ConsolidationFunction.Average:
return "GRD AVG";
// Handle other cases as per requirement
default:
return super.getGrandTotalName(functionType);
}
}
}
try {
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Loads an existing spreadsheet containing some data
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sample.xlsx"));
// Assigns the GlobalizationSettings property of the WorkbookSettings class to a custom class created
workbook.getSettings().setGlobalizationSettings(new CustomSettings());
// Accesses the 1st worksheet from the collection which contains data that resides in the cell range A2:B9
const sheet = workbook.getWorksheets().get(0);
// Adds Subtotal of type Average to the worksheet
sheet.getCells().subtotal(AsposeCells.CellArea.createCellArea("A2", "B9"), 0, AsposeCells.ConsolidationFunction.Average, [1]);
// Calculates Formulas
workbook.calculateFormula();
// Auto fits all columns
sheet.autoFitColumns();
// Saves the workbook on disc
workbook.save(path.join(dataDir, "output_out.xlsx"));
} catch (error) {
console.error(`Test failed: ${error.message}`);
throw error;
}