セルのリッチテキストの一部をアクセスして更新する
Contents
[
Hide
]
Aspose.Cells for JavaScriptはC++経由でセルのリッチテキスト部分のアクセスと更新を可能にします。その目的のために、Cell.charactersとCell.charactersのプロパティを使用できます。これらのプロパティはフォント名、フォント色、太字などさまざまなフォントのプロパティにアクセスしたり更新したりするためのFontSettingオブジェクトの配列を返したり受け取ったりします。
セルのリッチテキストの部分にアクセスして更新
以下のコードは、提供されたリンクからダウンロード可能なソースエクセルファイルを使用して、Cell.charactersとCell.charactersのプロパティの使用例を示しています。ソースエクセルファイルのセルA1にはリッチテキストがあり、3つの部分と異なるフォントが設定されています。以下のコードスニペットはこれらの部分にアクセスし、最初の部分のフォント名を新しいものに更新し、その後ワークブックを出力エクセルファイルとして保存します。開くと、最初の部分のフォントが**“Arial”**に変更されているのがわかります。
JavaScriptを用いたセルのリッチテキスト部分へのアクセスと更新のコード
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Update Cell Character Font 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;
const readyPromise = 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;
}
// Ensure Aspose.Cells is initialized
await readyPromise;
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet and its cells
const cells = workbook.worksheets.get(0).cells;
const cell = cells.get("A1");
console.log("Before updating the font settings....");
let fnts = cell.characters;
const count = fnts.length;
for (let i = 0; i < count; i++) {
console.log(fnts[i].font.name);
}
// Modify the first FontSetting Font Name
fnts[0].font.name = "Arial";
// And update it using characters property
cell.characters = fnts;
console.log("After updating the font settings....");
fnts = cell.characters;
for (let i = 0; i < count; i++) {
console.log(fnts[i].font.name);
}
// Save workbook and provide 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 Excel File';
resultDiv.innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
サンプルコードによって生成されたコンソール出力
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana