Özel fonksiyonun doğrudan hesaplanması, çalışma sayfasına yazmadan Node.js aracılığıyla C++
Özel işlevin çalışma tablosuna yazılmadan doğrudan hesaplanması
Bu konu, özel fonksiyonlarınızı öncelikle çalışma sayfasına yazmadan doğrudan nasıl hesaplayabileceğinizi açıklar. Lütfen bu amaçla Worksheet.calculateFormula(formula, opts) metodunu kullanın.
Bu metodu kullanımını gösteren aşağıdaki örnek kodu inceleyin. MyCompany.CustomFunction() adında özel bir işlev kullandık ve bu işlevin değerini “Aspose.Cells.” olarak kendimiz hesapladık ve bu değer otomatik olarak hesaplama motoru tarafından “Hoş geldiniz " olan A1 hücresinin değeri ile birleştirildi ve son hesaplanmış değer olarak “Hoş geldiniz Aspose.Cells.” olarak döndü. Kodun içinde özel bir işlevimizi çalıştırmadığımızı ve bunun yerine, özel mantığımızla doğrudan hesaplandığını görebilirsiniz.
Programlama Örneği
const AsposeCells = require("aspose.cells.node");
class CustomEngine extends AsposeCells.AbstractCalculationEngine {
// Override the Calculate method with custom logic
calculate(data) {
// Check the formula name and calculate it yourself
if (data.getFunctionName() === "MyCompany.CustomFunction") {
// This is our calculated value
data.setCalculatedValue("Aspose.Cells.");
}
}
}
class ImplementDirectCalculationOfCustomFunction {
static run() {
// Create a workbook
const wb = new AsposeCells.Workbook();
// Access first worksheet
const ws = wb.getWorksheets().get(0);
// Add some text in cell A1
ws.getCells().get("A1").putValue("Welcome to ");
// Create a calculation options with custom engine
const opts = new AsposeCells.CalculationOptions();
opts.setCustomEngine(new CustomEngine());
// This line shows how you can call your own custom function without
// a need to write it in any worksheet cell
// After the execution of this line, it will return
// Welcome to Aspose.Cells.
const ret = ws.calculateFormula("=A1 & MyCompany.CustomFunction()", opts);
// Print the calculated value
console.log("Calculated Value: " + ret);
}
}
// Example invocation
ImplementDirectCalculationOfCustomFunction.run();
Konsol Çıktısı
Yukarıdaki örnek kodun konsol çıktısı aşağıdaki gibidir.
Calculated Value: Welcome to Aspose.Cells.