Hi Team,
I am selecting cells in the viewer and want to merge them in the .xlsx file. Is there any logic available to perform cell merging?
Note: I am using GridJs Aspose.Cells for Python via .NET on the backend.
I want the logic in grid js.
Cause
To merge cells in an Excel file using Aspose.Cells for Python via .NET, you need to utilize the merge method provided by the Cells class. This method allows you to specify the range of cells you want to merge.
Solution
You can merge cells by specifying the starting row and column, along with the total number of rows and columns you wish to merge. Here’s how you can do it:
Code
Here’s a sample code snippet that demonstrates how to merge cells in a worksheet:
from aspose.cells import Workbook
# Create a new workbook
workbook = Workbook()
# Access the first worksheet
worksheet = workbook.worksheets[0]
# Merge cells from C6 to E7
worksheet.cells.merge(5, 2, 2, 3) # (first_row, first_column, total_rows, total_columns)
# Input data into the merged cell (C6)
worksheet.cells.get(5, 2).put_value("Merged Cell")
# Save the workbook
workbook.save("merged_cells.xlsx")
In this example, cells from C6 to E7 are merged into a single cell, and the value “Merged Cell” is set for the merged cell.
Next Steps
- Adjust the row and column indices in the
mergemethod according to your specific needs. - Save the workbook in your desired format (e.g.,
.xlsx). - Test the code to ensure it meets your requirements.
If you have any further questions or need additional assistance, feel free to ask!
@sarathepiq
Hello, you can use the following code to merge C2 and D2:
xs.sheet.data.selector.range.set(1,2,1,3)
xs.sheet.data.setSelectedCellAttr("merge",true)
//trigger UI refresh
xs.sheet.table.render();
You can also select C2 and D2 and then click the freeze button below.
@sarathepiq
or you can use the new js API since v25.7
//set attribute without change current selection
xs.sheet.data.setRangeAttr(range,attributename,value)
//do merge for A1:C2
xs.sheet.data.setRangeAttr({sri:0,sci:0,eri:2:eci:2},'merge',true);
//do unmerge for A1:C2
xs.sheet.data.setRangeAttr({sri:0,sci:0,eri:2:eci:2},'merge',false);