Tabellen: Zellen suchen und formatieren mit GREP

Community Beginner ,
Jan 25, 2022 Jan 25, 2022

Copy link to clipboard

Copied

Hallo,

ich bekomme regelmäßig mehrseitige Tabellen aus Excel, die ich formatieren muss. Diese Tabelle hat immer zwei Spalten, eine mit Text und eine mit einer dazugehörenden Zahl.

Die Formatierung für Indesign ist folgendermaßen:

  • eine Zeile mit einer Überschrift: Hier gibt es nur Text und keine Zahl (Head)
  • eine Zeile mit einem Text in Zelle 1 mit der zugehörigen Zahl 100 in Zelle 2 (kommt immer direkt nach der Überschrift) (Subhead)
  • eine oder mehrere weitere Zeilen mit je einer Zelle Text und Zahl (Body)
  • dann kommt die nächste Head usw...

Ich möchte nun nach den jeweiligen Zellen suchen und diese in Zellenformate umwandeln. Folgende Schritte muss ich machen:

  • Die gesamte Tabelle wird als Body formatiert
  • Ich muss nach den leeren Zellen in der Zahlenspalte suchen – die bei jeder Head leer sind – und dieser Zeile das Zeilenformat »Head« geben. (Ganz toll wäre es übrigens, wenn ich hier auch noch die beiden Zellen vereinen könnte, weil ich das bei langen Begriffen auch händisch machen muss.)
  • Dann muss ich nach der Zahl 100 suchen und dieser kompletten Zeile das Zeilenformat «Subhead« zuweisen.

Meine Frage ist nun, ob ich das irgendwie automatisieren kann mit Eingaben in dem Suchen&Ersetzen-Menü? Oder ein Script (wobei ich mich hier gar nicht auskenne)?

Ich hoffe, ich konnte verständlich machen, was ich machen möchte und bin gespannt, ob Ihr mir dabei helfen könnt?

Zur Verdeutlichung habe ich einen Screenshot gemacht (vor und nach der Formatierung).

jd

 

TOPICS
Import and export , Scripting

Views

166

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2022 Jan 25, 2022

Copy link to clipboard

Copied

Ich würde das gar nicht mit einer Tabelle machen. Absatzformate und nächstes Absatzformat, Eintrag und Ziffer getrennt durch Umschalt+Tabulator.

 

Hat es irgendeinen Grund, weshalb es eine Tabelle sein muss?

 

Sonst könnte man doch ganz einfach mit Tabellenformaten arbeiten. Kopfzeile, Body, Body rechte Spalte. Manuell die Unterüberschrift.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 31, 2022 Jan 31, 2022

Copy link to clipboard

Copied

Hallo Willi,

danke für die Antwort, aber dann habe ich mich nicht so richtig ausdrücken können.

Es müssen leider Tabellen sein und die Tabellenzeilen, die du vorschlägst, sind natürlich auch so zugewiesen.

Aber es handelt sich um eine sehr lange Tabelle, die einen Tabellenkopf hat, der auch zugewiesen ist.

Nun kommen z.B. alle 6 sib 10 Zeilen eine Unterüberschrift, die wie oben beschrieben formatiert werden soll. Akutell wähle ich diese Zeile aus und weise Ihr mit einem Shortcut eine Formatierung zu.

Die Zeile direkt darunter bekommt dann eine weitere Formatierung, indem ich diese ebenfalls auswähle und zuweise.

Die 4-8 Zeilen darunter bleiben als Body formatiert, bis die nächste Zeile kommt, die als Unterüberschrift formatiert werden soll. 

Meine Hoffnung war nun, dass ich nach wiederkehrenden Vorgaben dieser Zeilen (rechte Zelle der Zeile leer oder mit der Zahl »100« gefüllt) suchen und dann diese gesamte Zeile automatisiert eine Formatieren zuweisen kann.

Aber das scheint nicht so ohne weiteres möglich zu sein...

Trotzdem Danke für den Versuch.

Viele Grüße, jd

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 31, 2022 Jan 31, 2022

Copy link to clipboard

Copied

Das kann mit Absatz- und auch mit Objektformaten automatisiert erfolgen.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Die Frage ist halt wie?

Aber das Skript von Mark scheint hier die Lösung zu bringen...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 31, 2022 Jan 31, 2022

Copy link to clipboard

Copied

Hi Jan, I've written a script that I hope will do what you need. Please adjust the variables with your correct Cell Style names, make a selection that includes one or more tables, and run script.

- Mark

 

Edit: by the way, if you want to apply to ALL tables in the document, change line 23 to "var tables = getTables(doc);"

 

 

/*
    Set Table Row Styles With Criteria

    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/tabellen-zellen-suchen-und-formatieren-mit-grep/m-p/12705550

*/

