Skip to main content
Participating Frequently
November 20, 2024
Answered

InDesign Bulk Number to Arabic Text Script

  • November 20, 2024
  • 1 reply
  • 292 views

Hello

 

I have numbers in a coulum and I want a script to write the numbers to Arabic TEXT

 

I asked claude ai to write a script but it doesn't work

 

Any help to edit the script and make it work?

 

demo document in the attachments!

 

Thanks in advanced!

 

 

// تحويل الأرقام إلى كلمات باللغة العربية
var ones = ['', 'واحد', 'اثنتان', 'ثلاث', 'أربع', 'خمس', 'ست', 'سبع', 'ثمان', 'تسع', 'عشر',
    'إحدى عشر', 'اثنات عشرة', 'ثلاث عشرة', 'أربع عشرة', 'خمس عشرة', 'ست عشرة', 'سبع عشرة', 'ثمان عشرة', 'تسع عشرة'];
var tens = ['', '', 'عشرون', 'ثلاثون', 'أربعون', 'خمسون', 'ستون', 'سبعون', 'ثمانون', 'تسعون'];
var hundreds = ['', 'مائة', 'مئتان', 'ثلاثمائة', 'أربعمائة', 'خمسمائة', 'ستمائة', 'سبعمائة', 'ثمانمائة', 'تسعمائة'];

function numberToWords(num) {
    if (num === 0) return 'صفر';
    if (num === 1000) return 'ألف';
    
    var words = '';
    
    // معالجة المئات
    if (num >= 100) {
        var hundredsDigit = Math.floor(num / 100);
        words = hundreds[hundredsDigit];
        num = num % 100;
        
        if (num > 0) {
            words += ' و';
        }
    }
    
    // معالجة العشرات والآحاد
    if (num > 0) {
        // للأرقام من 1-19
        if (num < 20) {
            words += ones[num];
        }
        // للأرقام من 20-99
        else {
            var remainder = num % 10;
            var tensIndex = Math.floor(num / 10);
            
            if (remainder === 0) {
                words += tens[tensIndex];
            } else {
                words += ones[remainder] + ' و' + tens[tensIndex];
            }
        }
    }
    
    return words;
}

// الكود الرئيسي للتنفيذ في InDesign
try {
    if (app.documents.length > 0) {
        var doc = app.activeDocument;
        var selection = doc.selection;
        
        // التأكد من تحديد خلايا في الجدول
        if (selection.length > 0 && selection[0] instanceof Cell) {
            var firstCell = selection[0];
            var table = firstCell.parent;
            var selectedColumn = firstCell.columnIndex;
            var targetColumn = selectedColumn + 1;
            
            // التحقق من أن العمود المستهدف موجود في الجدول
            if (targetColumn < table.columns.length) {
                // معالجة كل صف في العمود المحدد
                for (var i = 0; i < selection.length; i++) {
                    var currentCell = selection[i];
                    var cellContent = currentCell.contents;
                    var number = parseInt(cellContent);
                    
                    // التحقق من أن المحتوى رقم صحيح
                    if (!isNaN(number) && number >= 0 && number <= 1000) {
                        // الحصول على الخلية المجاورة في نفس الصف
                        var targetCell = table.cells[currentCell.rowIndex][targetColumn];
                        // تحويل الرقم إلى كلمات وكتابته في الخلية المجاورة
                        targetCell.contents = numberToWords(number);
                    }
                }
                alert("تم تحويل الأرقام بنجاح!");
            } else {
                alert("لا يوجد عمود مجاور للكتابة فيه");
            }
        } else {
            alert("الرجاء تحديد الخلايا التي تحتوي على الأرقام في الجدول");
        }
    }
} catch (e) {
    alert("حدث خطأ: " + e);
}

 

 

 
This topic has been closed for replies.
Correct answer m1b

Hi @alma_3660, AI is amazing, but often gets code wrong, as in this case. I've made many changes to get it working, including adding a function I created for your other question.

 

Please try the following code. It puts the words into the table cell to the left of the selected cell. You can select multiple rows.

- Mark

 

 

/**
 * Convert numbers to words (Arabic version).
 * Puts converted number in the cell to the left of the selected cell.
 * @author Claude AI and m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/indesign-bulk-number-to-arabic-text-script/m-p/14992156
 */
