We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

FormatCondition not work

Hello! I have a problem with FormatCondition. I create FormatCondition for this solution

https://support.office.com/en-us/article/Apply-shading-to-alternate-rows-in-a-worksheet-9e77c551-0bcf-4515-9497-fdbf724de9be

var area = new CellArea()
{
EndColumn = 2,
EndRow = 5,
StartColumn = 0,
StartRow = 2
};
FormatConditionCollection conds = GetFormatCondition(workbook.Worksheets[0], area);
int idx = conds.AddCondition(FormatConditionType.Expression);
FormatCondition cond = conds[idx];
cond.Formula1 = “=MOD(ROW();2)=0”;
cond.Style.BackgroundColor = Color.LightBlue;
cond.Style.Pattern = BackgroundType.Solid;
cond.Priority = 1;

//This method adds formatted conditions.
private FormatConditionCollection GetFormatCondition(Worksheet worksheet, CellArea area)
{
//Adds an empty conditional formattings
int index = worksheet.ConditionalFormattings.Add();
//Get the formatted conditions
FormatConditionCollection formatConditions = worksheet.ConditionalFormattings[index];
//Add the formatted conditions cell area.
formatConditions.AddArea(area);
//Return the formatted conditions
return formatConditions;
}

In Excel FormatCondition exist, but it not work. Cells dont change background color

Hi Tigar,


Thank you for contacting Aspose support.

Please try the case with following piece of code as it works fine (see attached resultant spreadsheet). If you compare this piece of code with yours, you will notice that the problem is with the formula.

C#

Workbook workbook = new Workbook();
Worksheet ws = workbook.Worksheets[0];
int idx = ws.ConditionalFormattings.Add();
FormatConditionCollection fcc = ws.ConditionalFormattings[idx];
CellArea ca = CellArea.CreateCellArea(“A1”, “D5”); fcc.AddArea(ca);
idx = fcc.AddCondition(FormatConditionType.Expression);
FormatCondition fc = fcc[idx];
fc.Formula1 = @"=MOD(ROW(),2)=0";
fc.Style.BackgroundColor = Color.Blue;
fc.Style.Pattern = BackgroundType.Solid;
workbook.Save(dir + “output.xlsx”);

Great! Thanks!

Hi Tigar,


Thank you for the confirmation. Please feel free to contact us back in case you need our further assistance with Aspose APIs.