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