DioDocs for Excel
DioDocs for Excel
特定のセル範囲の取得
機能 > ワークシート > 範囲の操作 > 特定のセル範囲の取得

特定のセル範囲は、指定したデータ型または値を含むセル範囲を指します。たとえば、コメント、テキスト、数式、空白、定数、数値などを含むセル。

DioDocs for Excelでは、IRangeインタフェースのSpecialCellsメソッドを使用して、特定のセル範囲を取得できます。このメソッドはパラメータとして次の列挙を受け取ります。

セルの種類で特定のセル範囲の検索

セルの種類を指定して特定のセル範囲を検索する方法については、次のサンプルコードを参照してください。

C#
コードのコピー
// ワークブックを初期化します
var workbook = new Workbook();

IWorksheet ws = workbook.ActiveSheet;

//  データを準備します
var rngA1D2 = new object[,] {
{ "Register", null, null, null},
{ "Field name", "Wildcard", "Validation error", "User input"}
};
ws.Range["$A$1:$D$2"].Value = rngA1D2;

var rngA3C6 = new object[,] {
{ "User name", "??*", "At least 2 characters"},
{ "Captcha", "?????", "5 characters required"},
{ "E-mail", "?*@?*.?*", "The format is incorrect"},
{ "Security code", "#######", "7 digits required"}
};
ws.Range["$A$3:$C$6"].Value = rngA3C6;

var rngA8D14 = new object[,] {
{ "User table", null, null, null},
{ "Id", "Name", "Email", "Banned"},
{ 1d, "User 1", "8zgnvlkp2@163.com", true},
{ 2d, "User 2", "b9fvaswb@163.com", false},
{ 3d, "User", "md78b", false},
{ 4d, "User 4", "1qasghjfg@163.com", false},
{ 5d, "U", "mncx23k8@163.com", false}
};
ws.Range["$A$8:$D$14"].Value = rngA8D14;

ws.Range["A1:D1"].Merge();
ws.Range["A1:D1"].HorizontalAlignment = HorizontalAlignment.Center;
ws.Range["A8:D8"].Merge();
ws.Range["A8:D8"].HorizontalAlignment = HorizontalAlignment.Center;

ws.Range["D3"].AddComment("Required");
ws.Range["D4"].AddComment("Required");
ws.Range["D5"].AddComment("Required");
ws.Range["D6"].AddComment("Required");

ws.Range["D10:D14"].Validation.Add(
    ValidationType.List, ValidationAlertStyle.Stop,
    ValidationOperator.Between, "True,False");

var condition = (IFormatCondition)ws.Range["C10:C14"].FormatConditions.Add(
    FormatConditionType.Expression, formula1: "=ISERROR(MATCH($B$5,C10,0))");
condition.Font.Color = Color.Red;

var condition2 = (IFormatCondition)ws.Range["B10:B14"].FormatConditions.Add(
    FormatConditionType.Expression, formula1: "=LEN(B10)<=2");
condition2.Font.Color = Color.Red;

ws.Range["4:4"].EntireRow.Hidden = true;

IRange searchScope = ws.Range["1:14"];

// コメントを検索します
var comments = searchScope.SpecialCells(SpecialCellType.Comments);

// 最後のセルを検索します
var lastCell = searchScope.SpecialCells(SpecialCellType.LastCell);

// 表示されているセルを検索します
var visible = searchScope.SpecialCells(SpecialCellType.Visible);

// 空白セルを検索します
var blanks = searchScope.SpecialCells(SpecialCellType.Blanks);

// すべての条件付き書式を検索します
var allFormatConditions = searchScope.SpecialCells(SpecialCellType.AllFormatConditions);

// すべての検証を検索します
var allValidation = searchScope.SpecialCells(SpecialCellType.AllValidation);

// セルB10と同じ条件付き書式を検索します
var sameFormatConditions = ws.Range["B10"].SpecialCells(SpecialCellType.SameFormatConditions);

// セルD10と同じ検証を検索します
var sameValidation = ws.Range["D10"].SpecialCells(SpecialCellType.SameValidation);

// 結合されたセルを検索します
var merged = searchScope.SpecialCells(SpecialCellType.MergedCells);

// 結果
ws.Range["A16"].Value = "Find result";
ws.Range["A16:C16"].Merge();
ws.Range["A16:C16"].HorizontalAlignment = HorizontalAlignment.Center;
ws.Range["$A$17:$A$25"].Value = new object[,] {
{"Comments"},
{"LastCell"},
{"Visible"},
{"Blanks"},
{"AllFormatConditions"},
{"AllValidation"},
{"SameFormatConditions B10"},
{"SameValidation D10"},
{"MergedCells"}
};
ws.Range["$C$17:$C$25"].Value = new object[,] {
{comments.Address},
{lastCell.Address},
{visible.Address},
{blanks.Address},
{allFormatConditions.Address},
{allValidation.Address},
{sameFormatConditions.Address},
{sameValidation.Address},
{merged.Address}
};

