Excel 中的条件格式

Microsoft Office Excel 允许您以电子表格或工作簿格式布局文本数据。您可以将来自不同文件和位置的信息和数据带到一个目的地,以便在一个文件中对其进行处理和分析。在 excel 工作表中定义各种颜色阴影、粗体、斜体以突出显示重要数据您可以选择合适的配色方案进行快速分析。下面列出了 excel 文件的常见用途

  • 数据输入和存储
  • 业务数据的收集和验证
  • 行政和管理职责
  • 会计和预算
  • 数据分析
  • 报告 + 可视化
  • 预测

条件格式

条件格式是许多电子表格应用程序中的一项功能。它允许您将特定格式应用于满足特定条件的单元格。它最常用作基于颜色的格式,以突出显示、强调或区分电子表格中存储的数据和信息。如果条件为假,则单元格范围不会被格式化。例如,您可以将千以下的数字显示为红色,将千以上的数字显示为蓝色。

以下是可用的流行条件格式可视化

  • (单元格的)背景颜色阴影
  • 前景色阴影(字体)
  • 数据条
  • 图标(有 4 种不同的图像类型)
  • 价值观

下面将更详细地阐述指定主题

获取工作表的条件格式

Aspose.Cells Cloud 提供了从 excel 工作表中读取条件格式信息的功能。API 的 GetWorksheetConditionalFormattings 方法使您能够从 excel 工作表中读取格式详细信息。第一步是根据您的个性化客户端 ID 和客户端密钥生成 JWT 令牌。有关更多信息,请访问 如何使用客户端 ID 和客户端密钥获取 JWT 令牌

cURL 命令如下所示,它有助于从 Excel 工作表中读取格式详细信息

curl -X GET "https://api.aspose.cloud/v3.0/cells/conditional.xlsx/worksheets/Sheet1/conditionalFormattings" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

.NET 教程

// 完整示例和数据文件请见https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet/
CellsApi instance = new CellsApi(clientId, clientSecret, apiVersion, testbaseurl);
string name = BOOK1;
string sheetName = SHEET1;
string folder = TEMPFOLDER;
instance.UploadFile(folder + @"\" + name, File.Open( @"C:\TestData\" +name));
var response = instance.CellsConditionalFormattingsGetWorksheetConditionalFormattings(name, sheetName, folder);
Assert.IsInstanceOf<ConditionalFormattingsResponse>(response, "response is ConditionalFormattingsResponse");
Assert.AreEqual(response.Code, 200);

添加条件格式

Microsoft Excel 提供了三种预设,分别为条件格式、数据条、颜色标度和图标集,而 Aspose.Cells Cloud API 支持所有这些类型。指定条件的不同参数,包括类型、运算符、样式、单元格区域等。

cURL 命令

curl -X PUT "https://api.aspose.cloud/v3.0/cells/conditional.xlsx/worksheets/Sheet1/conditionalFormattings?cellArea=C4%3AC6" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{ \"link\": { \"Href\": \"string\", \"Rel\": \"string\", \"Title\": \"string\", \"Type\": \"string\" }, \"Priority\": 2, \"Type\": \"CellValue\", \"StopIfTrue\": true, \"AboveAverage\": { \"IsAboveAverage\": true, \"IsEqualAverage\": true, \"StdDev\": 0 }, \"ColorScale\": { \"MaxCfvo\": { \"IsGTE\": true, \"Type\": \"string\" }, \"MaxColor\": { \"A\": 100, \"R\": 200, \"G\": 200, \"B\": 0 }, \"MidCfvo\": { \"IsGTE\": true, \"Type\": \"string\" }, \"MidColor\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 }, \"MinCfvo\": { \"IsGTE\": true, \"Type\": \"string\" }, \"MinColor\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 } }, \"DataBar\": { \"AxisColor\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 }, \"AxisPosition\": \"string\", \"BarBorder\": { \"Color\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 }, \"Type\": \"string\" }, \"BarFillType\": \"string\", \"Color\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 }, \"Direction\": \"string\", \"MaxCfvo\": { \"IsGTE\": true, \"Type\": \"string\" }, \"MaxLength\": 0, \"MinCfvo\": { \"IsGTE\": true, \"Type\": \"string\" }, \"MinLength\": 0, \"NegativeBarFormat\": { \"BorderColor\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 }, \"BorderColorType\": \"string\", \"Color\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 }, \"ColorType\": \"string\" }, \"ShowValue\": true }, \"Formula1\": \"string\", \"Formula2\": \"string\", \"IconSet\": { \"CfIcons\": [ { \"ImageData\": \"string\", \"Index\": 0, \"Type\": \"string\" } ], \"Cfvos\": [ { \"IsGTE\": true, \"Type\": \"string\" } ], \"IsCustom\": true, \"Reverse\": true, \"ShowValue\": true, \"IconSetType\": \"string\" }, \"Operator\": \"string\", \"Style\": { \"link\": { \"Href\": \"string\", \"Rel\": \"string\", \"Title\": \"string\", \"Type\": \"string\" }, \"Font\": { \"Color\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 }, \"DoubleSize\": 0, \"IsBold\": true, \"IsItalic\": true, \"IsStrikeout\": true, \"IsSubscript\": true, \"IsSuperscript\": true, \"Name\": \"string\", \"Size\": 0, \"Underline\": \"string\" }, \"Name\": \"string\", \"CultureCustom\": \"string\", \"Custom\": \"string\", \"BackgroundColor\": { \"A\": 220, \"R\": 10, \"G\": 120, \"B\": 0 }, \"ForegroundColor\": { \"A\": 0, \"R\": 0, \"G\": 0, \"B\": 0 }, \"IsFormulaHidden\": true, \"IsDateTime\": true, \"IsTextWrapped\": true, \"IsGradient\": true, \"IsLocked\": true, \"IsPercent\": true, \"ShrinkToFit\": true, \"IndentLevel\": 0, \"Number\": 0, \"RotationAngle\": 0, \"Pattern\": \"None\", \"TextDirection\": \"Context\", \"VerticalAlignment\": \"Center\", \"HorizontalAlignment\": \"General\", \"BorderCollection\": [ { \"LineStyle\": \"string\", \"Color\": { \"A\": 120, \"R\": 0, \"G\": 0, \"B\": 0 }, \"BorderType\": \"BottomBorder\", \"ThemeColor\": { \"ColorType\": \"string\", \"Tint\": 0 }, \"ArgbColor\": -16777216 } ], \"BackgroundThemeColor\": { \"ColorType\": \"string\", \"Tint\": 0 }, \"ForegroundThemeColor\": { \"ColorType\": \"string\", \"Tint\": 0 } }, \"Text\": \"string\", \"TimePeriod\": \"string\", \"Top10\": { \"IsBottom\": true, \"IsPercent\": true, \"Rank\": 0 }}"

下面给出的 C# 代码片段在 Excel 工作表中添加条件格式:

.NET 教程

// 从 https://dashboard.aspose.cloud/ 获取 AppKey 和 AppSID
// 安装 Nuget 包 Aspose.Cells-Cloud
// 完整示例和数据文件请见https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet

CellsApi cellsApi = new CellsApi(AppKey, AppSid);
string name = "BOOK1.xlsx";
string sheetName = "SHEET1";
string cellArea = "A1:C10";
FormatCondition formatcondition = new FormatCondition();
formatcondition.Type = "CellValue";
formatcondition.Operator = "Between";
formatcondition.Formula1 = "10";
formatcondition.Formula2 = "20";
var color = new Color();
color.A = 1;
color.R = 132;
color.G = 157;
color.B = 204;
Style style = new Style();
style.BackgroundColor = color;
formatcondition.Style = style;
string folder = "TEMPFOLDER";
var response = cellsApi.CellsConditionalFormattingsPutWorksheetConditionalFormatting(name, sheetName, cellArea, formatcondition, folder);

清除所有条件格式

Aspose.Cells Cloud 使您能够清除条件格式。 DeleteWorksheetConditionalFormattings 方法为您提供了从 Excel 电子表格中清除条件格式的功能。下面给出的 cURL 命令可用于满足此要求

cURL 命令

curl -X DELETE "https://api.aspose.cloud/v3.0/cells/conditional.xlsx/worksheets/Sheet1/conditionalFormattings" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>"

.NET 教程

// 完整示例和数据文件请见https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet/
CellsApi instance = new CellsApi(clientId, clientSecret, apiVersion, testbaseurl);
string name = BOOK1;
string sheetName = SHEET1;
string folder = TEMPFOLDER;
instance.UploadFile(folder + @"\" + name, File.Open( @"C:\TestData\" +name));
var response = instance.CellsConditionalFormattingsDeleteWorksheetConditionalFormattings(name, sheetName, folder);
Assert.IsInstanceOf<CellsCloudResponse>(response, "response is CellsCloudResponse");
Assert.AreEqual(response.Code, 200);

上述方法删除了所有条件格式,如果您想要删除任何特定格式,请尝试使用 DeleteWorksheetConditionalFormatting 方法。此方法接受 Excel 电子表格中格式化条件的索引号。

以下命令删除索引 1 处的条件格式(索引值从 0 开始)。

cURL 命令

curl -X DELETE "https://api.aspose.cloud/v3.0/cells/conditional.xlsx/worksheets/Sheet1/conditionalFormattings/1" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>"
格式已从工作表中删除。

图 1:从 Excel 工作表中删除格式。

删除特定区域的条件格式

如果您需要从特定区域删除条件格式,请使用 DeleteWorksheetConditionalFormattingArea 方法。我们需要指定 startRow、startColumn、totalRows 和 totalColumns 信息来完成要求。请注意,行和列索引从 0 开始。

cURL 命令

curl -X DELETE "https://api.aspose.cloud/v3.0/cells/conditional.xlsx/worksheets/Sheet1/conditionalFormattings/area?startRow=4&startColumn=1&totalRows=3&totalColumns=1" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>"

结论

Aspose.Cells Cloud API 是一款出色的产品,它提供了创建、操作和将 MS ExcelOpenOffice sheet 文件转换为其他支持格式的功能。无需安装产品。

欲了解更多信息,请访问Aspose.Cells Cloud 主页。有关特定编程语言 SDK 的安装和配置信息,请访问如何安装 Aspose.Cloud SDKs