フォント設定をNode.js経由のC++で
フォント設定の構成
Aspose.Cellsは、Microsoft Excelファイルを表すクラスWorkbookを提供しています。Workbookクラスは、Excelファイル内の各ワークシートへアクセスできるWorksheetsコレクションを含みます。ワークシートはWorksheetクラスによって表されます。WorksheetクラスはCellsコレクションを提供し、その中の各アイテムはCellクラスのオブジェクトを表します。
Aspose.Cellsは、セルの書式設定スタイルを取得および設定に使用されるCellクラスのgetStyleおよびsetStyleメソッドを提供します。Styleクラスはフォント設定を構成するためのプロパティを提供します。
フォント名の設定
開発者は、FontオブジェクトのsetNameメソッドを使用して、セル内のテキストに任意のフォントを適用できます。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
const fs = require("fs"); | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font name to "Times New Roman" | |
style.getFont().setName("Times New Roman"); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
太字にフォントスタイルを設定する
開発者は、FontオブジェクトのsetIsBoldメソッドをtrueに設定して、テキストを太字にできます。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font weight to bold | |
style.getFont().setIsBold(true); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("out.xlsx"); |
フォントサイズの設定
FontオブジェクトのsetSizeメソッドを使用してフォントサイズを設定します。
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().getCount(); | |
workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font size to 14 | |
style.getFont().setSize(14); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("out.xlsx"); |
フォントの色の設定
FontオブジェクトのsetColorメソッドを使用してフォントの色を設定します。Color列挙体(Node.jsの一部)から任意の色を選択し、setColorメソッドに割り当てます。
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font color to blue | |
style.getFont().setColor(AsposeCells.Color.Blue); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("out.xlsx"); |
フォントの下線タイプの設定
FontオブジェクトのsetUnderlineメソッドを使用してテキストに下線を引きます。Aspose.Cellsは、FontUnderlineType列挙体にさまざまな事前定義されたフォント下線タイプを提供します。
フォントの下線の種類 | 説明 |
---|---|
Accounting | 単一のアカウンティング下線 |
Double | ダブル下線 |
DoubleAccounting | ダブルアカウンティング下線 |
None | 下線なし |
Single | 単一の下線 |
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const outputPath = path.join(dataDir, "out.xlsx"); | |
// Create directory if it is not already present. | |
if (!require("fs").existsSync(dataDir)){ | |
require("fs").mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the font to be underlined | |
style.getFont().setUnderline(AsposeCells.FontUnderlineType.Single); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(outputPath); |
取り消し線の効果の設定
FontオブジェクトのsetIsStrikeoutメソッドをtrueに設定して取消線を適用します。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the strike out effect on the font | |
style.getFont().setIsStrikeout(true); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "out.xlsx")); |
下付き文字の効果の設定
FontオブジェクトのsetIsSubscriptメソッドをtrueに設定して下付き文字を適用します。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting the strike out effect on the font | |
style.getFont().setIsStrikeout(true); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "out.xlsx")); |
フォントの上付き文字効果の設定
開発者は、FontオブジェクトのsetIsSuperscriptメソッドをtrueに設定して上付き文字の効果を適用できます。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Adding a new worksheet to the Excel object | |
const i = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
const worksheet = workbook.getWorksheets().get(i); | |
// Accessing the "A1" cell from the worksheet | |
const cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
const style = cell.getStyle(); | |
// Setting superscript effect | |
style.getFont().setIsSuperscript(true); | |
// Applying the style to the cell | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "out.xlsx")); |