Пример #1
0
        public static void RangeStringToParameters(string range, out string sheetTitleId, out int leftColumnIndex, out int topRowIndex, out int columnCount, out int rowCount)
        {
            // Get index of the first letter of the range sheet title id.
            // Always equal 0;
            int sheetTitleIdStartIndex = 0;

            // Length of the string representing range sheet title id.
            // Equal to the index of the first separator last char '!'.
            int sheetTitleIdLenght = range.LastIndexOf(@"!");

            // Get index of the first letter of the string representing left part of range.
            // Equal to the combined length of the sheet title and the first separator.
            int rangeLeftStartIndex = sheetTitleIdLenght + 1;

            // Get index of the first letter of the string representing right part of range.
            int rangeRightStartIndex = range.LastIndexOf(@":") + 1;

            // Get string representing sheet title id - part of range.
            // And assign as an out parameter.
            sheetTitleId = range.Substring(sheetTitleIdStartIndex, sheetTitleIdLenght);

            // Get string representing right/bottom - part of range.
            string rangeRightBottom = range.Substring(rangeRightStartIndex)?.ToUpper();

            // Get string representing left/top - part of range.
            string rangeLeftTop = range.Substring(rangeLeftStartIndex, rangeRightStartIndex - 1 - rangeLeftStartIndex)?.ToUpper();

            // Get index of the first letter of the string representing top part of range.
            int rangeTopStartIndex = rangeLeftTop.IndexOfAny(Numbers);

            // Get index of the first letter of the string representing bottom part of range.
            int rangeBottomStartIndex = rangeRightBottom.IndexOfAny(Numbers);

            // Get length of the string representing left part of the range.
            int rangeLeftLenght = rangeTopStartIndex;

            // Get length of the string representing right part of the range.
            int rangeRightLenght = rangeBottomStartIndex;

            // Get string representing left part of range.
            string rangeLeft = rangeLeftTop.Substring(0, rangeLeftLenght);

            // Get string representing top part of range.
            string rangeTop = rangeLeftTop.Substring(rangeTopStartIndex).TrimEnd(':');

            // Get string representing right part of range.
            string rangeRight = rangeRightBottom.Substring(0, rangeRightLenght);

            // Get string representing bottom part of range.
            string rangeBottom = rangeRightBottom.Substring(rangeRightLenght);

            // Get value of the left column index described by the provided range.
            // And assign it as an out parameter.
            leftColumnIndex = RangeTranslator.ColumnIndexerStringToIndex(rangeLeft);

            // Get value of the top column index described by the provided range.
            // And assign it as an out parameter.
            topRowIndex = RangeTranslator.RowIndexerStringToIndex(rangeTop);

            // Get value of the left column index described by the provided range.
            // And assign as an out parameter.
            int rightColumnIndex = RangeTranslator.ColumnIndexerStringToIndex(rangeRight);

            // Get value of the left column index described by the provided range.
            // And assign as an out parameter.
            int bottomRowIndex = RangeTranslator.RowIndexerStringToIndex(rangeBottom);

            // Calculate columns number. 5 2 1
            columnCount = (rightColumnIndex + 1) - leftColumnIndex;

            // Calculate row number.
            rowCount = (bottomRowIndex + 1) - topRowIndex;
        }