ws.UsedRange.EntireColumn.AutoFit();

// XLSXファイルに保存します
workbook.Save("specialcellsfindmiscellaneous.xlsx");

既存のファイルで特定のセル範囲の検索

既存のファイルを読み込んで、数式と定数を含む特定のセルを検索し、その背景色を変更する方法については、次のサンプルコードを参照してください。

C#
コードのコピー
// ワークブックを初期化します
var workbook = new GrapeCity.Documents.Excel.Workbook();

workbook.Open("FinancialReport.xlsx");
        
IRange cells = workbook.ActiveSheet.Cells;

// すべての数式を検索します
var allFormulas = cells.SpecialCells(SpecialCellType.Formulas);
// すべての定数を検索します
var allConstants = cells.SpecialCells(SpecialCellType.Constants);

// 検索されたセルの背景色を変更します
allFormulas.Interior.Color = Color.LightGray;
allConstants.Interior.Color = Color.DarkGray;

// XLSXファイルに保存します
workbook.Save("specialcellsinexistingfiles.xlsx");

セルの種類と値で特定のセルの検索

セルの種類と値を指定して特定のセルを検索する方法については、次のサンプルコードを参照してください。

C#
コードのコピー
// ワークブックを初期化します
var workbook = new GrapeCity.Documents.Excel.Workbook();

IWorksheet ws = workbook.ActiveSheet;

// データを設定します
ws.Range["A1"].Formula = "=\"Text \" & 1";
ws.Range["B1"].Formula = "=8*10^6";
ws.Range["C1"].Formula = "=SEARCH(A1,9)";
ws.Range["A2"].Value = "Text";
ws.Range["B2"].Value = 1;

// テキスト数式を検索します
var textFormula = ws.Cells.SpecialCells(SpecialCellType.Formulas, SpecialCellsValue.TextValues);

// 数値数式を検索します
var numberFormula = ws.Cells.SpecialCells(SpecialCellType.Formulas, SpecialCellsValue.Numbers);

// エラー数式を検索します
var errorFormula = ws.Cells.SpecialCells(SpecialCellType.Formulas, SpecialCellsValue.Errors);

// テキスト値を検索します
var textValue = ws.Cells.SpecialCells(SpecialCellType.Constants, SpecialCellsValue.TextValues);

// 数値を検索します
var numberValue = ws.Cells.SpecialCells(SpecialCellType.Constants, SpecialCellsValue.Numbers);

// 検索結果を表示します
ws.Range["A4:E5"].Value = new object[,] {
{ "Text formula", "Number Formula", "Error Formula", "Text Value", "Number Value"},
{ textFormula.Address, numberFormula.Address, errorFormula.Address, textValue.Address, numberValue.Address}
};

ws.UsedRange.EntireColumn.AutoFit();

// XLSXファイルに保存します
workbook.Save("specialcellsquickstart.xlsx");

セルの種類と値で特定のセル範囲を検索する方法については、次のサンプルコードを参照してください。さまざまな種類の特定のセルを簡単に区別するために、書式が定義されています。

C#
コードのコピー
// ワークブックを初期化します
var workbook = new GrapeCity.Documents.Excel.Workbook();

IWorksheet ws = workbook.ActiveSheet;

// データを準備します
ws.Range["$A$1:$F$1"].Value = new object[,]{
{ "Test id", "Group id", "Group item id", "New test id", "Test result", "Error code"}
};

ws.Range["$B$2:$C$2"].Value = 1d;
ws.Range["$E$2,$E$7,$E$12,$E$21,$E$27,$E$36,$E$40,$E$47:$E$48,$E$51,$E$59:$E$60,$E$70:$E$71,$E$80:$E$81,$E$88,$E$90:$E$91"].Value = "Error 80073cf9";
ws.Range["$G$1:$G$2,$I$1:$I$7,$H$8:$I$8,$A$93:$B$93,$E$93:$F$93"].Value = null;

ws.Range["$H$1:$H$7"].Value = new object[,]{
{ "Constants"}, { "Formulas"}, { "String constants"}, { "Number constants"}, { "String formulas"}, { "Number formulas"}, { "Error formulas"}
};

