Aspose.Cells Varsayılan Hesaplama Motorunu genişletmek için Özel Hesaplama Motorunu uygulayın
Contents
[
Hide
]
Aspose.Cells, Microsoft Excel formüllerinin neredeyse tamamını hesaplayabilen güçlü bir hesaplama motoruna sahiptir. Buna rağmen, size daha fazla güç ve esneklik sağlayan varsayılan hesaplama motorunu genişletmenize de izin verir.
Bu özelliğin uygulanmasında aşağıdaki özellik ve sınıflar kullanılır.
Özel Hesaplama Motorunu Uygulayın
Aşağıdaki kod, Özel Hesaplama Motorunu uygular. Arayüzü uygularSoyutHesaplamaMotoru tek bir yöntemi olanhesapla(HesaplamaVeri verileri). Bu yöntem, tüm formüllerinize karşı çağrılır. Bu yöntemin içinde,TOPLAM formülünü kullanır ve değerini 30 artırır. Yani Aspose.Cells hesaplanan değer 20 ise özel motorumuz 30 ekleyerek 50 yapar.
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-Java | |
public class CustomEngine extends AbstractCalculationEngine { | |
public void calculate(CalculationData data) { | |
if (data.getFunctionName().toUpperCase().equals("TODAY")) { | |
data.setCalculatedValue(CellsHelper.getDoubleFromDateTime(DateTime.getNow(), false) + 1.0); | |
} | |
} | |
public getProcessBuiltInFunctions() { return true; } | |
} | |
public static void main(String[] args) throws Exception { | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ImplementCustomCalculationEngine.class); | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
Cell a1 = sheet.getCells().get("A1"); | |
Style style = cell.getStyle(); | |
style.setNumber(14); | |
cell.setStyle(style); | |
a1.setFormula("=TODAY()"); | |
// Without Custom Engine, the value of cell A1 will be 20 | |
workbook.calculateFormula(); | |
System.out.println("Without Custom Engine Value of A1: " + a1.getStringValue()); | |
// With Custom Engine, the value of cell A1 will be 50 | |
CalculationOptions opts = new CalculationOptions(); | |
opts.setCustomEngine(new CustomEngine()); | |
workbook.calculateFormula(opts); | |
System.out.println("With Custom Engine Value of A1: " + a1.getStringValue()); | |
} |
Konsol Çıkışı
İşte yukarıdaki örnek kodun konsol çıktısı.
Without Custom Engine Value of A1: 20
With Custom Engine Value of A1: 50