CPF/CPF.ReoGrid/RGUtility.cs
2024-06-24 10:15:59 +08:00

216 lines
5.6 KiB
C#

using System;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
namespace CPF.ReoGrid
{
public static class RGUtility
{
public static string GetAlphaChar(long a)
{
char[] array = new char[10];
int num = 9;
while (a >= 26L)
{
array[num] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[(int)(a % 26L)];
a = a / 26L - 1L;
num--;
}
array[num] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[(int)(a % 26L)];
return new string(array, num, 10 - num);
}
public static int GetNumberOfChar(string address)
{
bool flag;
if (!string.IsNullOrEmpty(address) && address.Length >= 1)
{
flag = address.Any((char c) => c < 'A' || c > 'Z');
}
else
{
flag = true;
}
bool flag2 = flag;
if (flag2)
{
throw new ArgumentException("cannot convert into number of index from empty address", "id");
}
int num = (int)(address[0] - "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[0] + '\u0001');
for (int i = 1; i < address.Length; i++)
{
num *= 26;
num += (int)(address[i] - "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[0] + '\u0001');
}
return num - 1;
}
public static bool IsValidAddress(string address)
{
return CellPosition.IsValidAddress(address) || RangePosition.IsValidAddress(address);
}
public static bool IsValidName(string name)
{
return RGUtility.NameRegex.IsMatch(name);
}
public static object[,] ParseTabbedString(string str)
{
int num = 0;
int num2 = 0;
string[] array = str.Split(new string[]
{
"\n"
}, StringSplitOptions.None);
int num3 = array.Length;
while (string.IsNullOrEmpty(array[num3 - 1]))
{
num3--;
}
for (int i = 0; i < num3; i++)
{
string text = array[i];
bool flag = text.EndsWith("\n");
if (flag)
{
text = text.Substring(0, text.Length - 1);
}
bool flag2 = text.EndsWith("\r");
if (flag2)
{
text = text.Substring(0, text.Length - 1);
}
string[] array2 = text.Split(new char[]
{
'\t'
});
num2 = Math.Max(num2, array2.Length);
num++;
}
object[,] array3 = new object[num, num2];
for (int j = 0; j < array.Length; j++)
{
string text2 = array[j];
bool flag3 = text2.EndsWith("\n");
if (flag3)
{
text2 = text2.Substring(0, text2.Length - 1);
}
bool flag4 = text2.EndsWith("\r");
if (flag4)
{
text2 = text2.Substring(0, text2.Length - 1);
}
bool flag5 = text2.Length > 0;
if (flag5)
{
string[] array4 = text2.Split(new char[]
{
'\t'
});
num2 = Math.Max(num2, array4.Length);
for (int k = 0; k < array4.Length; k++)
{
string text3 = array4[k];
bool flag6 = text3.StartsWith("\"") && text3.EndsWith("\"");
if (flag6)
{
text3 = text3.Substring(1, text3.Length - 2);
}
array3[j, k] = text3;
}
num++;
}
}
return array3;
}
public static string ToAddress(int row, int col, int absNum)
{
return RGUtility.ToAddress(row, col, 1, 1, absNum, false);
}
public static string ToAddress(int row, int col, bool a1style)
{
return RGUtility.ToAddress(row, col, 1, 1, a1style);
}
public static string ToAddress(int row, int col, int absNum, bool a1style)
{
return RGUtility.ToAddress(row, col, 1, 1, 0, a1style);
}
public static string ToAddress(int row, int col, int rows = 1, int cols = 1, bool a1style = true)
{
return RGUtility.ToAddress(row, col, rows, cols, 0, a1style);
}
public static string ToAddress(int row, int col, int rows, int cols, int absNum, bool a1style)
{
bool flag = rows <= 1 && cols <= 1;
string result;
if (flag)
{
if (a1style)
{
result = RGUtility.GetAlphaChar((long)col) + (row + 1).ToString();
}
else
{
switch (absNum)
{
default:
result = string.Format("R{0}C{1}", row, col);
break;
case 2:
result = string.Format("R{0}C[{1}]", row, col);
break;
case 3:
result = string.Format("R[{0}]C{1}", row, col);
break;
case 4:
result = string.Format("R[{0}]C[{1}]", row, col);
break;
}
}
}
else
{
int num = row + rows - 1;
int num2 = col + cols - 1;
result = RGUtility.ToAddress(row, col, absNum, a1style) + ":" + RGUtility.ToAddress(row, col, absNum, a1style);
}
return result;
}
public static string ToSingleAddressIfPossible(Worksheet sheet, RangePosition range)
{
return sheet.IsMergedCell(range) ? range.StartPos.ToAddress() : range.ToAddress();
}
public static Process OpenFileOrLink(string url, string args = null)
{
return Process.Start(new ProcessStartInfo(url)
{
UseShellExecute = true,
Arguments = args,
Verb = "open"
});
}
internal static readonly Regex CellReferenceRegex = new Regex("^\\s*(?:(?<abs_col>\\$)?(?<col>[A-Z]+)(?<abs_row>\\$)?(?<row>[0-9]+))\\s*$", RegexOptions.Compiled | RegexOptions.Singleline);
internal static readonly Regex RangeReferenceRegex = new Regex("^\\s*(?:(?<abs_from_col>\\$)?(?<from_col>[A-Z]+)(?<abs_from_row>\\$)?(?<from_row>[0-9]+):(?<abs_to_col>\\$)?(?<to_col>[A-Z]+)(?<abs_to_row>\\$)?(?<to_row>[0-9]+))\\s*$", RegexOptions.Compiled | RegexOptions.Singleline);
internal static readonly Regex SingleAbsoulteRangeRegex = new Regex("^\\s*(((?<abs_from_row>\\$)?(?<from_row>[0-9]+):(?<abs_to_row>\\$)?(?<to_row>[0-9]+))|((?<abs_from_col>\\$)?(?<from_col>[A-Z]+):(?<abs_to_col>\\$)?(?<to_col>[A-Z]+)))\\s*$", RegexOptions.Compiled | RegexOptions.Singleline);
internal static readonly Regex NameRegex = new Regex("^\\s*[A-Za-z0-9_$]+\\s*$", RegexOptions.Compiled | RegexOptions.Singleline);
private const string AlphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const int AlphaCharsLength = 26;
}
}