ws.Range["$A$2:$A$13"].Value = "Test00001";
ws.Range["$A$14:$A$67"].Value = "Test00153";
ws.Range["$A$68:$A$92"].Value = "Test05789";
ws.Range["$E$3:$E$5,$E$9:$E$11,$E$25:$E$26,$E$37:$E$38,$E$57,$E$75:$E$76,$E$86:$E$87"].Value = "Runtime Error c0000005";
ws.Range["$E$6,$E$13:$E$20,$E$28:$E$35,$E$41:$E$46,$E$52:$E$56,$E$61:$E$64,$E$72:$E$74,$E$77:$E$78,$E$82:$E$85,$E$89,$E$92"].Value = "Passed";
ws.Range["$E$8,$E$22:$E$24,$E$39,$E$49:$E$50,$E$58,$E$65:$E$69,$E$79"].Value = "Deploy Error 80073cf9";

ws.Range["$D$2:$D$92"].FormulaR1C1 = "=\"X-Test-G\" & RC[-2] & \"-I\" & RC[-1]";
ws.Range["$B$3:$B$92"].FormulaR1C1 = "=IF(RC[-1]=R[-1]C[-1],R[-1]C,R[-1]C+1)";
ws.Range["$C$3:$C$92"].FormulaR1C1 = "=IF(RC[-2]=R[-1]C[-2],R[-1]C+1,1)";
ws.Range["$F$2:$F$92"].FormulaR1C1 = "=MID(RC[-1], SEARCH(\"Error \",RC[-1])+6,8)";

Color constantBgColor;
Color formulasBgColor;
Color stringForeColor;
Color errorForeColor;
unchecked
{
    constantBgColor = Color.FromArgb((int)0xFFDDEBF7);
    formulasBgColor = Color.FromArgb((int)0xFFF2F2F2);
    stringForeColor = Color.FromArgb((int)0xFF0000C0);
}
errorForeColor = Color.DarkRed;
        

var searchScope = ws.Range["$A:$F"];

// 定数を含むセルを検索して、背景色を変更します
IRange allConsts = searchScope.SpecialCells(SpecialCellType.Constants);
allConsts.Interior.Color = constantBgColor;

// 数式を含むセルを検索して、背景色を変更します
IRange allFormulas = searchScope.SpecialCells(SpecialCellType.Formulas);
allFormulas.Interior.Color = formulasBgColor;

// テキスト定数を含むセルを検索して、背景色を変更します
IRange textConsts = searchScope.SpecialCells(
    SpecialCellType.Constants, SpecialCellsValue.TextValues);
textConsts.Font.Color = stringForeColor;

// テキスト数式を含むセルを検索して、背景色を変更します
IRange textFormulas = searchScope.SpecialCells(
    SpecialCellType.Formulas, SpecialCellsValue.TextValues);
textFormulas.Font.Color = stringForeColor;

// 数値定数を含むセルを検索して、フォントの太さを変更します
IRange numberConsts = searchScope.SpecialCells(
    SpecialCellType.Constants, SpecialCellsValue.Numbers);
numberConsts.Font.Bold = true;

// 数値数式を含むセルを検索して、フォントの太さを変更します
IRange numberFormulas = searchScope.SpecialCells(
    SpecialCellType.Formulas, SpecialCellsValue.Numbers);
numberFormulas.Font.Bold = true;

// エラー数式を含むセルを検索して、前景色とフォントスタイルを変更します
IRange errorFormulas = searchScope.SpecialCells(
    SpecialCellType.Formulas, SpecialCellsValue.Errors);
errorFormulas.Font.Color = errorForeColor;
errorFormulas.Font.Italic = true;

// セルのスタイルを設定します
ws.Range["$H$1,$H$3,$H$4"].Interior.Color = constantBgColor;
ws.Range["$H$2,$H$5:$H$7"].Interior.Color = formulasBgColor;
ws.Range["$H$3,$H$5"].Font.Color = stringForeColor;
ws.Range["$H$4,$H$6"].Font.Bold = true;
ws.Range["$H$7"].Font.Color = errorForeColor;
ws.Range["$H$7"].Font.Italic = true;

ws.UsedRange.EntireColumn.AutoFit();

// XLSXファイルに保存します
workbook.Save("specialcellsfindvaluesandformulas.xlsx");

制限

結果のセル範囲には、複数の隣接するセルが含まれている場合、DioDocs for ExcelはExcelと比較して異なる方法でセルを結合します。

たとえば、Excelで数値定数を検索すると、結果は$A$2:$C$3,$C$4:$D$4になります。

DioDocs for Excelでは、結果が$A$2:$B$3,$C$2:$C$4,$D$4になります。