Пример #1
0
        private static string GetCell(R1C1 currentCell, int refRow, int refCol)
        {
            string ret = "";

            if (currentCell.hasCol)
            {
                if (currentCell.Col > 0)
                {
                    ret = $"${ExcelCellBase.GetColumnLetter(currentCell.Col)}";
                }
                else
                {
                    if (refCol + currentCell.ColOffset < 1)
                    {
                        return("#REF!");
                    }
                    ret = ExcelCellBase.GetColumnLetter(refCol + currentCell.ColOffset);
                }
            }

            if (currentCell.hasRow)
            {
                if (currentCell.Row > 0)
                {
                    ret += $"${currentCell.Row}";
                }
                else
                {
                    if (refRow + currentCell.RowOffset < 1)
                    {
                        return("#REF!");
                    }
                    ret += (refRow + currentCell.RowOffset).ToString();
                }
            }
            return(ret);
        }
Пример #2
0
        private static string FromR1C1SingleAddress(string r1C1Address, int row, int col)
        {
            R1C1   firstCell   = new R1C1();
            var    currentCell = firstCell;
            bool   isRow       = false;
            bool   isSecond    = false;
            string num         = "";

            for (int i = 0; i < r1C1Address.Length; i++)
            {
                switch (r1C1Address[i])
                {
                case 'R':
                case 'r':
                    currentCell.hasRow = true;
                    isRow = true;
                    break;

                case 'C':
                case 'c':
                    if (!string.IsNullOrEmpty(num))
                    {
                        currentCell.Row = int.Parse(num);
                        num             = "";
                    }
                    currentCell.hasCol = true;
                    isRow = false;
                    break;

                case ':':
                    if (!string.IsNullOrEmpty(num))
                    {
                        if (isRow)
                        {
                            currentCell.Row = int.Parse(num);
                        }
                        else
                        {
                            currentCell.Col = int.Parse(num);
                        }
                        num = "";
                    }
                    firstCell   = currentCell;
                    currentCell = new R1C1();
                    isSecond    = true;
                    isRow       = false;
                    break;

                case '[':
                    break;

                case ']':
                    if (isRow)
                    {
                        currentCell.RowOffset = int.Parse(num);
                    }
                    else
                    {
                        currentCell.ColOffset = int.Parse(num);
                    }
                    num = "";
                    break;

                default:
                    if ((r1C1Address[i] >= '0' && r1C1Address[i] <= '9') || r1C1Address[i] >= '-')
                    {
                        num += r1C1Address[i];
                    }
                    break;
                }
            }
            if (!string.IsNullOrEmpty(num))
            {
                if (isRow)
                {
                    currentCell.Row = int.Parse(num);
                }
                else
                {
                    currentCell.Col = int.Parse(num);
                }
            }

            if (isSecond == false)
            {
                if (currentCell.hasRow == false || currentCell.hasCol == false)
                {
                    var cell = GetCell(currentCell, row, col);
                    return($"{cell}:{cell}");
                }
                else
                {
                    return(GetCell(currentCell, row, col));
                }
            }
            else
            {
                var cell1 = GetCell(firstCell, row, col);
                var cell2 = GetCell(currentCell, row, col);
                if (cell1 == cell2)
                {
                    return(cell1);
                }
                else
                {
                    return($"{cell1}:{cell2}");
                }
            }
        }