JavaScriptを使用したテーブルと範囲の操作 C++
Contents
[
Hide
]
紹介
Microsoft Excelでテーブルを作成し、その機能を使用せずにテーブルのデータを保持したい場合があります。代わりに、テーブルのように見えるものが欲しい場合があります。フォーマットを失わずにテーブル内のデータを保持するには、テーブルを通常のデータの範囲に変換します。
Aspose.Cellsは、テーブルやリストオブジェクトのMicrosoft Excelのこの機能をサポートしています。
Microsoft Excel の使用
表をフォーマットを保持したまま範囲に素早く変換するには、範囲に変換 機能を使用します。Microsoft Excel 2007/2010では:
- 表内のどこかをクリックして、アクティブなセルが表の列内にあることを確認します。
- デザイン タブの ツール グループで、範囲に変換 をクリックします。
Aspose.Cellsの使用
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Convert Table to Range</title>
</head>
<body>
<h1>Convert Table to Range 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();
// Instantiate workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access the first worksheet and convert the first table/list object to a normal range
const worksheet = workbook.worksheets.get(0);
const listObject = worksheet.listObjects.get(0);
listObject.convertToRange();
// Save the modified 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';
document.getElementById('result').innerHTML = '<p style="color: green;">Table converted to range successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
表を範囲に変換した後は、表の機能は利用できなくなります。たとえば、行ヘッダーにはソートとフィルターの矢印が含まれなくなり、数式で使用されたテーブル名を参照する構造化参照は通常のセル参照に変わります。
オプションで範囲にテーブルを変換
Aspose.Cellsは、TableToRangeOptions クラスを介してテーブルを範囲に変換する際に追加のオプションを提供します。TableToRangeOptions クラスは、テーブル行の最後のインデックスを設定できる lastRow プロパティを提供します。指定した行インデックスまでテーブルの書式設定が保持され、その後の書式設定が削除されます。
以下のサンプルコードは、TableToRangeOptions クラスの使用例を示しています。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Table to Range Example</title>
</head>
<body>
<h1>Convert Table to Range Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<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, TableToRangeOptions, 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');
const resultDiv = document.getElementById('result');
const downloadLink = document.getElementById('downloadLink');
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));
// Create TableToRangeOptions and set lastRow property
const options = new TableToRangeOptions();
options.lastRow = 5;
// Convert the first table/list object (from the first worksheet) to normal range
workbook.worksheets.get(0).listObjects.get(0).convertToRange(options);
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
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;">Table converted to range successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>