Files
HCoreLib/IO/Excel/ExcelCellValidatorUxer.cs
T
2026-06-01 12:28:44 +09:00

61 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.IO.Excel
{
public static class ExcelCellValidatorUxer
{
/// <summary>
/// ExcelのA1形式(例: "A1", "Z99", "AA1000")かどうかを判定します。
/// </summary>
/// <param name="cellRef">セル参照文字列</param>
/// <returns>有効なA1形式ならtrue、無効ならfalse</returns>
public static bool IsValidA1Format(string cellRef)
{
if (string.IsNullOrWhiteSpace(cellRef))
return false;
// 正規表現でA1形式をチェック(列: A〜XFD、行: 1〜1048576
var match = System.Text.RegularExpressions.Regex.Match(
cellRef.ToUpperInvariant(),
@"^([A-Z]{1,3})([1-9][0-9]{0,6})$"
);
if (!match.Success)
return false;
string colPart = match.Groups[1].Value;
string rowPart = match.Groups[2].Value;
// 列がXFD(Excel最大列)以内かチェック
if (ColumnNameToNumber(colPart) > 16384)
return false;
// 行がExcel最大行以内かチェック
if (int.TryParse(rowPart, out int row))
{
return row >= 1 && row <= 1048576;
}
return false;
}
/// <summary>
/// 列名(例: "A", "Z", "AA")を列番号に変換(例: 1, 26, 27)
/// </summary>
private static int ColumnNameToNumber(string columnName)
{
int sum = 0;
foreach (char c in columnName)
{
sum *= 26;
sum += (c - 'A' + 1);
}
return sum;
}
}
}