Convert from row & column number to Excel range - Song Nghia - Microsoft Dynamics Partner

Song Nghia - Microsoft Dynamics Partner

Song Nghia - Microsoft Dynamics Partner

Breaking

Monday, December 18, 2017

Convert from row & column number to Excel range

Trainee: Song Thanh Nghia

//convert from row & column number to Excel range
//(1,1) -> A1, (1,2) -> B1
final str getConvertedCellRange(  int     _startRow,
                            int     _startColumn,
                            int     _endRow         = _startRow,    //one whole row
                            int     _endColumn      = _startColumn) //one whole colum
{
    str             startColumnStr;
    str             endColumnStr;
    ;

    if (!_startColumn || !_endColumn
        || (_startColumn > _endColumn)
        || !_startRow || !_endRow
        || (_startRow > _endRow))

        throw warning("Invalid excel range!");

    if(_startColumn <= 26)
    {
        startColumnStr = num2char(_startColumn + 64);
    }
    else
    {
        startColumnStr = num2char((_startColumn - 1) / 26 + 64) + num2char(((_startColumn - 1) mod 26) + 65);
    }

    if(_endColumn <= 26)
    {
        endColumnStr = num2char(_endColumn + 64);
    }
    else
    {
        endColumnStr = num2char((_endColumn - 1) / 26 + 64) + num2char(((_endColumn - 1) mod 26) + 65);
    }

    return strfmt("%1%2:%3%4", startColumnStr, _startRow, endColumnStr, _endRow);
}

No comments:

Post a Comment