Ajout des formats conditionnels Échelle à 2 couleurs et Échelle à 3 couleurs avec C++
Contents
[
Hide
]
Les formats conditionnels 2 couleurs et 3 couleurs sont ajoutés de la même manière, sauf qu’ils sont différenciés par la propriété FormatCondition.GetIs3ColorScale(). Cette propriété est false pour les formats conditionnels 2 couleurs et true pour les formats conditionnels 3 couleurs.
Le code d’exemple suivant ajoute des formats conditionnels 2 couleurs et 3 couleurs. Il génère le fichier Excel en sortie.
#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;
// Access first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Add some data in cells
worksheet.GetCells().Get(u"A1").PutValue(u"2-Color Scale");
worksheet.GetCells().Get(u"D1").PutValue(u"3-Color Scale");
for (int i = 2; i <= 15; i++)
{
int row = i - 1;
worksheet.GetCells().Get(row, 0).PutValue(i); // Column A (0)
worksheet.GetCells().Get(row, 3).PutValue(i); // Column D (3)
}
// Adding 2-Color Scale Conditional Formatting
CellArea ca = CellArea::CreateCellArea(u"A2", u"A15");
int idx = worksheet.GetConditionalFormattings().Add();
FormatConditionCollection fcc = worksheet.GetConditionalFormattings().Get(idx);
fcc.AddCondition(FormatConditionType::ColorScale);
fcc.AddArea(ca);
FormatCondition fc = worksheet.GetConditionalFormattings().Get(idx).Get(0);
fc.GetColorScale().SetIs3ColorScale(false);
fc.GetColorScale().SetMaxColor(Color::LightBlue());
fc.GetColorScale().SetMinColor(Color::LightGreen());
// Adding 3-Color Scale Conditional Formatting
ca = CellArea::CreateCellArea(u"D2", u"D15");
idx = worksheet.GetConditionalFormattings().Add();
fcc = worksheet.GetConditionalFormattings().Get(idx);
fcc.AddCondition(FormatConditionType::ColorScale);
fcc.AddArea(ca);
fc = worksheet.GetConditionalFormattings().Get(idx).Get(0);
fc.GetColorScale().SetIs3ColorScale(true);
fc.GetColorScale().SetMaxColor(Color::LightBlue());
fc.GetColorScale().SetMidColor(Color::Yellow());
fc.GetColorScale().SetMinColor(Color::LightGreen());
// Save the workbook
workbook.Save(outDir + u"output_out.xlsx");
std::cout << "Conditional formatting applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}