テキスト条件付き書式の追加方法
可能な使用シナリオ
スプレッドシートでのテキストベースの条件付き書式は、特定のテキスト基準を満たすセルを強調表示するのに役立ちます。これにより、データ分析が向上し、大規模なデータセットから主要な情報を見つけやすくなります。テキスト条件付き書式を使用する理由は次のとおりです:
- 特定のテキストをハイライト:特定の語句や文字列、文字に基づいて書式を適用できます。例:“Urgent」や「Completed」などの単語を含むセルをハイライトして、タスクを簡単に区別できます。
- パターンや傾向の識別:“High”、“Medium”、“Low"などのカテゴリやステータスに基づいて作業の進捗や優先順位を視覚的に区別できます。
- エラーやデータ入力の警告:誤字や未完成のテキスト、不正な値などの不一致や誤りを示すために書式を設定できます。多くのテキスト入力を含むデータセットに特に有効です。
- 読みやすさの向上:テキストに色付けしたり、スタイルを変更したり(太字、イタリックなど)して、重要な情報を目立たせ、シート全体の可読性を向上させます。
- 動的なフィードバック:特定の条件に一致したときに書式を自動的に調整するルールを設定できます。これにより、データの変更に応じて手動で書式を更新する必要がなくなります。
要するに、テキスト条件付き書式は、関連情報、エラー、トレンドを素早く把握できるため、テキストデータの管理と解釈に役立つ強力なツールです。
Excelを使用したテキスト条件付き書式の追加方法
Excelに条件付き書式を追加するには、次の手順に従います:
- セル範囲を選択:条件付き書式を適用したいセルをハイライトします。
- 条件付き書式メニューを開く:Excelリボンのホームタブに移動します。「スタイル」グループの条件付き書式をクリックします。
- 「新しいルール」を選択:ドロップダウンメニューから「新しいルール」を選びます。
- 「セルの値を特定の条件で書式設定」を選択:新しい書式ルールダイアログで、「ルールの種類を選択してください」セクションの中から「セルの値を特定の条件で書式設定」を選びます。
- ルールの条件を設定:“セルを書式設定"セクションで、ドロップダウンから「特定のテキスト」を選択します。条件に応じて「含む」「始める」「終わる」を選び、書式設定したいテキスト(例:「Urgent」「Completed」など)を入力します。
- 書式の選択:書式ボタンをクリックします。セルの書式設定ダイアログで、フォントの色、背景色、その他の書式オプションを選択できます。
- ルールを適用:希望の書式を設定したら、「OK」をクリックしてルールを適用します。もう一度「OK」をクリックして新しい書式ルールダイアログを閉じます。
- 結果を確認:指定したテキストを含むセルに書式が適用され、関連情報を簡単に見つけやすくなります。
「Aspose.Cells for .NET」を使用したテキスト条件付き書式の追加方法
Aspose.Cellsは、Excel 2007以降の条件付き書式をXLSX形式のセル上でランタイムに完全にサポートします。これらの例は、「BeginsWith」「ContainsBlank」「ContainsText」などの高度な条件付き書式のタイプの練習例です。
指定されたテキストで始まる場合にセルの書式設定
// This method implements the BeginsWith conditional formatting type. | |
private void AddBeginWith() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E15:G16", Color.LightGoldenrodYellow, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.BeginsWith); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
cond.Text = "ab"; | |
Cell c = _sheet.Cells["E15"]; | |
c.PutValue("abc"); | |
c = _sheet.Cells["G16"]; | |
c.PutValue("babx"); | |
book.Save("BeginsWith.xlsx"); | |
} |
// This method adds formatted conditions. | |
private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet) | |
{ | |
// Adds an empty conditional formattings | |
int index = _sheet.ConditionalFormattings.Add(); | |
// Get the formatted conditions | |
FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index]; | |
// Get the cell area calling the custom GetCellAreaByName method | |
CellArea area = GetCellAreaByName(cellAreaName); | |
// Add the formatted conditions cell area. | |
formatConditions.AddArea(area); | |
// Call the custom FillCell method | |
FillCell(cellAreaName, color, _sheet); | |
// Return the formatted conditions | |
return formatConditions; | |
} | |
// This method specifies the cell shading color for the conditional formattings cellarea range. | |
private void FillCell(string cellAreaName, Color color, Worksheet _sheet) | |
{ | |
CellArea area = GetCellAreaByName(cellAreaName); | |
int k = 0; | |
for (int i = area.StartColumn; i <= area.EndColumn; i++) | |
{ | |
for (int j = area.StartRow; j <= area.EndRow; j++) | |
{ | |
Cell c = _sheet.Cells[j, i]; | |
if (!color.IsEmpty) | |
{ | |
Style s = c.GetStyle(); | |
s.ForegroundColor = color; | |
s.Pattern = BackgroundType.Solid; | |
c.SetStyle(s); | |
} | |
// Set some random values to the cells in the cellarea range | |
int value = j + i + k; | |
c.PutValue(value); | |
k++; | |
} | |
} | |
} | |
// This method specifies the CellArea range (start row, start col, end row, end col etc.) | |
// For the conditional formatting | |
internal static CellArea GetCellAreaByName(string s) | |
{ | |
CellArea area = new CellArea(); | |
string[] strCellRange = s.Replace("$", "").Split(':'); | |
int column; | |
CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column); | |
area.StartColumn = column; | |
if (strCellRange.Length == 1) | |
{ | |
area.EndRow = area.StartRow; | |
area.EndColumn = area.StartColumn; | |
} | |
else | |
{ | |
CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column); | |
area.EndColumn = column; | |
} | |
return area; | |
} |
空白を含む場合にセルの書式設定
// This method implements the ContainsBlank conditional formatting type. | |
private void AddContainsBlank() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E9:G10", Color.LightBlue, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.ContainsBlanks); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Yellow; | |
cond.Style.Pattern = BackgroundType.Solid; | |
Cell c = _sheet.Cells["E9"]; | |
c.PutValue(" "); | |
c = _sheet.Cells["G10"]; | |
c.PutValue(" "); | |
book.Save("ContainsBlank.xlsx"); | |
} |
エラーを含む場合にセルの書式設定
// This method implements the ContainsErrors conditional formatting type. | |
private void AddContainsErrors() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E17:G18", Color.LightSkyBlue, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.ContainsErrors); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Yellow; | |
cond.Style.Pattern = BackgroundType.Solid; | |
Cell c = _sheet.Cells["E17"]; | |
c.PutValue(" "); | |
c = _sheet.Cells["G18"]; | |
c.PutValue(" "); | |
book.Save("ContainsErrors.xlsx"); | |
} |
特定のテキストを含む場合にセルの書式設定
// This method implements the ContainsText conditional formatting type. | |
private void AddContainsText() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E5:G6", Color.LightBlue, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.ContainsText); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Yellow; | |
cond.Style.Pattern = BackgroundType.Solid; | |
cond.Text = "1"; | |
book.Save("ContainsText.xlsx"); | |
} | |
重複する値を含む場合にセルの書式設定
// This method implements the DuplicateValues conditional formatting type. | |
private void AddDuplicateValues() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E23:G24", Color.LightSlateGray, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.DuplicateValues); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
Cell c = _sheet.Cells["E23"]; | |
c.PutValue("bb"); | |
c = _sheet.Cells["G24"]; | |
c.PutValue("bb"); | |
book.Save("DuplicateValues.xlsx"); | |
} |
指定されたテキストで終わる場合にセルの書式設定
// This method implements the EndsWith conditional formatting type. | |
private void AddEndWith() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E13:G14", Color.LightGray, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.EndsWith); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Yellow; | |
cond.Style.Pattern = BackgroundType.Solid; | |
cond.Text = "ab"; | |
Cell c = _sheet.Cells["E13"]; | |
c.PutValue("nnnab"); | |
c = _sheet.Cells["G14"]; | |
c.PutValue("mmmabc"); | |
book.Save("EndsWith.xlsx"); | |
} |
空白を含まない場合にセルの書式設定
// This method implements the NotContainsBlank conditional formatting type. | |
private void AddNotContainsBlank() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E11:G12", Color.LightCoral, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.NotContainsBlanks); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
Cell c = _sheet.Cells["E11"]; | |
c.PutValue("abc"); | |
c = _sheet.Cells["G12"]; | |
c.PutValue(" "); | |
book.Save("NotContainsBlank.xlsx"); | |
} |
エラーを含まない場合にセルの書式設定
// This method implements the NotContainsErrors conditional formatting type. | |
private void AddNotContainsErrors() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E19:G20", Color.LightSeaGreen, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.NotContainsErrors); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
Cell c = _sheet.Cells["E19"]; | |
c.PutValue(" "); | |
c = _sheet.Cells["G20"]; | |
c.PutValue(" "); | |
book.Save("NotContainsErrors.xlsx"); | |
} |
指定されたテキストを含まない場合にセルの書式設定
// This method implements the NotContainsText conditional formatting type. | |
private void AddNotContainsText() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E7:G8", Color.LightCoral, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.NotContainsText); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
cond.Text = "3"; | |
book.Save("NotContainsText.xlsx"); | |
} |
ユニークな値を含む場合にセルの書式設定
// This method implements the UniqueValues conditional formatting type. | |
private void AddUniqueValues() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
FormatConditionCollection conds = GetFormatCondition("E21:G22", Color.LightSalmon, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.UniqueValues); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Yellow; | |
cond.Style.Pattern = BackgroundType.Solid; | |
Cell c = _sheet.Cells["E21"]; | |
c.PutValue("aa"); | |
c = _sheet.Cells["G22"]; | |
c.PutValue("aa"); | |
book.Save("UniqueValues.xlsx"); | |
} |