答案1
为什么不尝试宏的 VBA 脚本?
我认为这会有所帮助:Worksheets("Sheet1").Rows(i).OutlineLevel
浏览工作表并将该属性写入单元格中。
答案2
我遇到过完全相同的情况,我想正确使用从我的应用程序输出的大纲级别。然后,您需要在 Excel 中创建一个 Visual Basic 脚本。可以通过宏编辑器访问 vb-script 编辑器。插入一个额外的第二列(该列将包含级别),然后遍历文件并用正确的级别填充该列的典型代码如下所示:
Sub GetOutlineLevel()
' Initiate where to start. With a header row, typical start is row 2
RowCount = 2
' Insert a new column B, write "Level" as the title in row 1,
' and define the width of the column
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(1, 2).Value = "Level"
Columns("B:B").Select
Selection.ColumnWidth = 5
' Go to the first row you want to analyze
Cells(RowCount, 1).Activate
' Make a loop to go through all lines that contains data.
' This assumes that there is a continuous row of lines that all have data in the
' first column
Do
' Fill the cell in column 2 at the current row with the outline level
Cells(RowCount, 2).Value = ActiveCell.Rows.OutlineLevel
' Go to the next row
RowCount = RowCount + 1
Cells(RowCount, 1).Activate
' Continue and stop at the first empty row
Loop Until ActiveCell.Value = ""
End Sub
然后,它还可以扩展其他功能。我在上面循环中跟踪最高大纲级别,并制作了与级别数量相同的附加列,然后在每个列中制定了一些逻辑,以便级别结构可以可视化。
插入的列如下所示(其中包括一个条件格式设置,使包含“X”的单元格变为黑色):
ColumnsInserted = 0
Do
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(1, 3).Value = "L" & MaxLevel - ColumnsInserted
Columns("C:C").Select
Selection.ColumnWidth = 4
Columns("C:C").Select
Selection.FormatConditions.Add Type:=xlTextString, String:="X", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
ColumnsInserted = ColumnsInserted + 1
Loop Until ColumnsInserted = MaxLevel
最后是双循环序列,它用与级别列对应的检查填充附加列中的所有单元格,并在正确的单元格中放置“X”。此代码如下所示:
FIns = 1
Do
RowCount = 2
Do
Cells(RowCount, 2 + FIns).Select
ActiveCell.FormulaR1C1 = "=IF(RC[-" & FIns & "]=" & FIns & ",""X"","""")"
RowCount = RowCount + 1
Loop Until Cells(RowCount, 1).Value = ""
FormulaInsert = FormulaInsert + 1
Loop Until FormulaInsert > MaxLevel
我看到上面代码的良好缩进在最后一篇文章中被破坏了,但是如果您熟悉一些编码,您应该能够以好的方式整理它。