Formülleri Hesapla

Formüller Ekleyin ve Sonuçlarını Hesaplayın

Aspose.Cells, yerleşik bir formül hesaplama motoruna sahiptir. Sadece tasarımcı şablonlarından içe aktarılan formülleri yeniden hesaplayabileceği gibi, çalışma zamanında eklenen formül sonuçlarını da hesaplama desteği sağlar.

Aspose.Cells, Microsoft Excel’in bir parçası olan çoğu formülü veya işlevi destekler (Formül Motoru tarafından desteklenen işlevlerin bir listesini okuyun). Bu işlevler, API’ler veya tasarımcı elektronik tabloları aracılığıyla kullanılabilir. Aspose.Cells, matematiksel, dize, mantıksal, tarih/saat, istatistiksel, veritabanı, arama ve referans formüllerinin geniş bir setini destekler.

Bir hücreye formül eklemek için Cell sınıfının Formula özelliğini veya SetFormula(…) yöntemlerini kullanın. Bir formül uygularken, her zaman bir eşittir işareti ile ( = ) başlayın, Microsoft Excel’de bir formül oluştururken yaptığınız gibi ve bir virgül ( , ) kullanarak fonksiyon parametrelerini sınırlayın.

Formüllerin sonuçlarını hesaplamak için, kullanıcı, Workbook sınıfının CalculateFormula yöntemini çağırabilir, bir Excel dosyasına gömülü olan tüm formülleri işleyen. Veya, kullanıcı, Worsheet sınıfının CalculateFormula yöntemini çağırabilir, bir sayfaya gömülü olan tüm formülleri işleyen. Ya da, kullanıcı, Cell sınıfının Calculate yöntemini çağırabilir, bir Hücrenin formülünü işleyen:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CalculatingFormulas.class) + "formulas/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a value to "A1" cell
worksheet.getCells().get("A1").putValue(1);
// Adding a value to "A2" cell
worksheet.getCells().get("A2").putValue(2);
// Adding a value to "A3" cell
worksheet.getCells().get("A3").putValue(3);
// Adding a SUM formula to "A4" cell
worksheet.getCells().get("A4").setFormula("=SUM(A1:A3)");
// Calculating the results of formulas
workbook.calculateFormula();
// Get the calculated value of the cell
String value = worksheet.getCells().get("A4").getStringValue();
// Saving the Excel file
workbook.save(dataDir + "CalculatingFormulas_out.xls");

Bilinmesi Gerekenler

Formülün Doğrudan Hesaplanması

Aspose.Cells, gömülü bir formül hesaplama motoruna sahiptir. Bir tasarımcı dosyasından içe aktarılmış formülleri hesaplamanın yanı sıra, Aspose.Cells, formül sonuçlarını doğrudan hesaplamayı da destekler.

Bazen, bir elektronik tabloya formül eklemadan, formülde kullanılan hücre değerlerinin sonuçlarını, Microsoft Excel formülüne dayalı olarak bulmanız gerekebilir. Formülde kullanılan hücrelerin değerleri zaten bir elektronik tabloda mevcutsa ve ihtiyacınız olan tek şey bir elektronik tabloya formül eklemek değilse, Aspose.Cells’in formül hesaplama motoru API’lerini kullanarak bu tür formüllerin sonuçlarını {0} için {1} hesaplayabilirsiniz.

Yukarıdaki kod aşağıdaki çıktıyı üretir:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(DirectCalculationFormula.class) + "formulas/";
// Create a workbook
Workbook workbook = new Workbook();
// Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Put 20 in cell A1
Cell cellA1 = worksheet.getCells().get("A1");
cellA1.putValue(20);
// Put 30 in cell A2
Cell cellA2 = worksheet.getCells().get("A2");
cellA2.putValue(30);
// Calculate the Sum of A1 and A2
Object results = worksheet.calculateFormula("=Sum(A1:A2)");
// Print the output
System.out.println("Value of A1: " + cellA1.getStringValue());
System.out.println("Value of A2: " + cellA2.getStringValue());
System.out.println("Result of Sum(A1:A2): " + results.toString());

Formülleri Tekrarlı Hesaplamak İçin Nasıl

Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50.0

Formüllerin Tekrarlayarak Hesaplanması

Çalışma kitabında çok sayıda formül bulunduğunda ve kullanıcının bunların bir kısmını tekrarlanacak şekilde sık sık hesaplaması gerektiğinde, formül hesaplama zincirini etkinleştirmek, yalnızca bir elektronik tablo kısmını değiştirerek tekrarlamak için formül hesaplama zincirini etkinleştirmek performans için yararlı olabilir: FormulaSettings.EnableCalculationChain.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CalculatingFormulasOnce.class) + "formulas/";
// Load the template workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Print the time before formula calculation
System.out.println(DateTime.getNow());
// Set the CreateCalcChain as true
workbook.getSettings().getFormulaSettings().setEnableCalculationChain(true);
// Calculate the workbook formulas
workbook.calculateFormula();
Cells cells = workbook.getWorksheets().get("Sheet1").getCells();
//with original values, the calculated result
System.out.println(cells.get("A11").getValue());
//update one value the formula depends on
cells.get("A5").putValue(15);
// Calculate the workbook formulas again, in fact only A11 needs to be and will be calculated
workbook.calculateFormula();
//check the re-calculated value
System.out.println(cells.get("A11").getValue());
// Print the time after formula calculation
System.out.println(DateTime.getNow());

Bilinmesi Gerekenler

Gelişmiş Konular