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 “Range”.
For example, you can click the left mouse button in 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 value into the Cells of the Range
Say you have a range of cells that extends over A1:C4. The matrix makes 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 that represents the current region.
The current region is a range bounded by any combination of blank rows and blank columns. Read-only.
In Excel, you can get the CurrentRegion area by:
- Select an area (range1) with the mouse box.
- Click “Home - Editing - Find & Select - Go To Special - Current region”, or use “Ctrl+Shift+*”, you will see Excel automatically helps you select an area (range2). Now you made it, range2 is the CurrentRegion of range1.
Please download the following test file, open it in Excel, use the mouse box to select an area “A1:D7”, then click “Ctrl+Shift+*”, you will see area “A1:C3” selected.
Now please 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>
Advance topics
- AutoFill range of Excel file
- Copy Ranges of Excel
- 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