Applicare la formattazione condizionale nei fogli di lavoro

Utilizzare Aspose.Cells per Applicare la Formattazione Condizionale in Base al Valore della Cella

  1. Scarica e installa Aspose.Cells.
    1. Scarica Aspose.Cells for Node.js via C++.
  2. Installalo sul tuo computer di sviluppo. Tutti i componenti Aspose, una volta installati, funzionano in modalità di valutazione. La modalità di valutazione non ha limiti di tempo e inserisce solo filigrane nei documenti prodotti.
  3. Crea un progetto. Inizia il tuo progetto Node.js inizializzandolo. Questo esempio crea un’applicazione console Node.js.
  4. Aggiungi riferimenti. Aggiungi un riferimento a Aspose.Cells al tuo progetto, ad esempio richiedendo il pacchetto come segue:
    const AsposeCells = require("aspose.cells.node");
    
  5. Applicare formattazione condizionale basata sul valore della cella. Di seguito il codice usato per completare il compito. Applica la formattazione condizionale a una cella.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const outputFilePath = path.join(dataDir, "output.out.xls");
// Create directory if it is not already present.
const fs = require("fs");
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().getCount();
// Get the collection of conditional formatting
const fcs = sheet.getConditionalFormattings();
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(0, 0, 0, 0);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
// Sets the background color.
const fc = fcs.get(conditionIndex);
fc.getStyle().setBackgroundColor(AsposeCells.Color.Red);
// Saving the Excel file
workbook.save(outputFilePath, AsposeCells.SaveFormat.Auto);

Quando viene eseguito il codice sopra, viene applicata una formattazione condizionale alla cella “A1” nel primo foglio di lavoro del file di output (output.xls). La formattazione condizionale applicata ad A1 dipende dal valore della cella. Se il valore di A1 è tra 50 e 100, lo sfondo diventa rosso grazie alla formattazione condizionale applicata.

Utilizzare Aspose.Cells per Applicare la Formattazione Condizionale in Base a una Formula

  1. Applicare la formattazione condizionale in base alla formula (Snippet di codice) Di seguito è riportato il codice per completare il compito. Si applica la formattazione condizionale su B3.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
const fs = require("fs");
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().getCount();
sheet.getConditionalFormattings().add();
// Gets the conditional format collection
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(2, 1, 2, 1);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.Expression);
// Sets the background color.
const fc = fcs.get(conditionIndex);
fc.setFormula1("=IF(SUM(B1:B2)>100,TRUE,FALSE)");
fc.getStyle().setBackgroundColor(AsposeCells.Color.Red);
sheet.getCells().get("B3").setFormula("=SUM(B1:B2)");
sheet.getCells().get("C4").putValue("If Sum of B1:B2 is greater than 100, B3 will have RED background");
// Saving the Excel file
workbook.save(path.join(dataDir, "output.out.xls"), AsposeCells.SaveFormat.Auto);

Quando viene eseguito il codice sopra, la formattazione condizionale viene applicata alla cella “B3” nel primo foglio di lavoro del file di output (output.xls). La formattazione condizionale dipende dalla formula che calcola il valore di “B3” come somma di B1 e B2.