حساب الصيغ باستخدام JavaScript عبر C++
إضافة صيغ وحساب النتائج
تحتوي Aspose.Cells على محرك حساب الصيغ مدمج. فهي لا يمكنها فقط إعادة حساب الصيغ المستوردة من قوالب المصممين، بل تدعم أيضًا حساب نتائج الصيغ المضافة في وقت التشغيل.
يدعم Aspose.Cells معظم الصيغ أو الوظائف التي تعتبر جزءًا من Microsoft Excel (اقرأ قائمة الوظائف المدعومة من قبل محرك الحسابات). يمكن استخدام تلك الوظائف عبر واجهات برمجة التطبيقات أو جداول التصميم. يدعم Aspose.Cells مجموعة واسعة من الصيغ الرياضية، النصية، المنطقية، للتاريخ/الوقت، الإحصائية، قواعد البيانات، البحث، والمرجعية.
استخدم خاصية formula أو أساليب formula(string, object) من فئة Cell لإضافة صيغة إلى خليّة. عند تطبيق صيغة، ابدأ النص بعلامة يساوي (=) كما تفعل عند إنشاء صيغة في Microsoft Excel، واستخدم فاصلة (,) لفصل معلمات الدالة.
لحساب نتائج الصيغ، يمكن للمستخدم استدعاء طريقة calculateFormula() من فئة Workbook التي تعالج جميع الصيغ المدمجة في ملف Excel. أو، يمكن للمستخدم استدعاء طريقة calculateFormula(string) من فئة Worksheet التي تعالج جميع الصيغ المدمجة في ورقة. أو، يمكن للمستخدم أيضًا استدعاء طريقة calculate(CalculationOptions) من فئة Cell التي تعالج صيغة خلية واحدة:
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
// Instantiate a new Workbook object
const workbook = new Workbook();
// Add a new worksheet to the Excel object
const sheetIndex = workbook.worksheets.add();
// Obtain the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.worksheets.get(sheetIndex);
// Adding a value to "A1" cell
const cellA1 = worksheet.cells.get("A1");
cellA1.value = 1;
// Adding a value to "A2" cell
const cellA2 = worksheet.cells.get("A2");
cellA2.value = 2;
// Adding a value to "A3" cell
const cellA3 = worksheet.cells.get("A3");
cellA3.value = 3;
// Adding a SUM formula to "A4" cell
const cellA4 = worksheet.cells.get("A4");
cellA4.formula = "=SUM(A1:A3)";
// Calculating the results of formulas
workbook.calculateFormula();
// Get the calculated value of the cell
const value = worksheet.cells.get("A4").value.toString();
// Saving the Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = `<p style="color: green;">Operation completed successfully! Calculated value in A4: ${value}. Click the download link to get the file.</p>`;
});
</script>
</html>
مهم معرفته حول الصيغ
حساب مباشر للصيغ
Aspose.Cells لديه محرك حساب مضمن للصيغ. بالإضافة إلى حساب الصيغ المستوردة من ملف مصمم، يمكن لـ Aspose.Cells حساب نتائج الصيغ مباشرة.
أحيانًا، تحتاج إلى حساب نتائج الصيغ مباشرة دون إضافتها إلى ورقة عمل. القيم المستخدمة في الصيغة موجودة بالفعل في ورقة عمل، وكل ما تحتاج إليه هو العثور على النتيجة لتلك القيم بناءً على صيغة Microsoft Excel دون إضافة الصيغة في ورقة عمل.
يمكنك استخدام واجهات برمجة تطبيقات محرك حساب الصيغ الخاص بـ Aspose.Cells لـ Worksheet إلى calculateFormula(string, FormulaParseOptions, CalculationOptions, number, number, CalculationData) لحساب نتائج مثل هذه الصيغ دون إضافتها إلى ورقة العمل:
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - Calculate Sum</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
// Create or load workbook
let workbook;
if (fileInput.files && fileInput.files.length > 0) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Put 20 in cell A1
const cellA1 = worksheet.cells.get("A1");
cellA1.value = 20;
// Put 30 in cell A2
const cellA2 = worksheet.cells.get("A2");
cellA2.value = 30;
// Calculate the Sum of A1 and A2
const results = worksheet.calculateFormula("=Sum(A1:A2)");
// Prepare output text
const outputHtml = [
`<p>Value of A1: ${cellA1.stringValue}</p>`,
`<p>Value of A2: ${cellA2.stringValue}</p>`,
`<p>Result of Sum(A1:A2): ${results.toString()}</p>`
].join("");
// Save the workbook to a downloadable file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = `<div style="color: green;">Operation completed successfully!</div>${outputHtml}`;
});
</script>
</body>
</html>
ينتج الكود أعلاه الناتج التالي:
Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50.0
كيفية حساب الصيغ بشكل متكرر
عندما يكون هناك الكثير من الصيغ في دفتر العمل، ويحتاج المستخدم إلى حسابها بشكل متكرر أثناء تعديل جزء صغير منها، قد يكون من المفيد تمكين سلسلة حساب الصيغة: formulaSettings.enableCalculationChain.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Calculate Formulas</title>
</head>
<body>
<h1>Calculate Formulas Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Print the time before formula calculation
console.log(new Date());
// Set the CreateCalcChain as true
workbook.settings.formulaSettings.enableCalculationChain = true;
// Calculate the workbook formulas
workbook.calculateFormula();
// Print the time after formula calculation
console.log(new Date());
// Change the value of one cell (A1 in first worksheet)
const worksheet = workbook.worksheets.get(0);
const cell = worksheet.cells.get("A1");
cell.value = "newvalue";
// Re-calculate those formulas which depend on cell A1
workbook.calculateFormula();
// Save the modified workbook and provide download link
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Formulas calculated and cell updated successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
مهم معرفته
مواضيع متقدمة
- إضافة الخلايا إلى نافذة مراقبة صيغ Microsoft Excel
- حساب وظيفة IFNA باستخدام Aspose.Cells
- حساب الصيغة الصفية للجداول البيانات
- حساب وظائف MINIFS و MAXIFS في Excel 2016
- تقليل زمن حساب طريقة Cell.calculate
- اكتشاف المراجعة الدائرية
- الحساب المباشر للوظيفة المخصصة دون كتابتها في ورقة العمل
- تنفيذ محرك الحساب المخصص لتوسيع محرك الحساب الافتراضي لـ Aspose.Cells
- إيقاف أو إلغاء حساب الصيغ في سجل العمل
- إرجاع مجموعة من القيم باستخدام AbstractCalculationEngine
- ضبط وضع حساب الصيغة في سجل العمل
- استخدام وظيفة FormulaText في Aspose.Cells