塗りつぶし設定
色と背景パターン
Microsoft Excel では、セルの前景(輪郭)と背景(塗りつぶし)の色、および背景パターンを設定できます。
Aspose.Cells もこれらの機能を柔軟にサポートしています。このトピックでは、Aspose.Cells を使用してこれらの機能を使用する方法について学びます。
色と背景パターンの設定
Aspose.CellsはMicrosoft Excelファイルを表すクラスWorkbookを提供しています。Workbookクラスは、Excelファイル内の各ワークシートにアクセス可能なWorksheetsコレクションを含みます。ワークシートはWorksheetクラスで表されます。WorksheetクラスはCellsコレクションを提供し、Cellsコレクションの各アイテムはCellクラスのオブジェクトを表します。
Cellは、セルの書式設定を取得・設定するためのgetStyleおよびsetStyleメソッドを持ちます。Styleクラスはセルの前景色と背景色のプロパティを提供します。Aspose.Cellsは、以下に示す一連の事前定義された背景パターンのタイプを含むBackgroundType列挙型を提供します。
背景パターン | 説明 |
---|---|
DiagonalCrosshatch | 対角線のかすかな格子状のパターンを表します |
DiagonalStripe | 対角線のストライプパターンを表します |
Gray6 | 6.25% グレーのパターンを表します |
Gray12 | 12.5% グレーのパターンを表します |
Gray25 | 25% グレーのパターンを表します |
Gray50 | 50% グレーのパターンを表します |
Gray75 | 75% グレーのパターンを表します |
HorizontalStripe | 水平ストライプパターンを表します |
None | 背景なしを表します |
ReverseDiagonalStripe | 反対角ストライプパターンを表します |
Solid | ソリッドパターンを表します |
ThickDiagonalCrosshatch | 太い斜めクロスハッチパターンを表します |
ThinDiagonalCrosshatch | 細い斜めクロスハッチパターンを表します |
ThinDiagonalStripe | 細い斜めストライプパターンを表します |
ThinHorizontalCrosshatch | 細い水平クロスハッチパターンを表します |
ThinHorizontalStripe | 細い水平ストライプパターンを表します |
ThinReverseDiagonalStripe | 細い逆斜めストライプパターンを表します |
ThinVerticalStripe | 細い垂直ストライプパターンを表します |
VerticalStripe | 垂直ストライプパターンを表します |
以下の例では、A1セルの前景色が設定されていますが、A2は前景色と背景色の両方を垂直ストライプの背景パターンで構成するように設定されています。
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 Workbook 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); | |
// Define a Style and get the A1 cell style | |
let style = worksheet.getCells().get("A1").getStyle(); | |
// Setting the foreground color to yellow | |
style.setForegroundColor(AsposeCells.Color.Yellow); | |
// Setting the background pattern to vertical stripe | |
style.setPattern(AsposeCells.BackgroundType.VerticalStripe); | |
// Apply the style to A1 cell | |
worksheet.getCells().get("A1").setStyle(style); | |
// Get the A2 cell style | |
style = worksheet.getCells().get("A2").getStyle(); | |
// Setting the foreground color to blue | |
style.setForegroundColor(AsposeCells.Color.Blue); | |
// Setting the background color to yellow | |
style.setBackgroundColor(AsposeCells.Color.Yellow); | |
// Setting the background pattern to vertical stripe | |
style.setPattern(AsposeCells.BackgroundType.VerticalStripe); | |
// Apply the style to A2 cell | |
worksheet.getCells().get("A2").setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
重要なこと
- セルの前景色または背景色を設定するには、StyleオブジェクトのsetForegroundColorまたはsetBackgroundColorメソッドを使用します。これらのメソッドは、StyleオブジェクトのPatternプロパティが設定されている場合にのみ有効です。
- setForegroundColorメソッドはセルのシェード色を設定します。
setPatternメソッドは、前景または背景色に使用される背景パターンのタイプを指定します。Aspose.Cellsは、一連の事前定義された背景パターンのタイプを含むBackgroundType列挙型を提供します。 - BackgroundType列挙型からBackgroundType.Noneを選択すると、前景色は適用されません。
同様に、BackgroundType.None または BackgroundType.Solid 値を選択すると、背景色は適用されません。 - セルのシェード/塗りつぶし色を取得する場合、Style.setPattern が BackgroundType.None であれば、Style.getForegroundColor は Color.Empty を返します。
グラデーション塗りつぶし効果の適用
セルにグラデーション塗りつぶし効果を適用するには、Styleオブジェクトの適切なsetTwoColorGradientメソッドを使用します。
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"); | |
// Loads the workbook which contains hidden external links | |
const workbook = new AsposeCells.Workbook(); | |
// Get the first worksheet (default) in the workbook | |
const worksheet = workbook.getWorksheets().get(0); | |
// Input a value into B3 cell | |
worksheet.getCells().get(2, 1).putValue("test"); | |
// Get the Style of the cell | |
const style = worksheet.getCells().get("B3").getStyle(); | |
// Set Gradient pattern on | |
style.setIsGradient(true); | |
// Specify two color gradient fill effects | |
style.setTwoColorGradient(new AsposeCells.Color(255, 255, 255), new AsposeCells.Color(79, 129, 189), AsposeCells.GradientStyleType.Horizontal, 1); | |
// Set the color of the text in the cell | |
style.getFont().setColor(AsposeCells.Color.Red); | |
// Specify horizontal and vertical alignment settings | |
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center); | |
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center); | |
// Apply the style to the cell | |
worksheet.getCells().get("B3").setStyle(style); | |
// Set the third row height in pixels | |
worksheet.getCells().setRowHeightPixel(2, 53); | |
// Merge the range of cells (B3:C3) | |
worksheet.getCells().merge(2, 1, 1, 2); | |
// Save the Excel file | |
workbook.save(path.join(dataDir, "output.xlsx")); |
色とパレット
パレットとは、画像を作成するために使用可能な色の数です。プレゼンテーションで標準化されたパレットを使用することで、ユーザーは一貫した外観を作成できます。各 Microsoft Excel (97-2003) ファイルには、セル、フォント、グリッド線、グラフィックオブジェクト、塗りつぶし、およびグラフの線に適用できる 56 色のパレットがあります。
Aspose.Cells を使用すると、パレットの既存の色だけでなく、カスタム色も使用できます。カスタム色を使用する前に、まずパレットに色を追加します。
このトピックでは、パレットにカスタム色を追加する方法について説明します。
パレットにカスタム色を追加
Aspose.Cells は Microsoft Excel の 56 色のパレットをサポートしています。パレットに定義されていないカスタム色を使用するには、その色をパレットに追加します。
Aspose.CellsはMicrosoft Excelファイルを表すクラスWorkbookを提供します。Workbookクラスには、カスタムカラーを追加してパレットを変更するためのchangePaletteメソッドがあり、引数は次の通りです:
- カスタムカラー、追加するカスタムカラー。
カスタムカラーが置き換えるパレット内の色のインデックスです。0〜55の間である必要があります。
以下の例では、カスタムカラー(Orchid)をパレットに追加し、フォントに適用する前に追加します。
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"); | |
// Loads the workbook which contains hidden external links | |
const workbook = new AsposeCells.Workbook(filePath); | |
// Checks if a color is in the palette for the spreadsheet. | |
console.log(workbook.isColorInPalette(AsposeCells.Color.Orchid)); | |
// Adding Orchid color to the palette at 55th index | |
workbook.changePalette(AsposeCells.Color.Orchid, 55); | |
console.log(workbook.isColorInPalette(AsposeCells.Color.Orchid)); | |
// 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!"); | |
// Defining new Style object | |
const styleObject = workbook.createStyle(); | |
// Setting the Orchid (custom) color to the font | |
styleObject.getFont().setColor(workbook.getColors()[55]); | |
// Applying the style to the cell | |
cell.setStyle(styleObject); | |
// Saving the Excel file | |
workbook.save("out.xlsx"); |