/// <summary> /// Делаем расчет того что между {}, расчитанное возвращаем в виде строки /// </summary> /// <param name="inputExpression"></param> /// <param name="proryvSpreadsheetObject"></param> /// <returns></returns> public static string ParseTextToString(string inputExpression, ISpreadsheetProperties proryvSpreadsheetObject) { if (inputExpression.IndexOf('{') < 0) { return(inputExpression); } var ev = ParseTextValue(inputExpression, proryvSpreadsheetObject); return(ev != null?ev.ToString().Replace(_sep, ".") : null); }
public static object ParseTextValue(string inputExpression, ISpreadsheetProperties proryvSpreadsheetObject) { if (string.IsNullOrEmpty(inputExpression)) { return(null); } List <ProryvParser.StiAsmCommand> list = null; var parser = new ProryvParser(); parser.SetProryvSpreadsheetObject(proryvSpreadsheetObject); Init(); try { list = new List <ProryvParser.StiAsmCommand>(); int counter = 0; int pos = 0; while (pos < inputExpression.Length) { #region Plain text int posBegin = pos; while (pos < inputExpression.Length && inputExpression[pos] != '{') { pos++; } if (pos != posBegin) { list.Add(new ProryvParser.StiAsmCommand(ProryvParser.StiAsmCommandType.PushValue, inputExpression.Substring(posBegin, pos - posBegin))); counter++; if (counter > 1) { list.Add(new ProryvParser.StiAsmCommand(ProryvParser.StiAsmCommandType.Add)); } } #endregion #region Expression if (pos < inputExpression.Length && inputExpression[pos] == '{') { pos++; posBegin = pos; bool flag = false; while (pos < inputExpression.Length) { if (inputExpression[pos] == '"') { pos++; int pos2 = pos; while (pos2 < inputExpression.Length) { if (inputExpression[pos2] == '"') { break; } if (inputExpression[pos2] == '\\') { pos2++; } pos2++; } pos = pos2 + 1; continue; } if (inputExpression[pos] == '}') { string currentExpression = inputExpression.Substring(posBegin, pos - posBegin); if (currentExpression != null && currentExpression.Length > 0) { parser.expressionPosition = posBegin; list.AddRange(parser.ParseToAsm(currentExpression)); counter++; if (counter > 1) { list.Add(new ProryvParser.StiAsmCommand(ProryvParser.StiAsmCommandType.Cast, TypeCode.String)); list.Add(new ProryvParser.StiAsmCommand(ProryvParser.StiAsmCommandType.Add)); } } flag = true; pos++; break; } pos++; } if (!flag) { parser.expressionPosition = posBegin; list.Add(new ProryvParser.StiAsmCommand(ProryvParser.StiAsmCommandType.PushValue, inputExpression.Substring(posBegin))); counter++; if (counter > 1) { list.Add(new ProryvParser.StiAsmCommand(ProryvParser.StiAsmCommandType.Add)); } } } #endregion } } catch (Exception ex) { throw ex; } if (list.Count > 0) { var v = parser.ExecuteAsm(list); return(v); } return(null); }
/// <summary> /// Обсчитываем все что выделено между {} /// </summary> public static void Evaluate(XlsFile xls, ISpreadsheetProperties properties, out int row, StringBuilder errors) { row = 1; var rowCount = Math.Min(xls.RowCount, 100); for (var r = 1; r <= rowCount; r++) { var isNotExistVoidCol = false; for (var c = 1; c <= xls.ColCount; c++) { var cVal = xls.GetCellValue(r, c); if (!isNotExistVoidCol && cVal != null) { isNotExistVoidCol = true; } var str = cVal as string; if (!string.IsNullOrEmpty(str)) { var indxStart = str.IndexOf('{'); if (indxStart >= 0) { var indxEnd = str.IndexOf('}'); if (indxEnd > 0) { try { var subStr = str.Substring(indxStart, indxEnd - indxStart + 1); var ev = ProryvParsersFactory.ParseTextValue(subStr, properties); if (str[0] == '=' && ev != null) { //Это формула xls.SetCellValue(r, c, new TFormula(ev.ToString().Replace(',', '.'))); } else { xls.SetCellValue(r, c, str.Replace(subStr, ev.ToString())); } } catch (Exception ex) { if (errors != null) { errors.Append(" Ошибка в ячейке ").Append(TCellAddress.EncodeColumn(c)) .Append(r).Append(": ").Append(ex.Message).Append(" ") .Append( ex.InnerException != null ? ex.InnerException.Message : string.Empty) .Append("\n"); } } } } } } //Есть хоть одна заполненная ячейка, смещаем начальную ячейку if (isNotExistVoidCol) { row = r; } } row++; }