Formülleri C++ ile Hesapla

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

Aspose.Cells gömülü bir formül hesaplama motoruna sahiptir. Tasarımcı şablonlarından ithal edilen formülleri tekrar hesaplamanın yanı sıra, çalışma zamanında eklenen formüllerin sonuçlarını da hesaplamayı destekler.

Aspose.Cells, Microsoft Excel’in (desteklenen fonksiyonların listesini kullanılabilen fonksiyonların listesine göz at) parçası olan çoğu formülü veya fonksiyonu destekler. Bu fonksiyonlar API’ler veya tasarımcı elektronik tablolar aracılığıyla kullanılabilir. Aspose.Cells, büyük bir matematiksel, dize, mantıksal, tarih/zaman, istatistiksel, veritabanı, arama ve referans formülleri kümesine destek sağlar.

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

Formüllerin sonuçlarını hesaplamak için, kullanıcı Workbook sınıfının CalculateFormula yöntemini çağırabilir, bu tüm Excel dosyasına gömülü formülleri işler. Alternatif olarak, Worksheet sınıfının CalculateFormula yöntemini çağırabilir, bu da bir sayfadaki tüm formülleri işler. Ya da, Cell sınıfının Calculate yöntemini çağırabilir, bu da bir Hücre’nin formülünü işler:

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Create a new workbook
    Workbook workbook;

    // Add a new worksheet to the workbook
    int sheetIndex = workbook.GetWorksheets().Add();

    // Get the reference of the newly added worksheet
    Worksheet worksheet = workbook.GetWorksheets().Get(sheetIndex);

    // Add values to cells
    worksheet.GetCells().Get(u"A1").PutValue(1);
    worksheet.GetCells().Get(u"A2").PutValue(2);
    worksheet.GetCells().Get(u"A3").PutValue(3);

    // Add a SUM formula to cell A4
    worksheet.GetCells().Get(u"A4").SetFormula(u"=SUM(A1:A3)");

    // Calculate the results of formulas
    workbook.CalculateFormula();

    // Get the calculated value of cell A4
    U16String value = worksheet.GetCells().Get(u"A4").GetStringValue();

    // Save the Excel file
    workbook.Save(outDir + u"output.xls");

    std::cout << "Excel file saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Formüller İçin 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.

B sometimes, doğrudan formül sonuçlarını hesaplamanız gerekebilir, ve bu sonuçları çalışma sayfasına eklemeden de yapabilirsiniz. Formülde kullanılan hücrelerin değerleri zaten bir çalışma sayfasında mevcuttur ve sizin yapmanız gereken, bu değerlerin sonucunu Microsoft Excel formülleri kullanarak bulmaktır, formülü çalışma sayfasına ekmeden.

Aspose.Cells’ın formül hesaplama motoru API’lerini kullanarak, Worksheet ile calculate arasındaki formüllerin sonuçlarını, bunları çalışma sayfasına eklemeden alabilirsiniz:

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Create a workbook
    Workbook workbook;

    // Access first worksheet
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Put 20 in cell A1
    Cell cellA1 = worksheet.GetCells().Get(u"A1");
    cellA1.PutValue(20);

    // Put 30 in cell A2
    Cell cellA2 = worksheet.GetCells().Get(u"A2");
    cellA2.PutValue(30);

    // Calculate the Sum of A1 and A2
    Aspose::Cells::Object results = worksheet.CalculateFormula(u"=Sum(A1:A2)");

    // Print the output
    std::cout << "Value of A1: " << cellA1.GetStringValue().ToUtf8() << std::endl;
    std::cout << "Value of A2: " << cellA2.GetStringValue().ToUtf8() << std::endl;
    std::cout << "Result of Sum(A1:A2): " << results.ToString().ToUtf8() << std::endl;

    Aspose::Cells::Cleanup();
}

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

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

Formülleri Tekrar Hesaplama Yöntemleri

Çok sayıda formül olduğunda ve kullanıcının sadece küçük bir kısmını değiştirerek tekrar tekrar hesaplaması gerekirse, performans için formül hesaplama zincirini etkinleştirmek faydalı olabilir: FormulaSettings.GetEnableCalculationChain().

#include <iostream>
#include <chrono>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Load the template workbook
    Workbook workbook(srcDir + u"book1.xls");

    // Print the time before formula calculation
    auto start = std::chrono::system_clock::now();
    std::time_t start_time = std::chrono::system_clock::to_time_t(start);
    std::cout << "Start time: " << std::ctime(&start_time);

    // Set the CreateCalcChain as true
    workbook.GetSettings().GetFormulaSettings().SetEnableCalculationChain(true);

    // Calculate the workbook formulas
    workbook.CalculateFormula();

    // Print the time after formula calculation
    auto end = std::chrono::system_clock::now();
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
    std::cout << "End time: " << std::ctime(&end_time);

    // Change the value of one cell
    workbook.GetWorksheets().Get(0).GetCells().Get(u"A1").PutValue(u"newvalue");

    // Re-calculate those formulas which depend on cell A1
    workbook.CalculateFormula();

    Aspose::Cells::Cleanup();
    return 0;
}

Bilinmesi Gerekenler

Gelişmiş Konular