Question
Determining Student Pass/Incomplete Status Script
- November 22, 2024
- 1 reply
- 192 views
Hello Again
I have a table of Studen'ts Results
I asked Claudia AI to write a script
If a pupil doesn't have any degree less than 5 then he is "ناجح"
If a pupil have a degree or two less than 5 then he is "مكمل من"
If a pupil have three degrees less than 5 then he is "راسب"
I asked AI to write a result in the last row of the selected row/rows
But it didn't work
Any help to make it work?
Demo document in the attachments
Thanks in advanced!
// تأكد من وجود مستند نشط
if (app.documents.length > 0) {
var myDoc = app.activeDocument;
// التحقق من وجود تحديد
if (app.selection.length > 0) {
try {
// الحصول على الخلايا المحددة
var selectedCells = app.selection[0].cells;
// التكرار على كل صف محدد
var currentRow = selectedCells[0].rowIndex;
var rowGrades = [];
var resultCell;
for (var i = 0; i < selectedCells.length; i++) {
var cell = selectedCells[i];
// إذا انتقلنا لصف جديد
if (cell.rowIndex !== currentRow) {
// تحديد النتيجة للصف السابق
processGrades(rowGrades, resultCell);
// بدء صف جديد
currentRow = cell.rowIndex;
rowGrades = [];
}
// تخزين آخر خلية كخلية النتيجة
if (i === selectedCells.length - 1 ||
selectedCells[i + 1].rowIndex !== currentRow) {
resultCell = cell;
} else {
// تحويل محتوى الخلية إلى رقم وإضافته للدرجات
var grade = parseFloat(cell.contents.replace(/[^\d.-]/g, ''));
if (!isNaN(grade)) {
rowGrades.push(grade);
}
}
}
// معالجة آخر صف
if (rowGrades.length > 0) {
processGrades(rowGrades, resultCell);
}
} catch (e) {
alert("خطأ: " + e);
}
} else {
alert("الرجاء تحديد الخلايا في الجدول أولاً");
}
} else {
alert("الرجاء فتح مستند InDesign أولاً");
}
// دالة لمعالجة الدرجات وكتابة النتيجة
function processGrades(grades, resultCell) {
if (!grades.length || !resultCell) return;
// حساب عدد الدرجات أقل من 5
var failedCount = grades.filter(function(grade) {
return grade < 5;
}).length;
// تحديد النتيجة
var result = "";
var resultColor;
if (failedCount === 0) {
result = "ناجح";
resultColor = [100, 0, 100, 0]; // CMYK أخضر
} else if (failedCount === 1) {
result = "مكمل";
resultColor = [0, 50, 100, 0]; // CMYK برتقالي
} else if (failedCount === 2) {
result = "مكمل";
resultColor = [0, 50, 100, 0]; // CMYK برتقالي
} else {
result = "راسب";
resultColor = [0, 100, 100, 0]; // CMYK أحمر
}
// إضافة "من" للمكمل
if (result === "مكمل") {
result = "مكمل من " + failedCount;
}
// كتابة النتيجة في الخلية
resultCell.contents = result;
// تنسيق النص
var resultText = resultCell.texts[0];
resultText.pointSize = 12;
resultText.justification = Justification.CENTER_ALIGN;
// تعريف الألوان إذا لم تكن موجودة
try {
var colorName = "نتيجة " + (result.indexOf("مكمل") >= 0 ? "مكمل" : result);
var resultColorObj = myDoc.colors.itemByName(colorName);
if (!resultColorObj.isValid) {
resultColorObj = myDoc.colors.add({
name: colorName,
model: ColorModel.PROCESS,
colorValue: resultColor
});
}
resultText.fillColor = resultColorObj;
} catch (e) {
// في حالة وجود خطأ في إنشاء اللون
}
}