Formülleri Hesaplama Yolları

Giriş

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ın hesaplanmasını da destekler.

Formül Ekleme ve Sonuçları Hesaplama

Aspose.Cells, Microsoft Excel’in parçası olan formüllerin veya işlevlerin çoğunu destekler. API aracılığıyla veya tasarımcı elektronik tabloları kullanılarak kullanılabilirler. Aspose.Cells, çok sayıda matematiksel, dizi, boole, tarih/saat, istatistik, arama ve referans formüllerini destekler.

Bir hücreye formül eklemek için Cell.Formula yöntemini kullanın. Bir hücreye formül uygularken, Microsoft Excel’de formül oluştururken yaptığınız gibi dizeye her zaman eşittir işaretiyle (=) başlayın. İşlev parametrelerini ayırmak için virgül (,) kullanın.

Formüllerin sonuçlarını hesaplamak için, bir Excel dosyasına katıştırılmış tüm formülleri işleyen Workbook.CalculateFormula() yöntemini çağırın. Lütfen formülü ekleyen ve sonuçlarını hesaplayan aşağıdaki örnek koda bakın. lütfen kontrol edinizçıktı excel dosyası bu kod ile oluşturulmuştur.

Basit kod

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Output directory path
StringPtr outPath = new String("..\\Data\\Output\\");
//Path of output excel file
StringPtr outputAddingFormulasAndCalculatingResults = outPath->StringAppend(new String("outputAddingFormulasAndCalculatingResults.xlsx"));
//Create workbook
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook();
//Access first worksheet in the workbook
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0);
//Adding integer values to cells A1, A2 and A3
ws->GetICells()->GetObjectByIndex(new String("A1"))->PutValue(10);
ws->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(20);
ws->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(70);
//Adding a SUM formula to "A4" cell
ws->GetICells()->GetObjectByIndex(new String("A4"))->SetFormula(new String("=SUM(A1:A3)"));
//Calculating the results of formulas
wb->CalculateFormula();
//Get the calculated value of the cell
intrusive_ptr<String> strVal = ws->GetICells()->GetObjectByIndex(new String("A4"))->GetStringValue();
//Print the calculated value on console
StringPtr str1 = new String("Calculated Result: ");
Console::WriteLine(str1->StringAppend(strVal));
//Saving the workbook
wb->Save(outputAddingFormulasAndCalculatingResults);

Formülün Doğrudan Hesaplanması

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.

Bu tür formüllerin sonuçlarını çalışma sayfasına eklemeden hesaplamak için Worksheet.CalculateFormula(String formula) yöntemini kullanabilirsiniz.

Aşağıdaki kod aşağıdaki çıktıyı üretir.

 Value of A1: 20

Value of A2: 30

Result of Sum(A1:A2): 50

Basit kod

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Create workbook
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook();
//Access first worksheet in the workbook
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0);
//Put 20 in cell A1
intrusive_ptr<ICell> cellA1 = ws->GetICells()->GetObjectByIndex(new String("A1"));
cellA1->PutValue(20);
//Put 30 in cell A2
intrusive_ptr<ICell> cellA2 = ws->GetICells()->GetObjectByIndex(new String("A2"));
cellA2->PutValue(30);
//Calculate the Sum of A1 and A2
intrusive_ptr<Aspose::Cells::System::Object> results = ws->CalculateFormula(new String("=Sum(A1:A2)"));
//Print the output
StringPtr str1 = new String("Value of A1: ");
Console::WriteLine(str1->StringAppend(cellA1->GetStringValue()));
StringPtr str2 = new String("Value of A2: ");
Console::WriteLine(str2->StringAppend(cellA2->GetStringValue()));
StringPtr str3 = new String("Result of Sum(A1:A2): ");
Console::WriteLine(str3->StringAppend(results->ToString()));

Formülleri Yalnızca Bir Kez Hesaplama

Bir çalışma kitabı şablonundaki formüllerin değerlerini hesaplamak için Workbook.CalculateFormula() çağrıldığında, Aspose.Cells bir hesaplama zinciri oluşturur. Formüller ikinci veya üçüncü kez hesaplandığında performansı artırır.

Bununla birlikte, şablon çok sayıda formül içeriyorsa, formülün ilk kez hesaplanması çok fazla CPU işlem süresi ve bellek tüketebilir.

Aspose.Cells, formülleri yalnızca bir kez hesaplamak istediğinizde kullanışlı olan bir hesaplama zinciri oluşturmayı kapatmanıza olanak tanır.

Lütfen Workbook.GetISettings().SetCreateCalcChain() öğesini false parametresiyle çağırın. kullanabilirsinizsağlanan excel dosyası Bu kodu test etmek için.

Basit kod

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
StringPtr dirPath = new String("..\\Data\\Formulas\\");
//Path of input excel file
StringPtr sampleCalculatingFormulasOnceOnly = dirPath->StringAppend(new String("sampleCalculatingFormulasOnceOnly.xlsx"));
//Create workbook
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(sampleCalculatingFormulasOnceOnly);
//Set the CreateCalcChain as false
wb->GetISettings()->SetCreateCalcChain(false);
//Get the time in milliseconds before formula calculation
int before_miliseconds = Aspose::Cells::System::DateTime::GetNow()->GetMillisecond();
//Calculate the workbook formulas
wb->CalculateFormula();
//Get the time in milliseconds after formula calculation
int after_miliseconds = Aspose::Cells::System::DateTime::GetNow()->GetMillisecond();
//Print the difference in milliseconds
StringPtr str1 = new String("Workbook Formula Calculation Elapsed Time in Milliseconds: ");
Console::WriteLine(str1->StringAppend(Int32Helper::ToString(after_miliseconds - before_miliseconds)));