// 新規ワークブックの作成 var workbook = new GrapeCity.Documents.Excel.Workbook(); const string Formula = "RAND()>0.5+0.001"; // 構文木を取得 var syntaxTree = FormulaSyntaxTree.Parse(Formula); // ノードをフラット化 var displayItems = new List<(string TypeName, int IndentLevel, string Content)>(); void flatten(SyntaxNode node, int level) { displayItems.Add((node.GetType().Name, level, node.ToString())); foreach (var child in node.Children) { flatten(child, level + 1); } } flatten(syntaxTree.Root, 0); // 表示 var sheet1 = workbook.Worksheets["Sheet1"]; sheet1.ShowRowOutline = false; sheet1.OutlineColumn.ColumnIndex = 1; sheet1.OutlineColumn.CollapseIndicator = new ImageSource(GetResourceStream("decreaseIndicator.png"), ImageType.PNG); sheet1.OutlineColumn.ExpandIndicator = new ImageSource(GetResourceStream("increaseIndicator.png"), ImageType.PNG); // ヘッダー sheet1.Range["A1"].Value = "数式"; sheet1.Range["B1"].Value = "構文ノード"; sheet1.Range["C1"].Value = "要素"; // 値 sheet1.Range["A2"].Value = "'=" + Formula; for (var i = 0; i < displayItems.Count; i++) { var item = displayItems[i]; var text = "'" + item.TypeName; sheet1.Range[i + 1, 1].Value = text; sheet1.Range[i + 1, 1].IndentLevel = item.IndentLevel; sheet1.Range[i + 1, 2].Value = "'" + item.Content; } // 表示を調整 sheet1.Range["A:C"].EntireColumn.AutoFit(); sheet1.Range["B:B"].EntireColumn.ColumnWidthInPixel += 40; // xlsx ファイルに保存 workbook.Save("PrintFormulaSyntax.xlsx");
' 新規ワークブックの作成 Dim workbook As New Workbook Const Formula = "RAND()>0.5+0.001" ' 構文木を取得 Dim syntaxTree = FormulaSyntaxTree.Parse(Formula) ' ノードをフラット化 Dim displayItems As New List(Of (TypeName As String, IndentLevel As Integer, Content As String)) Dim flatten As Action(Of SyntaxNode, Integer) = Sub(node, level) displayItems.Add((node.GetType.Name, level, node.ToString)) For Each child In node.Children flatten(child, level + 1) Next End Sub flatten(syntaxTree.Root, 0) ' 表示 Dim sheet1 = workbook.Worksheets!Sheet1 sheet1.ShowRowOutline = False With sheet1.OutlineColumn .ColumnIndex = 1 .CollapseIndicator = New ImageSource(GetResourceStream("decreaseIndicator.png"), ImageType.PNG) .ExpandIndicator = New ImageSource(GetResourceStream("increaseIndicator.png"), ImageType.PNG) End With With sheet1.Range ' ヘッダー !A1.Value = "数式" !B1.Value = "構文ノード" !C1.Value = "要素" ' 値 !A2.Value = "'=" & Formula For i = 0 To displayItems.Count - 1 Dim item = displayItems(i) Dim text = "'" & item.TypeName .Item(i + 1, 1).Value = text .Item(i + 1, 1).IndentLevel = item.IndentLevel .Item(i + 1, 2).Value = "'" & item.Content Next End With ' 表示を調整 sheet1.Range("A:C").EntireColumn.AutoFit() sheet1.Range("B:B").EntireColumn.ColumnWidthInPixel += 40 ' xlsx ファイルに保存 workbook.Save("PrintFormulaSyntax.xlsx")