function main() {

    // table cell style names:
    var headCellStyleName = 'Head',
        subheadCellStyleName = 'Subhead',
        normalStyleName = "Normal";

    var doc = app.activeDocument;
    if (doc.selection.length == 0) {
        alert('Please make a select one or more tables and run script again.');
        return;
    }

    var tables = getTables(doc.selection);

    for (var i = 0; i < tables.length; i++) {
        for (var r = 0; r < tables[i].rows.length; r++) {
            var row = tables[i].rows[r];

            // set style based on criteria:

            if (
                // HEADING ROW
                row.cells.lastItem().contents == ''
            ) {
                setStyle(doc, row, 'cellStyle', headCellStyleName, true);
                // merge heading row
                row.cells.firstItem().merge(row.cells.lastItem());


            } else if (
                // SUBHEADING ROW
                row.cells.lastItem().contents == '100'
            ) {
                setStyle(doc, row, 'cellStyle', subheadCellStyleName, true);


            } else {
                // NORMAL ROW
                setStyle(doc, row, 'cellStyle', normalStyleName, true);
            }

        }
    }


    // convenience function for getting tables
    function getTables(items, found) {
        if (items == undefined) return [];
        if (found == undefined) found = [];
        if (!items.hasOwnProperty('0')) items = [items];

        for (var i = 0; i < items.length; i++) {
            var item = items[i];

            if (
                // item is a table
                item.constructor.name == 'Table'
            ) {
                found.push(item);


            } else if (
                // item contains tables
                item.hasOwnProperty('tables')
                && item.tables.length > 0
            ) {
                found = found.concat(getTables(item.tables));


            } else if (
                // parent is a table cell
                item.hasOwnProperty('parent')
                && item.parent.constructor.name == 'Cell'
            ) {
                found = found.concat(getTables([item.parent.parent]));


            } else if (
                // is in a story, which may contain tables
                item.hasOwnProperty('parentStory')
            ) {
                found = found.concat(getTables([item.parentStory]));


            } else if (
                // contains page items, which may be tables
                item.hasOwnProperty('pageItems')
                && item.pageItems.length > 0
            ) {
                found = found.concat(getTables(item.pageItems));


            } else if (
                // has a parent which might be a table
                item.hasOwnProperty('parent')
                && item.parent.constructor.name != 'Document'
            ) {
                found = found.concat(getTables([item.parent]));

            }
        }

        return found;
    }


    // convenience function for setting styles
    function setStyle(doc, item, styleType, styleName, clearOverrides) {
        if (doc == undefined || item == undefined || styleName == undefined) return;

        if (clearOverrides == undefined) clearOverrides = true;
        switch (styleType.toLowerCase()) {
            case 'cellstyle':
                if (doc.cellStyles.itemByName(styleName).isValid) {
                    if (
                        !item.hasOwnProperty('appliedCellStyle')
                        && item.hasOwnProperty('cells')
                        && item.cells.length > 0
                    ) {
                        item = item.cells.everyItem();
                    }
                    item.appliedCellStyle = doc.cellStyles.itemByName(styleName);
                    if (clearOverrides) item.clearCellStyleOverrides(false);
                } else {
                    throw ('Cell Style "' + styleName + '" not found.');
                }
                break;
            case 'paragraphstyle':
                if (doc.paragraphStyles.itemByName(styleName).isValid) {
                    item.texts[0].paragraphs.everyItem().appliedParagraphStyle = doc.paragraphStyles.itemByName(styleName);
                    if (clearOverrides) item.texts[0].clearOverrides(OverrideType.ALL);
                } else {
                    throw ('Paragraph Style "' + styleName + '" not found.');
                }
                break;
            case 'characterstyle':
                if (doc.characterStyles.itemByName(styleName).isValid) {
                    item.texts[0].paragraphs.everyItem().appliedCharacterStyle = doc.characterStyles.itemByName(styleName);
                    if (clearOverrides) item.texts[0].clearOverrides(OverrideType.CHARACTER_ONLY);
                } else {
                    throw ('Character Style "' + styleName + '" not found.');
                }
                break;
            default:
                throw ('Unrecognised style "' + styleName + '" (' + styleType + ').');
                break;
        }
    }

} // end main


app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Set Table Cell Styles");

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

Jan, I realise you may not know how to install a script. Here is what you can do:


1. Select the text from my post - be careful to get just the "code" styled part
2. Make a new text file in Notepad or TextEdit (must be plain text—not rich text)
3. Paste the code
4. Save as "Set Table Row Styles.js" (note: file extension is "js") to your desktop
5. In Indesign, open the Script panel, right-click on on User folder and choose "Reveal"
6. Move the file you saved in step 4 to the User folder that should now be visible.
7. Back in Indesign, the script should appear in the User folder of Scripts Panel (see Window menu/Utility)
8. Double click to run the script

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Hello Mark,
that works almost perfectly, the headline is recognized and reformatted.
However, the subline is not yet recognized. The criterion for this subline is that there is always a "100" in the second column/cell.
Where do I have to adjust the script so that this line is also recognized?
Thanks for your great help
jd

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Sorry for the confusion, but I posted the last message from my wife's computer...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

First, can you try an experiment? Just swap the names of the head and subhead styles in the script and run it. That will tell you something. Then swap them back. Did the "head" style get applied: that means the problem is the "subhead" style isn't being found I think. Or did the "Subhead" style get applied: that means the problem is the following line:

row.cells.lastItem().contents == '100'

It checks if the cell's contents *exactly* contains '100' and nothing else. Can you confirm that the subhead row only has two cells and that the right cell only contains '100'. You can turn on 'show invisibles' to see if there are other whitespace characters or something messing it up. I can easily make the line less fussy but ideally I'd like to know why it's not working as it is.

 

If you get stuck, try changing that line to:

row.cells.lastItem().contents.search('100') != -1

This means it will find any contents that has '100' even if it has whitespace before or after, or even if it is '1000' or '2100'.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied



 

Yes, when I reset the names, the following error message appears (see screenshot)
If I then put the names back in, the headline is found and formatted, the subline is not.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

With this line it works:

row.cells.lastItem().contents.search('100') != -1

Is it possible to change the name of the script and where do I have to make these adjustments

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

You can change the name of the script by just renaming the .js file. That will change the name in the scripts panel.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Now it works. Great!

Thank you very much for this script and your help!!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

LATEST

That error message is because it cannot find the cell style "Normal". The script expects you to supply a cell style name for the rows that aren't head or subhead.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines