条件付き書式を使用して交互の行と列に陰影を適用する
Contents
[
Hide
]
Aspose.Cells API は、条件付き書式ルールを追加および操作する手段を提供しますワークシート物体。これらのルールは、条件またはルールに基づいて目的のフォーマットを取得するために、さまざまな方法で調整できます。この記事では、Aspose.Cells for Java API を使用して、条件付き書式ルールと Excel の組み込み関数を使用して、行と列を交互にシェーディングを適用する方法を示します。
条件付き書式を使用して交互の行と列に陰影を適用する
この記事では、ROW、COLUMN、MOD などの Excel の組み込み関数を使用します。先に提供されたコード スニペットをよりよく理解するために、これらの関数の詳細を以下に示します。
- **行()**関数は、セル参照の行番号を返します。参照を省略した場合、参照はROW関数が入力されたセルアドレスであると見なされます。
- **桁()**関数は、セル参照の列番号を返します。参照を省略した場合は、COLUMN関数が入力されたセルアドレスを参照したものとみなします。
- **モッド()**関数は、数値を除数で割った後の剰余を返します。ここで、関数の最初のパラメーターは、求めたい剰余の数値であり、2 番目のパラメーターは、number パラメーターへの除算に使用される数値です。除数が 0 の場合、#DIV/0 が返されます。エラー。
Aspose.Cells for Java API の助けを借りて、目標を達成するためのコードを書き始めましょう。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(ApplyShadingToAlternateRowsAndColumns.class); | |
/* | |
* Create an instance of Workbook Optionally load an existing spreadsheet by passing its stream or path to Workbook | |
* constructor | |
*/ | |
Workbook book = new Workbook(); | |
// Access the Worksheet on which desired rule has to be applied | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Add FormatConditions to the instance of Worksheet | |
int index = sheet.getConditionalFormattings().add(); | |
// Access the newly added FormatConditions via its index | |
FormatConditionCollection conditionCollection = sheet.getConditionalFormattings().get(index); | |
// Define a CellsArea on which conditional formatting will be applicable | |
CellArea area = CellArea.createCellArea("A1", "I20"); | |
// Add area to the instance of FormatConditions | |
conditionCollection.addArea(area); | |
// Add a condition to the instance of FormatConditions. For this case, the condition type is expression, which is based on | |
// some formula | |
index = conditionCollection.addCondition(FormatConditionType.EXPRESSION); | |
// Access the newly added FormatCondition via its index | |
FormatCondition formatCondirion = conditionCollection.get(index); | |
// Set the formula for the FormatCondition. Formula uses the Excel's built-in functions as discussed earlier in this | |
// article | |
formatCondirion.setFormula1("=MOD(ROW(),2)=0"); | |
// Set the background color and patter for the FormatCondition's Style | |
formatCondirion.getStyle().setBackgroundColor(Color.getBlue()); | |
formatCondirion.getStyle().setPattern(BackgroundType.SOLID); | |
// Save the result on disk | |
book.save(dataDir + "output.xlsx"); |
次のスナップショットは、Excel アプリケーションに読み込まれた結果のスプレッドシートを示しています。
シェーディングを別の列に適用するには、式を変更するだけです**=MOD(ROW(),2)=0**なので**=MOD(COLUMN(),2)=0** 、 あれは;行インデックスを取得する代わりに、式を変更して列インデックスを取得します。 この場合、結果のスプレッドシートは次の画像のようになります。