Appliquer un formatage conditionnel dans les feuilles de calcul avec C++

Utilisation d’Aspose.Cells pour Appliquer un Formatage Conditionnel Basé sur la Valeur de la Cellule

  1. Téléchargez et Installez Aspose.Cells.
    1. Téléchargez Aspose.Cells for C++.
  2. Installez-le sur votre ordinateur de développement. Tous les composants Aspose, une fois installés, fonctionnent en mode d’évaluation. Le mode d’évaluation n’a pas de limite de temps et n’injecte que des filigranes dans les documents produits.
  3. Créer un projet. Démarrez votre environnement de développement C++ et créez une nouvelle application console.
  4. Ajouter des références. Ajoutez une référence à Aspose.Cells à votre projet, par exemple ajoutez une référence à ….\ Program Files\ Aspose\ Aspose.Cells\ Bin\ Net1.0\ Aspose.Cells.dll
  5. ** Appliquer une mise en forme conditionnelle en fonction de la valeur de la cellule **. Voici le code utilisé pour réaliser la tâche. Il applique un formatage conditionnel sur une cellule.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Instantiating a Workbook object
    Workbook workbook;

    // Get the first worksheet
    Worksheet sheet = workbook.GetWorksheets().Get(0);

    // Adds an empty conditional formatting
    int index = sheet.GetConditionalFormattings().Add();

    // Get the FormatConditionCollection
    FormatConditionCollection fcs = sheet.GetConditionalFormattings().Get(index);

    // Sets the conditional format range
    CellArea ca = CellArea::CreateCellArea(0, 0, 0, 0);

    // Add the cell area to the format condition collection
    fcs.AddArea(ca);

    // Adds condition
    int conditionIndex = fcs.AddCondition(FormatConditionType::CellValue, OperatorType::Between, u"50", u"100");

    // Get the format condition
    FormatCondition fc = fcs.Get(conditionIndex);

    // Sets the background color
    fc.GetStyle().SetBackgroundColor(Color::Red());

    // Saving the Excel file
    workbook.Save(outDir + u"output.out.xls", SaveFormat::Auto);

    std::cout << "Conditional formatting applied successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Lorsque le code ci-dessus est exécuté, le formatage conditionnel est appliqué à la cellule “A1” de la première feuille du fichier de sortie (output.xls). Le formatage conditionnel appliqué à A1 dépend de la valeur de la cellule. Si la valeur d’A1 est comprise entre 50 et 100, la couleur de fond est rouge en raison du formatage conditionnel appliqué.

Utilisation d’Aspose.Cells pour appliquer une mise en forme conditionnelle en fonction de la formule

  1. Application du formatage conditionnel selon la formule (Extrait de code) Voici le code pour accomplir la tâche. Il applique une mise en forme conditionnelle sur B3.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Create workbook
    Workbook workbook;

    // Get the first worksheet
    Worksheet sheet = workbook.GetWorksheets().Get(0);

    // Adds an empty conditional formatting
    int index = sheet.GetConditionalFormattings().Add();

    // Get the conditional formatting collection
    FormatConditionCollection fcs = sheet.GetConditionalFormattings().Get(index);

    // Sets the conditional format range
    CellArea ca = CellArea::CreateCellArea(2, 1, 2, 1);

    // Add the area to the conditional formatting
    fcs.AddArea(ca);

    // Adds condition
    int conditionIndex = fcs.AddCondition(FormatConditionType::Expression);

    // Get the format condition
    FormatCondition fc = fcs.Get(conditionIndex);

    // Set the formula for the condition
    fc.SetFormula1(u"=IF(SUM(B1:B2)>100,TRUE,FALSE)");

    // Set the background color
    Style style = fc.GetStyle();
    style.SetBackgroundColor(Color::Red());
    fc.SetStyle(style);

    // Set the formula for cell B3
    sheet.GetCells().Get(u"B3").SetFormula(u"=SUM(B1:B2)");

    // Set the value for cell C4
    sheet.GetCells().Get(u"C4").PutValue(u"If Sum of B1:B2 is greater than 100, B3 will have RED background");

    // Save the Excel file
    workbook.Save(outDir + u"output.out.xls", SaveFormat::Auto);

    std::cout << "Conditional formatting applied successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Lorsque le code ci-dessus est exécuté, le formatage conditionnel est appliqué à la cellule “B3” de la première feuille du fichier de sortie (output.xls). Le formatage dépend de la formule qui calcule la valeur de “B3” comme la somme de B1 et B2.