Accesso alla tabella dalla cella e aggiunta di valori all interno utilizzando offset di riga e colonna con JavaScript tramite C++
Normalmente, si aggiungono valori all’interno della Tabella o dell’oggetto Lista utilizzando il metodo Cell.putValue(boolean). Ma a volte potresti dover aggiungere valori all’interno della Tabella o dell’oggetto Lista utilizzando gli offset di riga e colonna.
Per accedere a una tabella o oggetto di elenco da una cella, usa la proprietà Cell.table. Per aggiungere valori al suo interno usando gli offset di riga e colonna, usa il metodo ListObject.putCellValue(number, number, object).
La schermata successiva mostra il file Excel di origine utilizzato all’interno del codice. Contiene la tabella vuota e evidenzia la cella D5 che si trova all’interno della tabella. Accederemo a questa tabella dalla cella D5 usando la proprietà Cell.table e poi aggiungeremo i valori all’interno usando i metodi Cell.putValue(boolean) e ListObject.putCellValue(number, number, object).
Esempio
Screenshots che confrontano i file di origine e di output
![]() |
|---|
Lo screenshot seguente mostra il file Excel di output generato dal codice. Come puoi vedere, la cella D5 ha un valore e la cella F6 che si trova all’offset 2,2 della tabella ha un valore.
![]() |
|---|
Codice JavaScript per accedere alla tabella dalla cella e aggiungere valori all’interno usando offset di riga e colonna
Il codice di esempio seguente carica il file Excel di origine come mostrato nello screenshot precedente e aggiunge valori all’interno della tabella e genera il file Excel di output come mostrato sopra.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells - Accessing Table 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, Utils } = 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 uploaded Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Access cell D5 which lies inside the table
const cell = worksheet.cells.get("D5");
// Put value inside the cell D5
cell.value = "D5 Data";
// Access the Table from this cell
const table = cell.table;
// Add some value using Row and Column Offset
table.putCellValue(2, 2, "Offset [2,2]");
// 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>

