示例#1
0
        public void CreateShouldCreateNamedValueAsExcelAddressToken()
        {
            var input = "NamedValue";

            A.CallTo(() => _nameValueProvider.IsNamedValue("NamedValue", "")).Returns(true);
            A.CallTo(() => _nameValueProvider.IsNamedValue("NamedValue", null)).Returns(true);
            var token = _tokenFactory.Create(Enumerable.Empty <Token>(), input);

            Assert.AreEqual(TokenType.NameValue, token.TokenType);
            Assert.AreEqual("NamedValue", token.Value);
        }
        public Token Create(IEnumerable <Token> tokens, string token, string worksheet)
        {
            if (_tokenSeparatorProvider.Tokens.TryGetValue(token, out Token tokenSeparator))
            {
                return(tokenSeparator);
            }
            var tokenList         = (IList <Token>)tokens;
            var previousTokenType = tokenList.Count > 0 ? tokenList[tokenList.Count - 1].TokenType : TokenType.Unrecognized;

            if (tokenList.Any() && tokenList.Last().TokenType == TokenType.String)
            {
                return(new Token(token, TokenType.StringContent));
            }

            if (!string.IsNullOrEmpty(token))
            {
                token = token.Trim();
            }

            if (!ReferenceTokens.Contains(previousTokenType))
            {
                if (Regex.IsMatch(token, RegexConstants.Decimal))
                {
                    return(new Token(token, TokenType.Decimal));
                }

                if (Regex.IsMatch(token, RegexConstants.Integer))
                {
                    return(new Token(token, TokenType.Integer));
                }

                if (Regex.IsMatch(token, RegexConstants.Boolean, RegexOptions.IgnoreCase))
                {
                    return(new Token(token, TokenType.Boolean));
                }
            }

            if (token.ToUpper(CultureInfo.InvariantCulture).Contains("#REF!"))
            {
                return(new Token(token, TokenType.InvalidReference));
            }
            if (token.ToUpper(CultureInfo.InvariantCulture) == "#NUM!")
            {
                return(new Token(token, TokenType.NumericError));
            }
            if (token.ToUpper(CultureInfo.InvariantCulture) == "#VALUE!")
            {
                return(new Token(token, TokenType.ValueDataTypeError));
            }
            if (token.ToUpper(CultureInfo.InvariantCulture) == "#NULL!")
            {
                return(new Token(token, TokenType.Null));
            }
            if (_nameValueProvider != null && _nameValueProvider.IsNamedValue(token, worksheet))
            {
                return(new Token(token, TokenType.NameValue));
            }
            if (_functionNameProvider.IsFunctionName(token))
            {
                return(new Token(token, TokenType.Function));
            }
            if (previousTokenType == TokenType.OpeningEnumerable)
            {
                return(new Token(token, TokenType.Enumerable));
            }
            var at = ExcelAddressBase.IsValid(token, _r1c1);

            if (at == ExcelAddressBase.AddressType.CellAddress)
            {
                return(new Token(token, TokenType.ExcelAddress));
            }
            if (at == ExcelAddressBase.AddressType.R1C1)
            {
                return(new Token(token.ToUpper(CultureInfo.InvariantCulture), TokenType.ExcelAddressR1C1));
            }
            if (at == ExcelAddressBase.AddressType.TableReference)
            {
                return(new Token(token, TokenType.TableReference));
            }
            return(new Token(token, TokenType.Unrecognized));
        }
