我已经查看了 PowerPoint 界面表、行和单元格中的所有属性和方法。虽然 Cell 有办法拆分和合并单元格,但没有办法确定单元格当前是否已合并。有办法做到这一点吗?
如果有区别的话,我们使用 COM(而不是 VSTO)API。
如果您使用Aspose.Slides for .NET,将很容易检查表格单元格是否合并。您将了解有关合并单元格的所有必要信息。以下代码示例展示了如何执行此操作。
for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
{
for (int columnIndex = 0; columnIndex < table.Columns.Count; columnIndex++)
{
ICell cell = table.Rows[rowIndex][columnIndex];
if (cell.IsMergedCell)
{
Console.WriteLine($"The merged cell [{rowIndex},{columnIndex}] was found.");
Console.WriteLine($"Row span: {cell.RowSpan}");
Console.WriteLine($"Column span: {cell.ColSpan}");
Console.WriteLine($"The first row index: {cell.FirstRowIndex}");
Console.WriteLine($"The first column index: {cell.FirstColumnIndex}");
Console.WriteLine();
}
}
}
我在 Aspose 担任支持开发人员。
在VBA中,我们可以比较单元格和行的高度。如果不同,则表示单元格垂直合并。以下函数确定给定单元格是垂直合并还是水平合并。
'simply check if the cell is merged
Function IsMerged(oTbl As Table, rr As Integer, cc As Integer) As Boolean
Dim c As Cell
'the current cell
Set c = oTbl.Cell(rr, cc)
'Check the width and height
'horizonatally merged
If c.Shape.Width <> oTbl.Columns(cc).Width Then IsMerged = True
'vertically merged
If c.Shape.Height <> oTbl.Rows(rr).Height Then IsMerged = True
End Function
我们还可以通过以下方法识别该单元格是否是合并区域的左上角(第一个)单元格:
Function isTopLeftCell(oTbl As Table, rr As Integer, cc As Integer) As Boolean
Dim i As Integer
With oTbl.Cell(rr, cc).Shape
'horizontally merged
If .Width <> oTbl.Columns(cc).Width Then
'count the left cells merged from the currnet cell
For i = 1 To cc - 1
If oTbl.Cell(rr, cc - i).Shape.Left <> .Left Then Exit For
Next i
'count the rows above
If i = 1 Then
For i = 1 To rr - 1
If oTbl.Cell(rr - i, cc).Shape.Top <> .Top Then Exit For
Next i
If i = 1 Then isTopLeftCell = True: Exit Function
End If
'vertically merged
ElseIf .Height <> oTbl.Rows(rr).Height Then
For i = 1 To rr - 1
If oTbl.Cell(rr - i, cc).Shape.Top <> .Top Then Exit For
Next i
If i = 1 Then isTopLeftCell = True: Exit Function
Else
'isTopLeftCell = False
End If
End With
End Function
以下返回合并区域的宽度(ww)和高度(hh)
'Returns the width and height of the merged area
Function getMergedArea(oTbl As Table, rr As Integer, cc As Integer, ByRef ww As Integer, ByRef hh As Integer)
Dim c As Cell
Dim i As Integer, j As Integer
Set c = oTbl.Cell(rr, cc)
'ww = 1: hh = 1
ww = c.Shape.Width / oTbl.Columns(cc).Width
hh = c.Shape.Height / oTbl.Rows(rr).Height
End Function
如果您想获取索引号。合并区域中的单元格: (这是我努力的结果!)
'Returns the index no. of the cell : top to bottom, left to right
Function getMergedIndex(oTbl As Table, rr As Integer, cc As Integer) As Integer
Dim c As Cell
Dim i As Integer, j As Integer, mc As Integer
Set c = oTbl.Cell(rr, cc)
'horizontally merged
If c.Shape.Width <> oTbl.Columns(cc).Width Then
'get the horizontal index
For i = 1 To cc - 1
If oTbl.Cell(rr, cc - i).Shape.Left <> c.Shape.Left Then Exit For
Next i
'get the merged row count above
For j = 1 To rr - 1
If oTbl.Cell(rr - j, cc).Shape.Top <> c.Shape.Top Then Exit For
Next j
'if merged both horizontally and vertically
If j > 1 Then
'get the column count of the merged cells above
mc = oTbl.Cell(rr - 1, cc).Shape.Width / oTbl.Columns(cc).Width
'For mc = 1 To oTbl.Columns.Count - cc
' If oTbl.Cell(rr - 1, cc + mc).Shape.Left <> c.Shape.Left Then Exit For
'Next mc
'mc = i + mc - 1
'add up to the merged cells above
getMergedIndex = (j - 1) * mc + i
Else
getMergedIndex = i
End If
'vertically merged
ElseIf c.Shape.Height <> oTbl.Rows(rr).Height Then
For i = 1 To rr - 1
If oTbl.Cell(rr - i, cc).Shape.Top <> c.Shape.Top Then Exit For
Next i
getMergedIndex = i
Else
'not merged
End If
End Function
''For example:
''[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
''[ ][01][02][03][04][ ][ ][ ][ ][ ]
''[ ][05][06][07][08][ ][ ][ ][ ][ ]
''[ ][ ][01][02][03][04][01][02][ ][ ]
''[ ][ ][05][06][07][08][03][04][ ][ ]
''[ ][ ][ ][ ][ ][ ][05][06][ ][ ]
''[01][02][03][04][01][ ][07][08][01][02]
''[05][06][07][08][02][ ][ ][ ][03][04]
''[01][02][03][04][ ][ ][ ][ ][05][06]
''[05][06][07][08][ ][ ][ ][ ][07][08]
最后,检查两个给定单元格是否包含在合并区域中
Function isMerged2(oTbl As Table, r1%, c1%, r2%, c2%) As Boolean
If oTbl.Cell(r1, c1).Shape.Left = oTbl.Cell(r2, c2).Shape.Left And _
oTbl.Cell(r1, c1).Shape.Top = oTbl.Cell(r2, c2).Shape.Top And _
oTbl.Cell(r1, c1).Shape.Width = oTbl.Cell(r2, c2).Shape.Width And _
oTbl.Cell(r1, c1).Shape.Height = oTbl.Cell(r2, c2).Shape.Height Then _
isMerged2 = True
End Function
在对个人项目的这个问题进行大量修改之后,我发现如果一个单元格与另一个单元格共享其
Shape
属性,您就可以知道该单元格已合并。您可以在每个单元格的 Is
上使用 Shape
运算符进行检查。
对于任何给定的单元格,只需检查它是否与其相邻单元格共享
Shape
,表格边缘的单元格除外。
例如,如果表格
(1, 1)
中的单元格 (1, 2)
和 MyTable
被合并,则以下语句将返回 True
:
MyTable.Cell(1, 1).Shape Is MyTable.Cell(1, 2).Shape