Working with Excel sheets becomes a challenging task when a huge amount of data is to be managed or manipulated. Merging or unMerging cells in an Excel worksheet is a frequent operation performed by the users. It lets you merge multiple cells and rows to place content in the center. Therefore, automating this whole process will bring efficiency and robustness and there will be no chance of data loss. However, in this blog post, we will unMerge or merge cells in Excel sheets using Node.js.
We will cover the following points in this blog post:
- Merge Excel cells in Node.js programmatically
- Node.js library to UnMerge cells in Excel Sheets
- How to clear content of a range in an Excel Sheet?
Note: Place a source XLSX file in the project root directory as we have placed the “sample.xlsx” file in this article.
Merge Excel cells in Node.js programmatically
Node.js Excel library provides powerful methods to merge several cells of an Excel worksheet programmatically.
Following are the steps to merge cells in an Excel Sheet using Node.js:
- Instantiate an object of the Cells class.
- Initialize an object of the WorkBook child class with an Excel file.
- Access your worksheet and call the merge(firstRow, firstColumn, totalRows, totalColumns) method to merge a specified range of cells into a single cell.
- Call the save(fileName) method to save the file.
The following code snippet is to merge cells in an Excel Sheet programmatically.
The output of the above code snippet can be seen below in the image.
Node.js library to UnMerge cells in Excel Sheets
Users need to unMerge multiple cells on many occasions and the Node.js Excel library enables you to automate this process.
We can achieve this functionality by following the steps mentioned below:
- Create an object of the Cells class.
- Initialize an object of the WorkBook child class and initialize it with an Excel file.
- Access your Excel sheet and call the unMerge(firstRow, firstColumn, totalRows, totalColumns) method to unMerge cells in an Excel sheet programmatically.
- Save the file using save(fileName) method.
The code snippet is mentioned below to unMerge cells in an Excel worksheet using Node.js.
Now, start the server and you will see the output as shown below in the image.
How to clear content of a range in an Excel Sheet?
Perform the following steps to clear data from a specified range of cells in an Excel worksheet programmatically in a Node.js app:
- Initialize an object of the Cells class.
- Now, create an object of the WorkBook child class and instantiate it with an Excel file.
- Call the clearContents(startRow, startColumn, endRow, endColumn) method to delete data of a range of cells in an Excel file.
- Save the file using the save(fileName) method.
Copy and paste the following code snippet into your main file to clear data from a range in an Excel worksheet.
The output of this code snippet will be something like shown in the image below.
Get a Free License
You can try a free temporary license to use Aspose.Cells for Node.js without evaluation limitations.
This is the end of this blog post. We have gone through some important topics such as unMerge or merge cells in Excel sheets using Node.js and clearing data from a specific range of cells. There are some other relevant methods available that you may explore in this Node.js Excel library. In addition, please explore the documentation to read more about Aspose.Cells for Node.js. Moreover, conholdate.com is in a consistent process of writing new articles. Therefore, please stay connected for regular updates.
Ask a question
You can share your questions or queries on our forum.