示例#3
0
        public Token Create(IEnumerable <Token> tokens, string token, string worksheet)
        {
            Token tokenSeparator = default(Token);

            if (_tokenSeparatorProvider.Tokens.TryGetValue(token, out tokenSeparator))
            {
                return(tokenSeparator);
            }
            var tokenList = tokens.ToList();

            //Address with worksheet-string before  /JK
            if (token.StartsWith("!") && tokenList[tokenList.Count - 1].TokenTypeIsSet(TokenType.String))
            {
                string addr = "";
                var    i    = tokenList.Count - 2;
                if (i > 0)
                {
                    if (tokenList[i].TokenTypeIsSet(TokenType.StringContent))
                    {
                        addr = "'" + tokenList[i].Value.Replace("'", "''") + "'";
                    }
                    else
                    {
                        throw(new ArgumentException(string.Format("Invalid formula token sequence near {0}", token)));
                    }
                    //Remove the string tokens and content
                    tokenList.RemoveAt(tokenList.Count - 1);
                    tokenList.RemoveAt(tokenList.Count - 1);
                    tokenList.RemoveAt(tokenList.Count - 1);

                    return(new Token(addr + token, TokenType.ExcelAddress));
                }
                else
                {
                    throw(new ArgumentException(string.Format("Invalid formula token sequence near {0}", token)));
                }
            }

            if (tokens.Any() && tokens.Last().TokenTypeIsSet(TokenType.String))
            {
                return(new Token(token, TokenType.StringContent));
            }
            if (!string.IsNullOrEmpty(token))
            {
                token = token.Trim();
            }
            if (Regex.IsMatch(token, RegexConstants.Decimal))
            {
                return(new Token(token, TokenType.Decimal));
            }
            if (Regex.IsMatch(token, RegexConstants.Integer))
            {
                return(new Token(token, TokenType.Integer));
            }
            if (Regex.IsMatch(token, RegexConstants.Boolean, RegexOptions.IgnoreCase))
            {
                return(new Token(token, TokenType.Boolean));
            }
            if (token.ToUpper(CultureInfo.InvariantCulture).Contains("#REF!"))
            {
                return(new Token(token, TokenType.InvalidReference));
            }
            if (token.ToUpper(CultureInfo.InvariantCulture) == "#NUM!")
            {
                return(new Token(token, TokenType.NumericError));
            }
            if (token.ToUpper(CultureInfo.InvariantCulture) == "#VALUE!")
            {
                return(new Token(token, TokenType.ValueDataTypeError));
            }
            if (token.ToUpper(CultureInfo.InvariantCulture) == "#NULL!")
            {
                return(new Token(token, TokenType.Null));
            }
            if (_nameValueProvider != null && _nameValueProvider.IsNamedValue(token, worksheet))
            {
                return(new Token(token, TokenType.NameValue));
            }
            if (_functionNameProvider.IsFunctionName(token))
            {
                return(new Token(token, TokenType.Function));
            }
            if (tokenList.Count > 0 && tokenList[tokenList.Count - 1].TokenTypeIsSet(TokenType.OpeningEnumerable))
            {
                return(new Token(token, TokenType.Enumerable));
            }
            var at = OfficeOpenXml.ExcelAddressBase.IsValid(token, _r1c1);

            if (at == ExcelAddressBase.AddressType.InternalAddress)
            {
                return(new Token(token.ToUpper(CultureInfo.InvariantCulture), TokenType.ExcelAddress));
            }
            else if (at == ExcelAddressBase.AddressType.R1C1)
            {
                return(new Token(token.ToUpper(CultureInfo.InvariantCulture), TokenType.ExcelAddressR1C1));
            }
            return(new Token(token, TokenType.Unrecognized));
        }
示例#4
0
        /// <summary>
        /// Create a new token.
        /// </summary>
        /// <param name="tokens">Existing tokens.</param>
        /// <param name="token">The token to create.</param>
        /// <param name="worksheet">The worksheet name to use to create the token.</param>
        /// <returns>The created token.</returns>
        public Token Create(IEnumerable <Token> tokens, string token, string worksheet)
        {
            if (_tokenSeparatorProvider.Tokens.TryGetValue(token, out var tokenSeparator))
            {
                return(tokenSeparator);
            }
            var tokenList = (IList <Token>)tokens;

            if (tokens.Any() && tokens.Last().TokenType == TokenType.String)
            {
                return(new Token(token, TokenType.StringContent));
            }
            if (!string.IsNullOrEmpty(token))
            {
                token = token.Trim();
            }
            if (Regex.IsMatch(token, RegexConstants.Decimal))
            {
                return(new Token(token, TokenType.Decimal));
            }
            if (Regex.IsMatch(token, RegexConstants.Integer))
            {
                return(new Token(token, TokenType.Integer));
            }
            if (Regex.IsMatch(token, RegexConstants.Boolean, RegexOptions.IgnoreCase))
            {
                return(new Token(token, TokenType.Boolean));
            }
            if (Regex.IsMatch(token, RegexConstants.StructuredReference, RegexOptions.IgnoreCase | RegexOptions.Multiline))
            {
                return(new Token(token, TokenType.StructuredReference));
            }
            if (_nameValueProvider != null && _nameValueProvider.IsNamedValue(token, worksheet))
            {
                return(new Token(token, TokenType.NameValue));
            }
            if (_functionNameProvider.IsFunctionName(token))
            {
                return(new Token(token, TokenType.Function));
            }
            if (tokenList.Count > 0 && tokenList[tokenList.Count - 1].TokenType == TokenType.OpeningEnumerable)
            {
                return(new Token(token, TokenType.Enumerable));
            }

            var addressType = ExcelAddress.IsValid(token);

            if (addressType == ExcelAddress.AddressType.InternalAddress)
            {
                return(new Token(token, TokenType.ExcelAddress));
            }
            if (addressType == ExcelAddress.AddressType.ExternalAddress)
            {
                return(new Token(token, TokenType.InvalidReference));
            }
            else if (addressType == ExcelAddress.AddressType.ExternalName)
            {
                return(new Token(token, TokenType.InvalidReference));
            }
            if (addressType == ExcelAddress.AddressType.Invalid)
            {
                return(new Token(token, TokenType.InvalidReference));
            }
            return(new Token(token, TokenType.Unrecognized));
        }