Showing Cell Range as the Data Labels with JavaScript via C++
Contents
[
Hide
]
In Microsoft Excel 2013, you can display a cell range for data labels. Aspose.Cells for JavaScript via C++ supports this feature.
Check-box to Show Cell Range as Data Labels
To show the cell range as data labels in Microsoft Excel:
- Select the series data labels and right-click to open the context menu.
- Select Format Data Labels. Label options are displayed.
- Select or clear the option Label Contains - Value From Cells.
The sample code below accesses a chart series data labels and sets the DataLabels.showCellRange property to true to select the Label Contains - Value From Cells option.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Show Range in Data Labels</title>
</head>
<body>
<h1>Show Range in Data Labels 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();
// Create workbook from the source Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access the first worksheet
const worksheet = workbook.worksheets.get(0);
// Access the chart inside the worksheet
const chart = worksheet.charts.get(0);
// Check the "Label Contains - Value From Cells"
const dataLabels = chart.nSeries.get(0).dataLabels;
dataLabels.showCellRange = true;
// Save 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 = 'output_out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>