Formülleri Hesapla

Formül Ekleme ve Sonuçları Hesaplama

Aspose.Cells yerleşik bir formül hesaplama motoruna sahiptir. Yalnızca tasarımcı şablonlarından içe aktarılan formülleri yeniden hesaplamakla kalmaz, aynı zamanda çalışma zamanında eklenen formüllerin sonuçlarını hesaplamayı da destekler.

Aspose.Cells, Microsoft Excel’in parçası olan formüllerin veya işlevlerin çoğunu destekler (Okuhesaplama motoru tarafından desteklenen işlevlerin bir listesi). Bu işlevler, API’ler veya tasarımcı elektronik tabloları aracılığıyla kullanılabilir. Aspose.Cells, çok sayıda matematiksel, dizi, boole, tarih/saat, istatistik, veritabanı, arama ve referans formüllerini destekler.

Kullanformül mülk veyaFormülü Ayarla(…) yöntemleriCellhücreye formül eklemek için sınıf. Bir formülü uygularken, Microsoft Excel’de formül oluştururken yaptığınız gibi dizeye her zaman eşittir işaretiyle (=) başlayın ve işlev parametrelerini ayırmak için virgül (,) kullanın.

Formüllerin sonuçlarını hesaplamak için kullanıcıHesaplaFormül yöntemiÇalışma kitabıbir Excel dosyasına katıştırılmış tüm formülleri işleyen sınıf. Veya, kullanıcı arayabilirHesaplaFormül yöntemiWorsheet bir sayfaya katıştırılmış tüm formülleri işleyen sınıf. Veya kullanıcı ayrıcaHesaplamak yöntemiCellbir Cell formülünü işleyen sınıf:

// 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");

Bilmeniz Önemli

Formülün Doğrudan Hesaplanması

Aspose.Cells yerleşik bir formül hesaplama motoruna sahiptir. Aspose.Cells, bir tasarımcı dosyasından içe aktarılan formülleri hesaplamanın yanı sıra, formül sonuçlarını doğrudan hesaplayabilir.

Bazen, formül sonuçlarını bir çalışma sayfasına eklemeden doğrudan hesaplamanız gerekir. Formülde kullanılan hücrelerin değerleri bir çalışma sayfasında zaten var ve ihtiyacınız olan tek şey, formülü bir çalışma sayfasına eklemeden bazı Microsoft Excel formüllerine dayalı olarak bu değerlerin sonucunu bulmak.

Aspose.Cells' formül hesaplama motoru API’lerini aşağıdakiler için kullanabilirsiniz:Çalışma kağıdı ilehesaplamak çalışma sayfasına eklemeden bu tür formüllerin sonuçları:

// 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());

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

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

Formülleri tekrar tekrar hesaplamak

Çalışma kitabında çok sayıda formül olduğunda ve kullanıcının bunları yalnızca küçük bir bölümünü değiştirerek tekrar tekrar hesaplaması gerektiğinde, formül hesaplama zincirini etkinleştirmek performans açısından 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());

Bilmeniz Önemli

ileri konular