Imposta i Formati Condizionali dei file Excel e ODS

Introduzione

La formattazione condizionale è una funzione avanzata che permette di applicare formati a una cella o un intervallo di celle e fare in modo che tale formattazione cambi in base al valore della cella o al valore di una formula. Ad esempio, puoi far apparire una cella in grassetto solo quando il valore della cella è superiore a 500. Quando il valore della cella soddisfa la condizione, il formato specificato viene applicato alla cella. Se il valore della cella non soddisfa la condizione, viene utilizzata la formattazione predefinita della cella. In Microsoft Excel, seleziona Formato, poi Formattazione Condizionale per aprire la finestra di dialogo della formattazione condizionale.

Aspose.Cells supporta l’applicazione della formattazione condizionale alle celle in fase di esecuzione. Questo articolo spiega come farlo. Spiega anche come calcolare il colore utilizzato da Excel per la formattazione condizionale basata sulla scala cromatica.

Applicare la formattazione condizionale

Aspose.Cells supporta la formattazione condizionale in diversi modi:

  • Utilizzando il foglio di calcolo del designer
  • Utilizzando il metodo di copia.
  • Creare la formattazione condizionale in fase di esecuzione.

Utilizzo del foglio di calcolo del designer

I programmatori possono creare un foglio di calcolo del designer che contiene la formattazione condizionale in Microsoft Excel e quindi aprire quel foglio di calcolo con Aspose.Cells. Aspose.Cells carica e salva il foglio di calcolo del designer, mantenendo qualsiasi impostazione della formattazione condizionale.

Utilizzando il metodo di copia

Aspose.Cells consente ai programmatori di copiare le impostazioni della formattazione condizionale da una cella a un’altra nel foglio di lavoro chiamando il metodo Range.copy().

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open a template Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xlsx"));
// Get the first worksheet in the workbook
const sheet = workbook.getWorksheets().get(0);
// Get the A1 cell
const cell = sheet.getCells().get("A1");
// Get the conditional formatting result object
const cfr = cell.getConditionalFormattingResult();
// Get the icon set
const icon = cfr.getConditionalFormattingIcon();
// Create the image file based on the icon's image data
require("fs").writeFileSync(path.join(dataDir, "imgIcon.out.jpg"), icon.getImageData());

Applicare la formattazione condizionale in fase di esecuzione

Aspose.Cells consente di aggiungere e rimuovere la formattazione condizionale in fase di esecuzione. Di seguito sono riportati degli esempi di codice su come impostare la formattazione condizionale:

  1. Istanziare un foglio di lavoro.
  2. Aggiungere un formato condizionale vuoto.
  3. Impostare l’intervallo a cui dovrebbe essere applicata la formattazione.
  4. Definire le condizioni di formattazione.
  5. Salvare il file.

Dopo questo esempio ci sono diversi altri esempi più piccoli che mostrano come applicare impostazioni del carattere, impostazioni dei bordi e schemi.

Microsoft Excel 2007 ha aggiunto una formattazione condizionale più avanzata, che anche Aspose.Cells supporta. Gli esempi qui mostrano come usare formattazioni semplici, mentre gli esempi di Microsoft Excel 2007 mostrano come applicare formattazioni condizionali più avanzate.

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xlsx");
// 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();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
let ca = AsposeCells.CellArea.createCellArea(0, 0, 0, 0);
fcs.addArea(ca);
ca = AsposeCells.CellArea.createCellArea(1, 1, 1, 1);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "=A2", "100");
// Adds condition.
const conditionIndex2 = 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(path.join(dataDir, "output.xls"));

Imposta Carattere

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open a template Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xlsx"));
// Get the first worksheet in the workbook
const sheet = workbook.getWorksheets().get(0);
// Get the A1 cell
const cell = sheet.getCells().get("A1");
// Get the conditional formatting result object
const cfr = cell.getConditionalFormattingResult();
// Get the icon set
const icon = cfr.getConditionalFormattingIcon();
// Create the image file based on the icon's image data
require("fs").writeFileSync(path.join(dataDir, "imgIcon.out.jpg"), icon.getImageData());

Imposta Bordo

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().add();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(0, 0, 5, 3);
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().getBorders().get(AsposeCells.BorderType.LeftBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.RightBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.TopBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.LeftBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.RightBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.TopBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(new AsposeCells.Color(255, 255, 0));
workbook.save(path.join(dataDir, "output.xlsx"));

Imposta Schema

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().add();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(0, 0, 5, 3);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
const fc = fcs.get(conditionIndex);
fc.getStyle().setPattern(AsposeCells.BackgroundType.ReverseDiagonalStripe);
fc.getStyle().setForegroundColor(new AsposeCells.Color(255, 255, 0));
fc.getStyle().setBackgroundColor(new AsposeCells.Color(0, 255, 255));
workbook.save(path.join(dataDir, "output.xlsx"));

Argomenti avanzati