Excel Conditional Formatting - How do I apply the conditions that are providing the fill color? #1206
Replies: 3 comments 2 replies
-
Hi @DonFrazier , Here is a link to the OOXML SDK documentation for conditional formatting: https://docs.microsoft.com/en-us/office/open-xml/working-with-conditional-formatting. There is also the I will also try to find some sample code for you. |
Beta Was this translation helpful? Give feedback.
-
Thanks. I found ConditionalFormatValueObject which looks interesting. I'll try posting on SO but find it is deteriorating in quality and increasing in bickering and petty territorial / legalistic fiefdoms. If I can find some free time I'll probably poke through the repo and hope to find a unit test with some hints or the answer. |
Beta Was this translation helpful? Give feedback.
-
This isn't totally complete, but it shows you how to get the cell id and find the differential styles that should be applied to them: using SpreadsheetDocument spd = SpreadsheetDocument.Open(path, true);
WorkbookPart? wbp = spd.WorkbookPart;
IEnumerable<ExternalWorkbookPart>? ewbp = wbp?.GetPartsOfType<ExternalWorkbookPart>();
IEnumerable<WorksheetPart>? worksheetParts = wbp?.WorksheetParts;
if (worksheetParts is not null)
{
foreach (WorksheetPart worksheetPart in worksheetParts)
{
IEnumerable<OpenXmlElement> conditionalFormatting = worksheetPart.Worksheet.ChildElements.Where(w => w is ConditionalFormatting);
foreach (ConditionalFormatting cf in conditionalFormatting)
{
// Get the cell/cell range to be styled
string? cellLocation = cf.GetAttribute("sqref", "").Value;
IEnumerable<OpenXmlElement> conditionalFormattingRules = cf.ChildElements.Where(r => r is ConditionalFormattingRule);
foreach (ConditionalFormattingRule cfr in conditionalFormattingRules)
{
// determine what operation is applied
EnumValue<ConditionalFormattingOperatorValues>? oper = cfr.Operator;
// get the index of the DifferentialFormat to apply
UInt32Value? dxfId = cfr.FormatId;
// the formula to calucate and use the operator on
OpenXmlElement? formula = cfr.ChildElements.Where(c => c is Formula).FirstOrDefault();
OpenXmlElement? differentialFormats = wbp?.WorkbookStylesPart?.Stylesheet.ChildElements.Where(s => s is DifferentialFormats).FirstOrDefault();
if (differentialFormats is not null)
{
DifferentialFormats? dxfs = differentialFormats as DifferentialFormats;
if (dxfs is not null)
{
foreach (var (df, i) in dxfs.Select((df, i) => (df, i)))
{
// if the index matches the dxfId
if (dxfId is not null && i == dxfId)
{
IEnumerable<OpenXmlElement> elems = df.Elements();
foreach (OpenXmlElement elem in elems)
{
// Each elem is a formatting option font, fill etc.
//
}
break;
}
}
}
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I have a large Excel workbook with multiple sheets. We are trying to save the workbook with the resolved values and removing the formulas. Part of the resolution includes conditionally formatting the fill color of some cells. I'm trying to find a way to get the fill color that the Excel app would use to render the cell in a window. This process is running on hundreds of sheets with no UI though.
The desired end result is a set of sheets that have no formulas, no conditional formatting, etc. but the result of calculating the value of each cell and applying the conditional style. I've gotten the value but haven't had any luck following the model to find the fill color. Any suggestions? Links, snippets, etc. all welcome.
Beta Was this translation helpful? Give feedback.
All reactions