/// <summary> /// Translates betweein R1C1 or absolut addresses /// </summary> /// <param name="value">The addresss/function</param> /// <param name="addressTranslator">The translating function</param> /// <param name="row"></param> /// <param name="col"></param> /// <param name="rowIncr"></param> /// <param name="colIncr"></param> /// <returns></returns> private static string Translate(string value, dlgTransl addressTranslator, int row, int col, int rowIncr, int colIncr) { if (value == "") { return(""); } bool isText = false; string ret = ""; string part = ""; char prevTQ = (char)0; for (int pos = 0; pos < value.Length; pos++) { char c = value[pos]; if (c == '"' || c == '\'') { if (isText == false && part != "" && prevTQ == c) { ret += addressTranslator(part, row, col, rowIncr, colIncr); part = ""; prevTQ = (char)0; } prevTQ = c; isText = !isText; ret += c; } else if (isText) { ret += c; } else { if ((c == '-' || c == '+' || c == '*' || c == '/' || c == '=' || c == '^' || c == ',' || c == ':' || c == '<' || c == '>' || c == '(' || c == ')' || c == '!' || c == ' ' || c == '&' || c == '%') && (pos == 0 || value[pos - 1] != '[')) //Last part to allow for R1C1 style [-x] { ret += addressTranslator(part, row, col, rowIncr, colIncr) + c; part = ""; } else { part += c; } } } if (part != "") { ret += addressTranslator(part, row, col, rowIncr, colIncr); } return(ret); }
/// <summary> /// Translates betweein R1C1 or absolut addresses /// </summary> /// <param name="value">The addresss/function</param> /// <param name="addressTranslator">The translating function</param> /// <param name="row"></param> /// <param name="col"></param> /// <returns></returns> private static string Translate(string value, dlgTransl addressTranslator, int row, int col) { if (value == "") { return(""); } var lexer = new Lexer(SourceCodeTokenizer.R1C1, new SyntacticAnalyzer()); var tokens = lexer.Tokenize(value, null); foreach (var token in tokens) { //Console.WriteLine($"{token.TokenType} : {token.Value}"); if (token.TokenType == TokenType.ExcelAddress || token.TokenType.Equals(TokenType.NameValue) || token.TokenType == TokenType.ExcelAddressR1C1) { var part = addressTranslator(token.Value, row, col); //Console.Write($"==> " + part); token.Value = part; } } var ret = string.Join("", tokens.Select(x => x.Value).ToArray()); return(ret); }
/// <summary> /// Translates betweein R1C1 or absolut addresses /// </summary> /// <param name="value">The addresss/function</param> /// <param name="addressTranslator">The translating function</param> /// <param name="row"></param> /// <param name="col"></param> /// <param name="rowIncr"></param> /// <param name="colIncr"></param> /// <returns></returns> private static string Translate(string value, dlgTransl addressTranslator, int row, int col, int rowIncr, int colIncr) { if (value == "") return ""; bool isText = false; string ret = ""; string part = ""; for (int pos = 0; pos < value.Length; pos++) { char c = value[pos]; if (c == '"') { if (isText == false && part != "") { ret += addressTranslator(part, row, col, rowIncr, colIncr); part = ""; } isText = !isText; ret += c; } else if (isText) { ret += c; } else { if ((c == '-' || c == '+' || c == '*' || c == '/' || c == '=' || c == '^' || c == ',' || c == ':' || c == '<' || c == '>' || c == '(' || c == ')') && (pos == 0 || value[pos - 1] != '[')) //Last part to allow for R1C1 style [-x] { ret += addressTranslator(part, row, col, rowIncr, colIncr) + c; part = ""; } else { part += c; } } } if (part != "") { ret += addressTranslator(part, row, col, rowIncr, colIncr); } return ret; }