Skip to main content
Jensie—Typografics
Participant
March 6, 2025
Answered

specifying this script to only work in tables

  • March 6, 2025
  • 1 reply
  • 269 views

Hi

 

There's this script from Peter Kahrel, that swaps comma and period for number notation.

However, I'd like a version where I can only target numbers inside a cell/table.

 

 

//DESCRIPTION: Exchange commas and periods in numbers
// Peter Kahrel

(function () {

function findItems () {
if (!app.selection.length) {
return app.activeDocument.findGrep();
}
if (app.selection[0] instanceof TextFrame) {
return app.selection[0].parentStory.findGrep();
}
try {
return app.selection[0].findGrep();
} catch (_) {
}
exit();
}

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\d+[,.\\d]+\\d+";
var found = findItems();
var temp;
for (var i = found.length-1; i >= 0; i--) {
temp = found[i].contents.replace (/,/g, '#');
temp = temp.replace (/\./g, ',');
found[i].contents = temp.replace (/#/g, '.')
}
}());

 

 
Correct answer FRIdNGE

That will play ALL THE TABLES AND ONLY THEM:

 

var myDoc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\d+[,.\\d]+\\d+";
var myFound = myDoc.findGrep();
var F = myFound.length,  f;
var temp;
for ( f = F-1; f >= 0; f-- ) {
    if ( myFound[f].parent.constructor.name === "Cell" ) {
        temp = myFound[f].contents.replace(/,/g, '#');
        temp = temp.replace(/\./g, ',');
        myFound[f].contents = temp.replace(/#/g, '.');
    }
}
app.findGrepPreferences = app.changeGrepPreferences = null;
alert( "Done! ..." )

 

(^/)  The Jedi

1 reply

Community Expert
March 6, 2025

Can you try this

 

//DESCRIPTION: Exchange commas and periods in numbers inside table cells
// Peter Kahrel

(function () {

    function findItems() {
        var items = [];
        if (!app.selection.length) {
            // Search in all text frames and table cells
            var allTextFrames = app.activeDocument.textFrames;
            for (var i = 0; i < allTextFrames.length; i++) {
                var tf = allTextFrames[i];
                for (var j = 0; j < tf.tables.length; j++) {
                    var table = tf.tables[j];
                    for (var k = 0; k < table.rows.length; k++) {
                        for (var l = 0; l < table.rows[k].cells.length; l++) {
                            var cell = table.rows[k].cells[l];
                            if (cell.contents.match(/\d+[,.]\d+/)) {
                                items.push(cell);
                            }
                        }
                    }
                }
            }
        } else if (app.selection[0] instanceof TextFrame) {
            // If selection is a text frame, check its table cells
            var tf = app.selection[0];
            for (var i = 0; i < tf.tables.length; i++) {
                var table = tf.tables[i];
                for (var j = 0; j < table.rows.length; j++) {
                    for (var k = 0; k < table.rows[j].cells.length; k++) {
                        var cell = table.rows[j].cells[k];
                        if (cell.contents.match(/\d+[,.]\d+/)) {
                            items.push(cell);
                        }
                    }
                }
            }
        } else {
            // Otherwise search only in selected text
            try {
                items.push(app.selection[0]);
            } catch (_) {
                exit();
            }
        }
        return items;
    }

    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findGrepPreferences.findWhat = "\\d+[,.\\d]+\\d+";
    var foundItems = findItems();
    var temp;

    for (var i = foundItems.length - 1; i >= 0; i--) {
        temp = foundItems[i].contents.replace(/,/g, '#');
        temp = temp.replace(/\./g, ',');
        foundItems[i].contents = temp.replace(/#/g, '.');
    }

}());

 

 

FRIdNGE
FRIdNGECorrect answer
March 6, 2025

That will play ALL THE TABLES AND ONLY THEM:

 

var myDoc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\d+[,.\\d]+\\d+";
var myFound = myDoc.findGrep();
var F = myFound.length,  f;
var temp;
for ( f = F-1; f >= 0; f-- ) {
    if ( myFound[f].parent.constructor.name === "Cell" ) {
        temp = myFound[f].contents.replace(/,/g, '#');
        temp = temp.replace(/\./g, ',');
        myFound[f].contents = temp.replace(/#/g, '.');
    }
}
app.findGrepPreferences = app.changeGrepPreferences = null;
alert( "Done! ..." )

 

(^/)  The Jedi