Skip to content

Commit e80c0c4

Browse files
Merge pull request #436 from telerik/new-kb-retrieve-cell-color-radspreadprocessing-c4a2efdcd8f248f383435b87ffcf51ae
Added new kb article retrieve-cell-color-radspreadprocessing
2 parents b0214d5 + 2f31559 commit e80c0c4

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed
482 KB
Loading
12.2 KB
Loading
15.5 KB
Loading
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: Retrieving Themable Cell Color in RadSpreadProcessing
3+
description: Learn how to retrieve the actual cell color in RadSpreadProcessing when the color comes from the document theme.
4+
type: how-to
5+
page_title: How to Retrieve Cell Color from Theme in RadSpreadProcessing
6+
slug: retrieve-cell-color-radspreadprocessing
7+
tags: radspreadprocessing, document processing, cell color, themable color, pattern fill
8+
res_type: kb
9+
ticketid: 1656165
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.2.426| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
Let's import an Excel file with some cells formatted with color. Learn how to retrieve the cell color when the background color is set from MS Excel with a theme.
21+
22+
It is possible to set a **Standard** Color (e.g. Yellow) or a **Theme** Color (e.g. Dark Teal, Accent 1) to a cell:
23+
24+
|Standard Color|Theme Color|
25+
|----|----|
26+
|![Standard Color](images/worksheet-standard-color.png) |![Theme Color](images/worksheet-theme-color.png) |
27+
28+
The *Yellow* color will be fixed and after the changing the document's theme, it wouldn't change. However, the *Dark Teal, Accent 1* color will be changed if another theme is selected:
29+
30+
![Changing Theme Color](images/worksheet-changing-theme-color.gif)
31+
32+
This article demonstrates how to extract the color value from a cell when it is applied via a theme color.
33+
34+
## Solution
35+
36+
To retrieve the cell color in RadSpreadProcessing, especially when the color is applied through the document theme, follow these steps:
37+
38+
1. [Import the Excel document]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%}) using the appropriate format provider.
39+
2. [Access the desired cell]({%slug radspreadprocessing-working-with-cells-accessing-cells-of-worksheet%}) or range of cells.
40+
3. Check if the cell's fill is of type [PatternFill](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/working-with-cells/get-set-clear-properties#fill-property).
41+
4. Retrieve the [ThemableColor]({%slug radspreadprocessing-features-styling-document-themes%}) object from the `PatternFill`.
42+
5. Use the [GetActualValue](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/styling/document-themes#getting-actual-values) method of the `ThemableColor` object, passing in the document's theme, to get the actual color value.
43+
44+
Here is a sample code snippet demonstrating these steps:
45+
46+
```csharp
47+
string filePath = "Book1.xlsx";
48+
Workbook workbook = new Workbook();
49+
IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider();
50+
51+
using (Stream input = new FileStream(filePath, FileMode.Open))
52+
{
53+
workbook = formatProvider.Import(input);
54+
}
55+
Worksheet worksheet = workbook.Worksheets.First();
56+
CellSelection selection = worksheet.Cells[0,1];
57+
PatternFill solidPatternFill = selection.GetFill().Value as PatternFill;
58+
if (solidPatternFill != null)
59+
{
60+
PatternType type = solidPatternFill.PatternType;
61+
ThemableColor patternColor = solidPatternFill.PatternColor;
62+
Color color = patternColor.LocalValue;
63+
ThemableColor bg = solidPatternFill.BackgroundColor;
64+
Color bgcolor = bg.LocalValue;
65+
66+
Color actualColor = patternColor.GetActualValue(workbook.Theme);
67+
// The actual color is the same as Accent1 color of the colorScheme
68+
Debug.WriteLine("RGB: " + actualColor.R.ToString() + ", " + actualColor.G.ToString() + ", " + actualColor.B.ToString());
69+
}
70+
```
71+
72+
This approach ensures that even when a cell's color is derived from the document's theme, you can obtain the actual color value as displayed in the Excel file.
73+
74+
## See Also
75+
76+
- [Document Themes in RadSpreadProcessing]({%slug radspreadprocessing-features-styling-document-themes%}})
77+
- [Getting Actual Values](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/styling/document-themes#getting-actual-values)
78+
79+
---

libraries/radspreadprocessing/features/styling/cell-styles.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,4 @@ You can also remove a style from the __Styles__ collection. It is as easy as rem
215215

216216
* [Document Themes]({%slug radspreadprocessing-features-styling-document-themes%})
217217
* [Whats is a Workbook?]({%slug radspreadprocessing-working-with-workbooks-what-is-workbook%})
218+
* [Retrieving Themable Cell Color in RadSpreadProcessing]({%slug retrieve-cell-color-radspreadprocessing%})

libraries/radspreadprocessing/features/styling/document-themes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,4 @@ In order to get the actual value from __ThemableColor__ or __ThemableFontFamily_
231231
## See Also
232232

233233
* [Cell Styles]({%slug radspreadprocessing-features-styling-cell-styles%})
234+
* [Retrieving Themable Cell Color in RadSpreadProcessing]({%slug retrieve-cell-color-radspreadprocessing%})

0 commit comments

Comments
 (0)