Copy link to clipboard
Copied
Is there any script to center but also right-aligned text/numbers inside every cell in every table?
so that the output on every table looks like this. I'm working on a template file that contains about 200 tables, but unfortunately it's bit of a mess. Thank you in advance
Copy link to clipboard
Copied
Maybe a better way, but this should work.
app.activeDocument.stories.everyItem().tables.everyItem().columns.lastItem().cells.everyItem().texts.everyItem().justification = Justification.RIGHT_ALIGN;
Copy link to clipboard
Copied
Although, looks like maybe you're trying to exclude header cells, which would complicate matters.
Copy link to clipboard
Copied
thank you so much for the respond, but i tried to execute it, somehow it came out with an error notification, that "justification is not defined"
Copy link to clipboard
Copied
Yes, there's my (not free) https://www.id-extras.com/products/centertablecolumns/ which does just that!
Copy link to clipboard
Copied
This is exactly what I'm looking for, even better than what I need, because you create UI (menu) to help the user. Sadly, I would need my own bucks to buy it since the company just pretty much being cheap T_T. I'm sorry in advance if this offending you (because i have so little information about how intellectual property works), if I buy it, will i get the script and find out how you write the script or i can only merely use it ?
Copy link to clipboard
Copied
No offense, but unfortunately it is a commercial product, so you would be able to use it, but not inspect the code.
Copy link to clipboard
Copied
oh i see.... thank you for the information
Copy link to clipboard
Copied
if i buy it, can i install it on multiple devices with same adobe account ? (i need this both on my laptop and my computer in the office)
Copy link to clipboard
Copied
Absolutely, it can be installed on all machines with the same Adobe account.
Copy link to clipboard
Copied
If we are suggesting paid solutions - my tool will load texts from the tables, then can selectively style them / align. But it's PC only.
Find&Change GREP would work "globally" - but only if texts you want to realign are ONLY in tables.
Do you have a unique Char / Para Style already applied to the texts in Tables - that could be used to narrow the search?
Copy link to clipboard
Copied
Thank you for the reply, I'm sorry for the late reply, yes!, the file already has Paragraph Style applied in each cells in the Tables, so the first column has its own style, the second and the rest of the tables has same style but different from the first column, the header (column title cells) already has it's own style too.
Copy link to clipboard
Copied
Hi @KALSEL33703823ork9, you've got some ideas already, but here's another scripting approach. I've focused on (a) the selection vs the document, and (b) using findGrep to do the heavy lifting. I've also tried to show the workings so you can adjust yourself. For example, instead of justifying right, you could apply a paragraph style that was justified right.
Anyway, let me know if it helps.
- Mark
/**
* @file Numbered Cells Align Right.js
*
* Finds paragraphs inside table cells which
* consist of numerical representations,
* eg. 123 or 12.3 or -123 or (123) or (1,234.00)
* It will not find currency symbols.
*
* If a selection, it will try to process those item(s),
* or if no selection, will process the whole document;
*
* To adjust the find, look at the line that assigns
* a string to `app.findGrepPreferences.findWhat`.
*
* @author m1b
* @version 2024-07-20
* @discussion https://community.adobe.com/t5/indesign-discussions/is-there-any-script-to-center-right-aligned-text-numbers-inside-every-cell-in-every-table/m-p/14747317
*/
function main() {
if (0 === app.documents.length)
return alert('Please open a document and try again.');
// reset grep
app.findGrepPreferences = NothingEnum.NOTHING;
// grep to find paragraphs which are numbers
app.findGrepPreferences.findWhat = '^\\h*\\(?-?[\\d\\.,]+\\)?\\h*$';
var doc = app.activeDocument,
items = doc.selection.length > 0 ? doc.selection : [doc];
itemLoop:
for (var i = 0, item, found; i < items.length; i++) {
// we want only specific items here
var item = getParent(items[i], ['Document', 'Table', 'TextFrame', 'Story']);
if (undefined == item.findGrep)
// can't do findGrep on this, so move on
continue itemLoop;
// find paragraphs we want
var found = item.findGrep();
foundLoop:
for (var j = 0; j < found.length; j++) {
if (
'Cell' !== found[j].parent.constructor.name
|| RowTypes.BODY_ROW !== found[j].parent.rowType
)
// we don't want this paragraph
continue foundLoop;
// make the change
found[j].justification = Justification.RIGHT_ALIGN;
}
};
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Numbered Cells Align Right');
/**
* Returns a parent of `item`, chosen
* according to a list of types.
* The order of types matters.
* @author m1b
* @version 2024-07-20
* @param {PageItem} item - an Indesign page item.
* @param {Array<String>} types - the desired types.
* @returns {PageItem} - the parent page item.
*/
function getParent(item, types) {
while (item.hasOwnProperty('parent') && item.parent) {
for (var i = 0; i < types.length; i++)
if (types[i] === item.constructor.name)
return item;
if (!item.parent)
break;
item = item.parent;
}
};
Edit 2024-07-22: fixed typo in the script documentation.
Copy link to clipboard
Copied
@m1bI think the OP wants to "center and right align," not just right-align, which is what I think your script does.
IIUC, the idea is to center the longest number in the column, and then right-align all the other numbers in the column to that number.
Copy link to clipboard
Copied
@m1bI think the OP wants to "center and right align," not just right-align, which is what I think your script does.
IIUC, the idea is to center the longest number in the column, and then right-align all the other numbers in the column to that number.
By @TᴀW
Can be hard to achieve if you are right...
123456789
12.3456
And columns are narrow...
And if ALL tables should look the same...
@KALSEL33703823ork9 could you please confirm your requirements - with much more "real" example.
Copy link to clipboard
Copied
Ah, thanks! Then perhaps then my script could apply a Cell Style that included an appropriate right inset? Or a Paragraph Style might do it okay? At most, the complication would be calculating a right margin based on the column's cells (on a per-table basis) which is not super hard, but is fiddly. We'll see what @KALSEL33703823ork9 has in mind I guess—maybe posting a demo document, before and after would be easiest.
- Mark
Copy link to clipboard
Copied
Thank you for the replies, I'll post the demo documents, i'm really sorry for being vague 🙏
Copy link to clipboard
Copied
@m1b here is the demo document, thank you so much
Copy link to clipboard
Copied
Hi @mfebrianrizkyr / @KALSEL33703823ork9, I've written a simple script that block centers the selected table cells. So in your demo document, try selecting all the cells you want to center (you can select multiple columns) and run this script. Let me know if it works for you.
- Mark
/**
* @file Block Center Table Columns.js
*
* Attempts to block center the selected
* table cells, ie. center based on the
* longest line of text in each column.
*
* Note: this script will alter every
* selected cell's `textRightInset` value.
*
* @author m1b
* @version 2024-08-01
* @discussion https://community.adobe.com/t5/indesign-discussions/is-there-any-script-to-center-right-aligned-text-numbers-inside-every-cell-in-every-table/m-p/14747317
*/
function main() {
if (0 === app.documents.length)
return alert('Please open a document and try again.');
var doc = app.activeDocument,
cells = doc.selection.length > 0 ? doc.selection[0] : undefined;
if (
undefined == cells
|| 'Cell' !== cells.constructor.name
|| 0 === cells.cells.length
)
return alert('Please select some table cells and try again.');
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var cells = cells.cells,
widths = {};
// reset right indents and justify right
cells.everyItem().textRightInset = 0;
cells.everyItem().texts[0].justification = Justification.RIGHT_ALIGN;
// collect widths
for (var i = 0, cell, left, right; i < cells.length; i++) {
cell = cells[i];
left = cell.insertionPoints[0].horizontalOffset;
right = cell.insertionPoints[-1].horizontalOffset;
// store the widest line
widths[cell.parentColumn.index] = Math.max(right - left, widths[cell.parentColumn.index] || -Infinity);
}
// set right inset according to longest text
for (var i = 0, cell; i < cells.length; i++) {
cell = cells[i];
cell.textRightInset = (cell.parentColumn.width - widths[cell.parentColumn.index]) / 2;
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Center Table Cells');
Copy link to clipboard
Copied
Correct, centered right-aligned on each cells. I wanna deal with my works for the company, but finding out how the script looks like is just for my personal curiosity