共有式の設定
次のサンプル ワークシートのような形式のデータが入力されたワークシートがあるとします。
1 つの列またはデータを含む入力ファイル
データの最初の行の消費税を計算する関数を B2 に追加します。税金は9% .売上税を計算する式は次のとおりです。「=A2*0.09」 .この記事では、この式を Aspose.Cells に適用する方法について説明します。
Aspose.Cells を使用すると、式を指定できますCell.Formula プロパティ、具体的にはCell.set式() とCell.get式() .
列の他のセル (B3、B4、B5 など) に数式を追加するには、2 つのオプションがあります。
最初のセルに対して行ったことを実行し、各セルの数式を効果的に設定し、それに応じてセル参照を更新します (A30.09、A4 0.09、A5*0.09 など)。これには、各行のセル参照を更新する必要があります。また、各数式を個別に解析するには Aspose.Cells が必要です。これは、大規模なスプレッドシートや複雑な数式では時間がかかる可能性があります。また、コードの余分な行を追加しますが、ループを使用すると多少削減できます。
別のアプローチは、共有式 .共有数式を使用すると、税金が適切に計算されるように、各行のセル参照の数式が自動的に更新されます。の[Cell.setSharedFormula](https://reference.aspose.com/cells/java/com.aspose.cells/cell#setSharedFormula (java.lang.String,%20int,%20int)メソッドは、最初のメソッドよりも効率的です。
次の例は、その使用方法を示しています。以下のスクリーンショットは、出力ファイルを示しています。
出力ファイル: 共有数式を適用
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(SettingSharedFormula.class);
String filePath = dataDir + "input.xlsx";
// Instantiate a Workbook from existing file
Workbook workbook = new Workbook(filePath);
// Get the cells collection in the first worksheet
Cells cells = workbook.getWorksheets().get(0).getCells();
// Apply the shared formula in the range i.e.., B2:B14
cells.get("B2").setSharedFormula("=A2*0.09", 13, 1);
// Save the excel file
workbook.save(dataDir + "output.xlsx", SaveFormat.XLSX);