private void UpdateToNextState(int line, int column, ParseAttribute attribute) { if (attribute.InRepeat) //循环中不改变单元格状态 { return; } if (this.PatternGetState(line, column) == CellState.Unhandled) { this.PatternSetState(line, column, CellState.NormalHandled); } }
/// <summary> /// Init - we want to make sure the ParseStatus for this file is freshly initialized (e.g. when opening / re-opening a file) /// </summary> /// <param name="targetFile"></param> public static void Init(string targetFile) { lock (_synchronizationParseStatus) { if (!ParseStatus.ContainsKey(targetFile)) { ParseStatus.Add(targetFile, new ParseAttribute()); } else { ParseStatus[targetFile] = new ParseAttribute(); } } }
public static int LastPreparseVersion(string forFile) { lock (_synchronizationParseStatus) { if (ParseStatus.ContainsKey(forFile)) { return(ParseStatus[forFile].LastReparseVersion); } else { ParseAttribute newParseAttribute = new ParseAttribute(); ParseStatus.Add(forFile, newParseAttribute); return(0); } } }
private void ParseCell(int line, int column, ParseAttribute attribute) { if (resultTable.Columns <= column) { return; } bool gotNextCell = false; Func <Cell> GetOperationResultCell = () => { if (gotNextCell) { return(ResultGetCurCellByColumn(column)); } else { gotNextCell = true; ResultMoveToNextCellByColumn(column); //首先把样式赋值给resultCell ResultSetCurCellByColumn(column, this.patternTable.GetCell(line, column)); var resultCell = ResultGetCurCellByColumn(column); resultCell.Data = ""; if (resultCell.IsMergedCell) { this.ResultMoveToNextCellByColumn(column, resultCell.GetRowspan() - 1); for (int col = resultCell.Column + 1; col <= resultCell.Column + resultCell.GetColspan() - 1; col++) { this.ResultMoveToNextCellByColumn(col, resultCell.GetRowspan()); } } return(resultCell); } }; //单元格的状态为已经处理过,则跳过 if (this.PatternGetState(line, column) == CellState.NormalHandled) { return; } //否则开始处理单元格数据 Cell curPatternCell = this.PatternGetCell(line, column); //模式表单元格为null,则目标表单元格也为null if (curPatternCell == null) { this.ResultMoveToNextCellByColumn(column); this.UpdateToNextState(line, column, attribute); return; } //无效单元格,则直接跳过 if (curPatternCell.IsValidCell == false) { this.UpdateToNextState(line, column, attribute); return; } //单元格为空则直接向目标表加入单元格内容 if (curPatternCell.Data.ToString().Length == 0) { Cell curResultCell = GetOperationResultCell(); this.UpdateToNextState(line, column, attribute); return; } if (egcmdTranslator.Compile(curPatternCell.Data.ToString(), out var commandList, out string errorMessage) == false) { Cell curResultCell = GetOperationResultCell(); curResultCell.Data = string.Format("Error({0},{1}): {2}", line, column, errorMessage); return; } foreach (Command command in commandList) { if (command is Command.WRITE) { Command.WRITE writeCommand = command as Command.WRITE; var curResultCell = GetOperationResultCell(); string exprResult = null; //表达式计算结果 try { var result = jsEngine.Execute(writeCommand.JsExpr).GetCompletionValue(); if (result.IsNull()) { exprResult = ""; } else { exprResult = result.ToString(); } } catch (Exception e) { curResultCell.Data = string.Format("Error({0},{1}): {2}", line, column, e.Message); return; } curResultCell.Data += exprResult; this.UpdateToNextState(line, column, attribute); } if (command is Command.REPEAT) { var repeatCommand = command as Command.REPEAT; int countLines = repeatCommand.Rows; int countColumns = repeatCommand.Columns; string varName = repeatCommand.VarName; object[] range = null; //循环范围 try { range = (object[])jsEngine.Execute(repeatCommand.Range).GetCompletionValue().ToObject(); } catch { Cell curResultCell = GetOperationResultCell(); curResultCell.Data = string.Format("Error({0},{1}): repeat range \"{2}\" must be iterable", line, column, repeatCommand.Range); return; } //Console.Write("循环开始位置:{0},{1}", line, column); //Console.WriteLine("循环次数:{0}", range.Length); for (int i = column + 1; i < column + countColumns; i++) //将REPEAT单元格同一行右面的REPEAT范围内的单元格状态设为NormalHandled { this.PatternSetState(line, i, CellState.NormalHandled); } for (int i = 0; i < countLines; i++) //将Repeat块部分设置成UnHandled { for (int j = 0; j < countColumns; j++) { this.PatternSetState(line + 1 + i, column + j, CellState.Unhandled); } } foreach (object time in range) //开始循环 { jsEngine.SetValue(varName, time); for (int i = 0; i < countLines; i++) { for (int j = 0; j < countColumns; j++) { this.ParseCell(line + 1 + i, column + j, new ParseAttribute() { InRepeat = true }); } } } if (!attribute.InRepeat) //如果已经是最外层循环,则循环完毕后将自身设为NormalHandled { this.PatternSetState(line, column, CellState.NormalHandled); } for (int i = 0; i < countLines; i++) //将Repeat块的所有单元格的状态设置成NormalHandled { for (int j = 0; j < countColumns; j++) { this.PatternSetState(line + 1 + i, column + j, CellState.NormalHandled); } } } if (command is Command.TEXT) { var textCommand = command as Command.TEXT; Cell curResultCell = GetOperationResultCell(); curResultCell.Data += textCommand.Text; } if (command is Command.SET_COLOR) { var setColorCommand = command as Command.SET_COLOR; Cell curResultCell = GetOperationResultCell(); try { Color color = (Color)this.jsEngine.Execute(setColorCommand.JsExpr).GetCompletionValue().ToObject(); curResultCell.Style.BackColor = color; } catch { curResultCell.Data = string.Format("Error({0},{1}): unknown color \"{2}\"", line, column, setColorCommand.JsExpr); return; } } if (command is Command.SET_TABLE_COLUMNS) { var setTableColumnsCommand = command as Command.SET_TABLE_COLUMNS; Cell curResultCell = GetOperationResultCell(); try { int columns = (int)this.jsEngine.Execute(setTableColumnsCommand.JsExpr).GetCompletionValue().AsNumber(); resultTable.Columns = columns; } catch { curResultCell.Data = string.Format("Error({0},{1}): set table columns command doesn't recognize \"{2}\"", line, column, setTableColumnsCommand.JsExpr); return; } } } }