Workbook..merge()的源码
时间: 2024-05-03 07:23:23 浏览: 107
Execl操作源码系列C#
Workbook.merge()方法的源码如下:
```python
def merge(self, *args, **kwargs):
"""
Merge cells. The cell range to be merged is specified in the
same way as in write().
Args:
*args: A variable length argument list of cell ranges to be merged.
**kwargs: Optional parameters.
Returns:
Nothing.
Note:
You shouldn't merge cells that have a non-default alignment or
wrapping or other formatting. It will cause strange rendering
problems and probably isn't what you want anyway.
"""
if not args:
return
for area in args:
if isinstance(area, str):
# Convert a string range to a tuple
area = xl_range(area)
# Check that first row is <= last row and first col is <= last col
if area[0] > area[2] or area[1] > area[3]:
raise ValueError("Incorrect cell range: %s" % area)
# Check that all cells are within the worksheet limits
if area[2] > X12_MAX_ROW or area[3] > X12_MAX_COL:
raise ValueError("Cell range (%s) exceeds max limits (%d,%d)" %
(area, X12_MAX_ROW, X12_MAX_COL))
self._check_dimensions(area)
# Write the merge range as an array formula
formula = '{=SUM(%s)}' % xl_range(area)
self.formula_array(area[0], area[1], formula)
return
```
该方法接受一个可变长度参数列表,每个参数表示一个需要合并的单元格区域。其中,单元格区域可以是一个字符串,也可以是一个元组。如果单元格区域为字符串,则该方法会将其转换为元组形式。然后,该方法会检查单元格区域是否正确,以及所有单元格是否在工作表限制范围内。如果有任何错误,则会引发异常。
接下来,该方法会检查单元格区域的维度,并将合并范围写入工作表中。具体来说,它会将合并范围的公式设置为一个数组公式,以便正确地合并单元格。
最后,该方法返回空值。
阅读全文