<commit_before>/**
 * Created with JetBrains WebStorm.
 * User: Dmitry.Shahtanov
 * Date: 27.06.13
 * Time: 15:21
 * To change this template use File | Settings | File Templates.
 */
cFormulaFunction.LookupAndReference = {
    'groupName':"LookupAndReference",
    'ADDRESS':function () {
        var r = new cBaseFunction( "ADDRESS" );
        r.setArgumentsMin( 2 );
        r.setArgumentsMax( 5 );
        r.Calculate = function ( arg ) {

            function _getColumnTitle( col ) {
                var q = col < 26 ? undefined : Asc.floor( col / 26 ) - 1;
                var r = col % 26;
                var text = String.fromCharCode( ("A").charCodeAt( 0 ) + r );
                return col < 26 ? text : _getColumnTitle( q ) + text;
            }

            function _getRowTitle( row ) {
                return "" + (row + 1);
            }

            var rowNumber = arg[0], colNumber = arg[1],
                refType = arg[2] ? arg[2] : new cNumber( 1 ),
                A1RefType = arg[3] ? arg[3] : new cBool( true ),
                sheetName = arg[4] ? arg[4] : new cEmpty();

            if ( rowNumber instanceof cArea || rowNumber instanceof cArea3D ) {
                rowNumber = rowNumber.cross( arguments[1].first );
            }
            else if ( rowNumber instanceof cArray ) {
                rowNumber = rowNumber.getElementRowCol( 0, 0 );
            }

            if ( colNumber instanceof cArea || colNumber instanceof cArea3D ) {
                colNumber = colNumber.cross( arguments[1].first );
            }
            else if ( colNumber instanceof cArray ) {
                colNumber = colNumber.getElementRowCol( 0, 0 );
            }

            if ( refType instanceof cArea || refType instanceof cArea3D ) {
                refType = refType.cross( arguments[1].first );
            }
            else if ( refType instanceof cArray ) {
                refType = refType.getElementRowCol( 0, 0 );
            }

            if ( A1RefType instanceof cArea || A1RefType instanceof cArea3D ) {
                A1RefType = A1RefType.cross( arguments[1].first );
            }
            else if ( A1RefType instanceof cArray ) {
                A1RefType = A1RefType.getElementRowCol( 0, 0 );
            }

            if ( sheetName instanceof cArea || sheetName instanceof cArea3D ) {
                sheetName = sheetName.cross( arguments[1].first );
            }
            else if ( sheetName instanceof cArray ) {
                sheetName = sheetName.getElementRowCol( 0, 0 );
            }

            rowNumber = rowNumber.tocNumber();
            colNumber = colNumber.tocNumber();
            refType = refType.tocNumber();
            A1RefType = A1RefType.tocBool();

            if ( rowNumber instanceof cError ) return this.value = rowNumber;
            if ( colNumber instanceof cError ) return this.value = colNumber;
            if ( refType instanceof cError ) return this.value = refType;
            if ( A1RefType instanceof cError ) return this.value = A1RefType;
            if ( sheetName instanceof cError ) return this.value = sheetName;

            if ( refType.getValue() > 4 && refType.getValue() < 1 || rowNumber.getValue() < 1 || colNumber.getValue() < 1 ) {
                return this.value = new cError( cErrorType.not_numeric );
            }
            var strRef;
            switch ( refType.getValue() ) {
                case 1:
                    strRef = "$" + _getColumnTitle( colNumber.getValue() - 1 ) + "$" + _getRowTitle( rowNumber.getValue() - 1 );
                    break;
                case 2:
                    strRef = _getColumnTitle( colNumber.getValue() - 1 ) + "$" + _getRowTitle( rowNumber.getValue() - 1 );
                    break;
                case 3:
                    strRef = "$" + _getColumnTitle( colNumber.getValue() - 1 ) + _getRowTitle( rowNumber.getValue() - 1 );
                    break;
                case 4:
                    strRef = _getColumnTitle( colNumber.getValue() - 1 ) + _getRowTitle( rowNumber.getValue() - 1 );
                    break;
            }

            if ( sheetName instanceof cEmpty ) {
                return this.value = new cString( strRef );
            }
            else {
                if ( !rx_test_ws_name.test( sheetName.toString() ) ) {
                    return this.value = new cString( "'" + sheetName.toString().replace( /'/g, "''" ) + "'" + "!" + strRef );
                }
                else {
                    return this.value = new cString( sheetName.toString() + "!" + strRef ) ;
                }
            }

        }
        r.getInfo = function () {
            return {
                name:this.name,
                args:"( row-number , col-number [ , [ ref-type ] [ , [ A1-ref-style-flag ] [ , sheet-name ] ] ] )"
            };
        }
        return r;
    },
    'AREAS':function () {
        var r = new cBaseFunction( "AREAS" );
        return r;
    },
    'CHOOSE':function () {
        var r = new cBaseFunction( "CHOOSE" );
        r.setArgumentsMin( 2 );
        r.setArgumentsMax( 30 );
        r.Calculate = function ( arg ) {
            var arg0 = arg[0];

            if ( arg0 instanceof cArea || arg0 instanceof cArea3D ) {
                arg0 = arg0.cross( arguments[1].first );
            }
            arg0 = arg0.tocNumber();

            if ( arg0 instanceof cError ) {
                return this.value = arg0;
            }

            if ( arg0 instanceof cNumber ) {
                if ( arg0.getValue() < 1 || arg0.getValue() > this.getArguments() ) {
                    return this.value = new cError( cErrorType.wrong_value_type );
                }

                return this.value = arg[arg0.getValue()];
            }

            return this.value = new cError( cErrorType.wrong_value_type );
        }
        r.getInfo = function () {
            return {
                name:this.name,
                args:"( index , argument-list )"
            };
        }
        return r;
    },
    'COLUMN':function () {
        var r = new cBaseFunction( "COLUMN" );
        r.setArgumentsMin( 0 );
        r.setArgumentsMax( 1 );
        r.Calculate = function ( arg ) {
            var arg0;
            if ( this.argumentsCurrent == 0 ) {
                arg0 = arguments[1];
                return this.value = new cNumber( arg0.getFirst().getCol() );
            }
            arg0 = arg[0];
            if ( arg0 instanceof cRef || arg0 instanceof cRef3D || arg0 instanceof cArea ) {
                var range = arg0.getRange();
                if ( range )
                    return this.value = new cNumber( range.getFirst().getCol() );
                else
                    return this.value = new cError( cErrorType.bad_reference );
            }
            else if ( arg0 instanceof cArea3D ) {
                var r = arg0.getRange();
                if ( r && r[0] && r[0].getFirst() ) {
                    return this.value = new cNumber( r[0].getFirst().getCol() );
                }
                else {
                    return this.value = new cError( cErrorType.bad_reference );
                }
            }
            else
                return this.value = new cError( cErrorType.bad_reference );
        }
        r.getInfo = function () {
            return {
                name:this.name,
                args:"( [ reference ] )"
            };
        }
        return r;
    },
    'COLUMNS':function () {
        var r = new cBaseFunction( "COLUMNS" );
        r.setArgumentsMin( 1 );
        r.setArgumentsMax( 1 );
        r.Calculate = function ( arg ) {
            var arg0 = arg[0];
            if ( arg0 instanceof cArray ) {
                return this.value = new cNumber( arg0.getCountElementInRow() );
            }
            else if ( arg0 instanceof cArea || arg0 instanceof cRef || arg0 instanceof cRef3D ) {
                var range = arg0.getRange();
                return this.value = new cNumber( Math.abs( range.getBBox().c1 - range.getBBox().c2 ) + 1 );
            }
            else if ( arg0 instanceof cArea3D ) {
                var range = arg0.getRange();
                if ( range.length > 1 )
                    return this.value = new cError( cErrorType.wrong_value_type );

                return this.value = new cNumber( Math.abs( range[0].getBBox().c1 - range[0].getBBox().c2 ) + 1 );
            }
            else
                return this.value = new cError( cErrorType.wrong_value_type );
        }
        r.getInfo = function () {
            return {
                name:this.name,
                args:"( array )"
            };
        }
        return r;
    },
    'GETPIVOTDATA':function () {
        var r = new cBaseFunction( "GETPIVOTDATA" );
        return r;
    },
    'HLOOKUP':function () {
        var r = new cBaseFunction( "HLOOKUP" );
        r.setArgumentsMin( 3 );
        r.setArgumentsMax( 4 );
        r.Calculate = function ( arg ) {
            var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arg3 = this.argumentsCurrent == 4 ? arg[3].tocBool() : new cBool( true );
            var numberRow = arg2.getValue() - 1, valueForSearching = arg0.getValue(), resC = -1, min, regexp;

            if ( isNaN( numberRow ) )
                return this.value = new cError( cErrorType.bad_reference );

            if ( numberRow < 0 )
                return this.value = new cError( cErrorType.wrong_value_type );

            if ( arg0 instanceof cString ) {
                valueForSearching = arg0.getValue();
                regexp = searchRegExp(valueForSearching);
            }
            else if ( arg0 instanceof cError )
                return this.value = arg0;
            else {
                valueForSearching = arg0.getValue();
            }

            var found = false, bb,
                f = function ( cell, r, c, r1, c1 ) {
                    if ( r == r1 ) {
                        var cv = cell.getValueWithoutFormat(), cvType = checkTypeCell(cv);
                        if ( c == c1 )
                            min = cv;
                        if ( arg3.value == true ) {
                            if ( arg0 instanceof cString ) {
                                if ( cvType instanceof cString ){
                                    if( valueForSearching.localeCompare( cvType.getValue() ) == 0 ){
                                        resC = r;
                                        found = true;
                                    }
                                    else if( valueForSearching.localeCompare( cvType.getValue() ) == 1 && !found ){
                                        resC = r;
                                    }<commit_msg>fix: Bug 19868 - Некорректно ищется аргумент, заданный текстом для формулы VLOOKUP/HLOOKUP неверно брался индекс столбца для поиска.<commit_after>/**
 * Created with JetBrains WebStorm.
 * User: Dmitry.Shahtanov
 * Date: 27.06.13
 * Time: 15:21
 * To change this template use File | Settings | File Templates.
 */
cFormulaFunction.LookupAndReference = {
    'groupName':"LookupAndReference",
    'ADDRESS':function () {
        var r = new cBaseFunction( "ADDRESS" );
        r.setArgumentsMin( 2 );
        r.setArgumentsMax( 5 );
        r.Calculate = function ( arg ) {

            function _getColumnTitle( col ) {
                var q = col < 26 ? undefined : Asc.floor( col / 26 ) - 1;
                var r = col % 26;
                var text = String.fromCharCode( ("A").charCodeAt( 0 ) + r );
                return col < 26 ? text : _getColumnTitle( q ) + text;
            }

            function _getRowTitle( row ) {
                return "" + (row + 1);
            }

            var rowNumber = arg[0], colNumber = arg[1],
                refType = arg[2] ? arg[2] : new cNumber( 1 ),
                A1RefType = arg[3] ? arg[3] : new cBool( true ),
                sheetName = arg[4] ? arg[4] : new cEmpty();

            if ( rowNumber instanceof cArea || rowNumber instanceof cArea3D ) {
                rowNumber = rowNumber.cross( arguments[1].first );
            }
            else if ( rowNumber instanceof cArray ) {
                rowNumber = rowNumber.getElementRowCol( 0, 0 );
            }

            if ( colNumber instanceof cArea || colNumber instanceof cArea3D ) {
                colNumber = colNumber.cross( arguments[1].first );
            }
            else if ( colNumber instanceof cArray ) {
                colNumber = colNumber.getElementRowCol( 0, 0 );
            }

            if ( refType instanceof cArea || refType instanceof cArea3D ) {
                refType = refType.cross( arguments[1].first );
            }
            else if ( refType instanceof cArray ) {
                refType = refType.getElementRowCol( 0, 0 );
            }

            if ( A1RefType instanceof cArea || A1RefType instanceof cArea3D ) {
                A1RefType = A1RefType.cross( arguments[1].first );
            }
            else if ( A1RefType instanceof cArray ) {
                A1RefType = A1RefType.getElementRowCol( 0, 0 );
            }

            if ( sheetName instanceof cArea || sheetName instanceof cArea3D ) {
                sheetName = sheetName.cross( arguments[1].first );
            }
            else if ( sheetName instanceof cArray ) {
                sheetName = sheetName.getElementRowCol( 0, 0 );
            }

            rowNumber = rowNumber.tocNumber();
            colNumber = colNumber.tocNumber();
            refType = refType.tocNumber();
            A1RefType = A1RefType.tocBool();

            if ( rowNumber instanceof cError ) return this.value = rowNumber;
            if ( colNumber instanceof cError ) return this.value = colNumber;
            if ( refType instanceof cError ) return this.value = refType;
            if ( A1RefType instanceof cError ) return this.value = A1RefType;
            if ( sheetName instanceof cError ) return this.value = sheetName;

            if ( refType.getValue() > 4 && refType.getValue() < 1 || rowNumber.getValue() < 1 || colNumber.getValue() < 1 ) {
                return this.value = new cError( cErrorType.not_numeric );
            }
            var strRef;
            switch ( refType.getValue() ) {
                case 1:
                    strRef = "$" + _getColumnTitle( colNumber.getValue() - 1 ) + "$" + _getRowTitle( rowNumber.getValue() - 1 );
                    break;
                case 2:
                    strRef = _getColumnTitle( colNumber.getValue() - 1 ) + "$" + _getRowTitle( rowNumber.getValue() - 1 );
                    break;
                case 3:
                    strRef = "$" + _getColumnTitle( colNumber.getValue() - 1 ) + _getRowTitle( rowNumber.getValue() - 1 );
                    break;
                case 4:
                    strRef = _getColumnTitle( colNumber.getValue() - 1 ) + _getRowTitle( rowNumber.getValue() - 1 );
                    break;
            }

            if ( sheetName instanceof cEmpty ) {
                return this.value = new cString( strRef );
            }
            else {
                if ( !rx_test_ws_name.test( sheetName.toString() ) ) {
                    return this.value = new cString( "'" + sheetName.toString().replace( /'/g, "''" ) + "'" + "!" + strRef );
                }
                else {
                    return this.value = new cString( sheetName.toString() + "!" + strRef ) ;
                }
            }

        }
        r.getInfo = function () {
            return {
                name:this.name,
                args:"( row-number , col-number [ , [ ref-type ] [ , [ A1-ref-style-flag ] [ , sheet-name ] ] ] )"
            };
        }
        return r;
    },
    'AREAS':function () {
        var r = new cBaseFunction( "AREAS" );
        return r;
    },
    'CHOOSE':function () {
        var r = new cBaseFunction( "CHOOSE" );
        r.setArgumentsMin( 2 );
        r.setArgumentsMax( 30 );
        r.Calculate = function ( arg ) {
            var arg0 = arg[0];

            if ( arg0 instanceof cArea || arg0 instanceof cArea3D ) {
                arg0 = arg0.cross( arguments[1].first );
            }
            arg0 = arg0.tocNumber();

            if ( arg0 instanceof cError ) {
                return this.value = arg0;
            }

            if ( arg0 instanceof cNumber ) {
                if ( arg0.getValue() < 1 || arg0.getValue() > this.getArguments() ) {
                    return this.value = new cError( cErrorType.wrong_value_type );
                }

                return this.value = arg[arg0.getValue()];
            }

            return this.value = new cError( cErrorType.wrong_value_type );
        }
        r.getInfo = function () {
            return {
                name:this.name,
                args:"( index , argument-list )"
            };
        }
        return r;
    },
    'COLUMN':function () {
        var r = new cBaseFunction( "COLUMN" );
        r.setArgumentsMin( 0 );
        r.setArgumentsMax( 1 );
        r.Calculate = function ( arg ) {
            var arg0;
            if ( this.argumentsCurrent == 0 ) {
                arg0 = arguments[1];
                return this.value = new cNumber( arg0.getFirst().getCol() );
            }
            arg0 = arg[0];
            if ( arg0 instanceof cRef || arg0 instanceof cRef3D || arg0 instanceof cArea ) {
                var range = arg0.getRange();
                if ( range )
                    return this.value = new cNumber( range.getFirst().getCol() );
                else
                    return this.value = new cError( cErrorType.bad_reference );
            }
            else if ( arg0 instanceof cArea3D ) {
                var r = arg0.getRange();
                if ( r && r[0] && r[0].getFirst() ) {
                    return this.value = new cNumber( r[0].getFirst().getCol() );
                }
                else {
                    return this.value = new cError( cErrorType.bad_reference );
                }
            }
            else
                return this.value = new cError( cErrorType.bad_reference );
        }
        r.getInfo = function () {
            return {
                name:this.name,
                args:"( [ reference ] )"
            };
        }
        return r;
    },
    'COLUMNS':function () {
        var r = new cBaseFunction( "COLUMNS" );
        r.setArgumentsMin( 1 );
        r.setArgumentsMax( 1 );
        r.Calculate = function ( arg ) {
            var arg0 = arg[0];
            if ( arg0 instanceof cArray ) {
                return this.value = new cNumber( arg0.getCountElementInRow() );
            }
            else if ( arg0 instanceof cArea || arg0 instanceof cRef || arg0 instanceof cRef3D ) {
                var range = arg0.getRange();
                return this.value = new cNumber( Math.abs( range.getBBox().c1 - range.getBBox().c2 ) + 1 );
            }
            else if ( arg0 instanceof cArea3D ) {
                var range = arg0.getRange();
                if ( range.length > 1 )
                    return this.value = new cError( cErrorType.wrong_value_type );

                return this.value = new cNumber( Math.abs( range[0].getBBox().c1 - range[0].getBBox().c2 ) + 1 );
            }
            else
                return this.value = new cError( cErrorType.wrong_value_type );
        }
        r.getInfo = function () {
            return {
                name:this.name,
                args:"( array )"
            };
        }
        return r;
    },
    'GETPIVOTDATA':function () {
        var r = new cBaseFunction( "GETPIVOTDATA" );
        return r;
    },
    'HLOOKUP':function () {
        var r = new cBaseFunction( "HLOOKUP" );
        r.setArgumentsMin( 3 );
        r.setArgumentsMax( 4 );
        r.Calculate = function ( arg ) {
            var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arg3 = this.argumentsCurrent == 4 ? arg[3].tocBool() : new cBool( true );
            var numberRow = arg2.getValue() - 1, valueForSearching = arg0.getValue(), resC = -1, min, regexp;

            if ( isNaN( numberRow ) )
                return this.value = new cError( cErrorType.bad_reference );

            if ( numberRow < 0 )
                return this.value = new cError( cErrorType.wrong_value_type );

            if ( arg0 instanceof cString ) {
                valueForSearching = arg0.getValue();
                regexp = searchRegExp(valueForSearching);
            }
            else if ( arg0 instanceof cError )
                return this.value = arg0;
            else {
                valueForSearching = arg0.getValue();
            }

            var found = false, bb,
                f = function ( cell, r, c, r1, c1 ) {
                    if ( r == r1 ) {
                        var cv = cell.getValueWithoutFormat(), cvType = checkTypeCell(cv);
                        if ( c == c1 )
                            min = cv;
                        if ( arg3.value == true ) {
                            if ( arg0 instanceof cString ) {
                                if ( cvType instanceof cString ){
                                    if( valueForSearching.localeCompare( cvType.getValue() ) == 0 ){
                                        resC = c;
                                        found = true;
                                    }
                                    else if( valueForSearching.localeCompare( cvType.getValue() ) == 1 && !found ){
                                        resC = c;
                                    }
{'fraction_non_alphanumeric': 0.10909847049581813, 'fraction_numerical': 0.01621921352987385, 'mean_word_length': 1.3193655556746329, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984260', 'n_tokens_mistral': 6140, 'n_tokens_neox': 5734, 'n_words': 1430}