function main() {

    // الكود الرئيسي للتنفيذ في InDesign
    if (0 === app.documents.length)
        return alert("الرجاء تحديد الخلايا التي تحتوي على الأرقام في الجدول");

    var doc = app.activeDocument;
    var selection = doc.selection;

    // التأكد من تحديد خلايا في الجدول
    if (
        selection.length === 0
        || !selection[0].hasOwnProperty('parentColumn')
    )
        return alert("الرجاء تحديد الخلايا التي تحتوي على الأرقام في الجدول");

    var rows = getCellsInRows(selection[0]);
    var targetColumnIndex = rows[0][0].parentColumn.index + 1;
    var table = rows[0][0].parent;

    while ('Table' !== table.constructor.name)
        table = table.parent;

    // التحقق من أن العمود المستهدف موجود في الجدول
    if (targetColumnIndex >= table.columns.length)
        // nowhere to put result
        return alert("لا يوجد عمود مجاور للكتابة فيه");

    // معالجة كل صف في العمود المحدد
    for (var r = 0, row, cell, num, targetCell; r < rows.length; r++) {

        row = rows[r];
        cell = row[0];
        num = parseInt(cell.contents);

        // التحقق من أن المحتوى رقم صحيح
        if (
            !isNaN(num)
            && num >= 0
            && num <= 1000
        ) {
            // الحصول على الخلية المجاورة في نفس الصف
            targetCell = table.rows[cell.parentRow.index].cells[targetColumnIndex];
            // تحويل الرقم إلى كلمات وكتابته في الخلية المجاورة
            targetCell.contents = numberToWords(num);
        }

    }

    // alert("تم تحويل الأرقام بنجاح!");

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Test Numbers To Words');

/**
 * Returns the number `num` converted into words.
 * @author Claude AI
 * @version 2024-11-21
 * @param {number} num - the number to convert.
 * @returns {String}
 */
function numberToWords(num) {

    // تحويل الأرقام إلى كلمات باللغة العربية
    const ones = ['', 'واحد', 'اثنتان', 'ثلاث', 'أربع', 'خمس', 'ست', 'سبع', 'ثمان', 'تسع', 'عشر', 'إحدى عشر', 'اثنات عشرة', 'ثلاث عشرة', 'أربع عشرة', 'خمس عشرة', 'ست عشرة', 'سبع عشرة', 'ثمان عشرة', 'تسع عشرة'],
        tens = ['', '', 'عشرون', 'ثلاثون', 'أربعون', 'خمسون', 'ستون', 'سبعون', 'ثمانون', 'تسعون'],
        hundreds = ['', 'مائة', 'مئتان', 'ثلاثمائة', 'أربعمائة', 'خمسمائة', 'ستمائة', 'سبعمائة', 'ثمانمائة', 'تسعمائة'];

    if (num === 0) return 'صفر';
    if (num === 1000) return 'ألف';

    var words = '';

    // معالجة المئات
    if (num >= 100) {
        var hundredsDigit = Math.floor(num / 100);
        words = hundreds[hundredsDigit];
        num = num % 100;

        if (num > 0) {
            words += ' و';
        }
    }

    // معالجة العشرات والآحاد
    if (num > 0) {
        // للأرقام من 1-19
        if (num < 20) {
            words += ones[num];
        }
        // للأرقام من 20-99
        else {
            var remainder = num % 10;
            var tensIndex = Math.floor(num / 10);

            if (remainder === 0) {
                words += tens[tensIndex];
            } else {
                words += ones[remainder] + ' و' + tens[tensIndex];
            }
        }
    }

    return words;

};

/**
 * Returns array of arrays of cells by row.
 * Useful when given a selection of cells
 * to divide into rows.
 * @author m1b
 * @version 2024-11-19
 * @param {Cell} cells - the cells to separate.
 * @returns {Array<Array<Cell>>?}
 */
function getCellsInRows(cells) {

    if (
        undefined == cells
        || 'Cell' !== cells.constructor.name
        || !cells.hasOwnProperty('cells')
        || 0 === cells.cells.length
    )
        return

    var cellsInRows = [],
        lookup = {};

    for (var i = 0; i < cells.cells.length; i++) {

        var rowIndex = cells.cells[i].parentRow.index;

        if (undefined == lookup[rowIndex])
            lookup[rowIndex] = cellsInRows.length;

        if (undefined == cellsInRows[lookup[rowIndex]])
            cellsInRows[lookup[rowIndex]] = [];

        cellsInRows[lookup[rowIndex]].push(cells.cells[i]);

    }

    return cellsInRows;

};

 

Edit: minor changes to clean up a bit.

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
November 20, 2024

Hi @alma_3660, AI is amazing, but often gets code wrong, as in this case. I've made many changes to get it working, including adding a function I created for your other question.

 

Please try the following code. It puts the words into the table cell to the left of the selected cell. You can select multiple rows.

- Mark

 

 

/**
 * Convert numbers to words (Arabic version).
 * Puts converted number in the cell to the left of the selected cell.
 * @author Claude AI and m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/indesign-bulk-number-to-arabic-text-script/m-p/14992156
 */
function main() {

    // الكود الرئيسي للتنفيذ في InDesign
    if (0 === app.documents.length)
        return alert("الرجاء تحديد الخلايا التي تحتوي على الأرقام في الجدول");

    var doc = app.activeDocument;
    var selection = doc.selection;

    // التأكد من تحديد خلايا في الجدول
    if (
        selection.length === 0
        || !selection[0].hasOwnProperty('parentColumn')
    )
        return alert("الرجاء تحديد الخلايا التي تحتوي على الأرقام في الجدول");

    var rows = getCellsInRows(selection[0]);
    var targetColumnIndex = rows[0][0].parentColumn.index + 1;
    var table = rows[0][0].parent;

    while ('Table' !== table.constructor.name)
        table = table.parent;

    // التحقق من أن العمود المستهدف موجود في الجدول
    if (targetColumnIndex >= table.columns.length)
        // nowhere to put result
        return alert("لا يوجد عمود مجاور للكتابة فيه");

    // معالجة كل صف في العمود المحدد
    for (var r = 0, row, cell, num, targetCell; r < rows.length; r++) {

        row = rows[r];
        cell = row[0];
        num = parseInt(cell.contents);

        // التحقق من أن المحتوى رقم صحيح
        if (
            !isNaN(num)
            && num >= 0
            && num <= 1000
        ) {
            // الحصول على الخلية المجاورة في نفس الصف
            targetCell = table.rows[cell.parentRow.index].cells[targetColumnIndex];
            // تحويل الرقم إلى كلمات وكتابته في الخلية المجاورة
            targetCell.contents = numberToWords(num);
        }

    }

    // alert("تم تحويل الأرقام بنجاح!");

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Test Numbers To Words');

/**
 * Returns the number `num` converted into words.
 * @author Claude AI
 * @version 2024-11-21
 * @param {number} num - the number to convert.
 * @returns {String}
 */
function numberToWords(num) {

    // تحويل الأرقام إلى كلمات باللغة العربية
    const ones = ['', 'واحد', 'اثنتان', 'ثلاث', 'أربع', 'خمس', 'ست', 'سبع', 'ثمان', 'تسع', 'عشر', 'إحدى عشر', 'اثنات عشرة', 'ثلاث عشرة', 'أربع عشرة', 'خمس عشرة', 'ست عشرة', 'سبع عشرة', 'ثمان عشرة', 'تسع عشرة'],
        tens = ['', '', 'عشرون', 'ثلاثون', 'أربعون', 'خمسون', 'ستون', 'سبعون', 'ثمانون', 'تسعون'],
        hundreds = ['', 'مائة', 'مئتان', 'ثلاثمائة', 'أربعمائة', 'خمسمائة', 'ستمائة', 'سبعمائة', 'ثمانمائة', 'تسعمائة'];

    if (num === 0) return 'صفر';
    if (num === 1000) return 'ألف';

    var words = '';

    // معالجة المئات
    if (num >= 100) {
        var hundredsDigit = Math.floor(num / 100);
        words = hundreds[hundredsDigit];
        num = num % 100;

        if (num > 0) {
            words += ' و';
        }
    }

    // معالجة العشرات والآحاد
    if (num > 0) {
        // للأرقام من 1-19
        if (num < 20) {
            words += ones[num];
        }
        // للأرقام من 20-99
        else {
            var remainder = num % 10;
            var tensIndex = Math.floor(num / 10);

            if (remainder === 0) {
                words += tens[tensIndex];
            } else {
                words += ones[remainder] + ' و' + tens[tensIndex];
            }
        }
    }

    return words;

};

/**
 * Returns array of arrays of cells by row.
 * Useful when given a selection of cells
 * to divide into rows.
 * @author m1b
 * @version 2024-11-19
 * @param {Cell} cells - the cells to separate.
 * @returns {Array<Array<Cell>>?}
 */
function getCellsInRows(cells) {

    if (
        undefined == cells
        || 'Cell' !== cells.constructor.name
        || !cells.hasOwnProperty('cells')
        || 0 === cells.cells.length
    )
        return

    var cellsInRows = [],
        lookup = {};

    for (var i = 0; i < cells.cells.length; i++) {

        var rowIndex = cells.cells[i].parentRow.index;

        if (undefined == lookup[rowIndex])
            lookup[rowIndex] = cellsInRows.length;

        if (undefined == cellsInRows[lookup[rowIndex]])
            cellsInRows[lookup[rowIndex]] = [];

        cellsInRows[lookup[rowIndex]].push(cells.cells[i]);

    }

    return cellsInRows;

};

 

Edit: minor changes to clean up a bit.

alma_3660Author
Participating Frequently
November 22, 2024

You are Amazing Too!

 

Thanks dear

It works!

m1b
Community Expert
Community Expert
November 22, 2024

Great to hear!