共有式の設定
いくつかの計算を行う関数をワークシートに追加したい場合。この記事では、Aspose.Cells を使用してこのタスクを達成する方法について説明します。
Aspose.Cells を使用して共有数式を設定する
次のサンプル ワークシートのような形式のデータが入力されたワークシートがあるとします。
1 つの列またはデータを含む入力ファイル
データの最初の行の消費税を計算する関数を B2 に追加します。税金は9% .売上税を計算する式は次のとおりです。「=A2*0.09」 .この記事では、この式を Aspose.Cells に適用する方法について説明します。
Aspose.Cells を使用すると、式を指定できますCell.Formula 財産。列の他のセル (B3、B4、B5 など) に数式を追加するには、2 つのオプションがあります。
最初のセルに対して行ったことを実行し、各セルの数式を効果的に設定し、それに応じてセル参照を更新します (A30.09、A4 0.09、A5*0.09 など)。これには、各行のセル参照を更新する必要があります。また、各数式を個別に解析するには Aspose.Cells が必要です。これは、大規模なスプレッドシートや複雑な数式では時間がかかる可能性があります。また、コードの余分な行を追加しますが、ループを使用すると多少削減できます。
別のアプローチは、共有式 .共有数式を使用すると、税金が適切に計算されるように、各行のセル参照の数式が自動的に更新されます。のCell.SetSharedFormula この方法は、最初の方法よりも効率的です。
次の例は、その使用方法を示しています。
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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook from existing file
Workbook workbook = new Workbook(dataDir + "source.xlsx");
// Get the cells collection in the first worksheet
Cells cells = workbook.Worksheets[0].Cells;
// Apply the shared formula in the range i.e.., B2:B14
cells["B2"].SetSharedFormula("=A2*0.09", 13, 1);
// Save the excel file
workbook.Save(dataDir + "Output_out.xlsx", SaveFormat.Xlsx);