Managing Ranges with JavaScript via C++
Introduction
In Excel, you can select multiple cells with a mouse‑box selection; the set of selected cells is called a range.
For example, you can click the left mouse button on cell A1 of Excel and then drag to cell C4. The rectangular area you selected can also be easily created as an object by using Aspose.Cells for JavaScript via C++.
Here is how to create a range, put a value, set a style, and perform more operations on the Range object.
Managing Ranges Using Aspose.Cells for JavaScript via C++
Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a Workbook.worksheets collection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a cells collection.
Create Range
When you want to create a rectangular area that extends over A1:C4, you can use the following code:
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get Cells (converted from getWorksheets().get(0).getCells() to properties)
const cells = workbook.worksheets.get(0).cells;
// Create Range A1:C4
const range = cells.createRange("A1:C4");
// Save the workbook and provide a download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultDiv.innerHTML = '<p style="color: green;">Range A1:C4 created successfully. Click the download link to get the modified file.</p>';
});
</script>
</html>
Put values into the cells of the range
Say you have a range of cells that extends over A1:C4. The matrix contains 4 × 3 = 12 cells. The individual range cells are arranged sequentially: Range[0,0], Range[0,1], Range[0,2], Range[1,0], Range[1,1], Range[1,2], Range[2,0], Range[2,1], Range[2,2], Range[3,0], Range[3,1], Range[3,2].
The following example shows how to input some values into the cells of the range.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Range Value Example</title>
</head>
<body>
<h1>Range Value Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
const worksheet = workbook.worksheets.get(0);
const cells = worksheet.cells;
const range = cells.createRange("A1:C4");
range.get(0, 0).value = "A1";
range.get(0, 1).value = "B1";
range.get(0, 2).value = "C1";
range.get(3, 0).value = "A4";
range.get(3, 1).value = "B4";
range.get(3, 2).value = "C4";
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'RangeValueTest.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and ready for download.</p>';
});
</script>
</html>
Set style of the cells of the range
The following example shows how to set the style of the cells of the range.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Range Style Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Gets Cells
const cells = workbook.worksheets.get(0).cells;
// Creates Range
const range = cells.createRange("A1:C4");
// Puts value
range.get(0, 0).value = "A1";
range.get(3, 2).value = "C4";
// Sets Style
let style00 = workbook.createStyle();
style00.pattern = AsposeCells.BackgroundType.Solid;
style00.foregroundColor = new AsposeCells.Color(255, 0, 0); // Red
range.get(0, 0).style = style00;
let style32 = workbook.createStyle();
style32.pattern = AsposeCells.BackgroundType.HorizontalStripe;
style32.foregroundColor = new AsposeCells.Color(0, 255, 0); // Green
range.get(3, 2).style = style32;
// Saves the Workbook
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'RangeStyleTest.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
resultDiv.innerHTML = '<p style="color: green;">Workbook created successfully. Click the download link to save the file.</p>';
});
</script>
</html>
Get CurrentRegion of the range
CurrentRegion is a property that returns a Range object representing the current region. The current region is a range bounded by any combination of blank rows and blank columns. It is read‑only.
In Excel, you can obtain the CurrentRegion area by:
- Selecting an area (range1) with the mouse box.
- Clicking Home → Editing → Find & Select → Go To Special → Current region, or using Ctrl + Shift + *. Excel will automatically select the surrounding area (range2). The resulting range2 is the CurrentRegion of range1.
Please download the following test file, open it in Excel, use the mouse box to select the area A1:D7, then press Ctrl + Shift + * – you will see the area A1:C3 selected.
Now run the following example to see how it works in Aspose.Cells:
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Get Current Region Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Creating a Workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.worksheets.get(0);
// Get Cells
const cells = worksheet.cells;
// Create Range
const src = cells.createRange("A1:D7");
// Get CurrentRegion (converted from getCurrentRegion())
const A1C3 = src.currentRegion;
// Save the workbook (no modifications were required by original code)
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.current_region.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Current region obtained successfully. Click the download link to download the file.</p>';
});
</script>
</html>
Advanced topics
- AutoFill range of an Excel file
- Copy ranges of an Excel file
- Copy range data only
- Copy range data with style
- Copy range style only
- Create union range
- Cut and paste range
- Delete ranges
- Get address, cell count, offset, entire column and entire row of the range
- Insert ranges
- Merge or unmerge range of cells
- Move range of cells in a worksheet
- Create workbook and worksheet‑scoped named ranges
- Search and replace data in a range