Skip to main content
Participating Frequently
September 27, 2022
Answered

Automatisation, supprimer des lignes vides dans un tableau automatiquement

  • September 27, 2022
  • 1 reply
  • 1061 views

Bonjour,
Je cherche à automatiser certains process via InDesign. Après une fusion de données, je me retrouve avec des pages contenant des tableaux avec plusieurs lignes vides.
Savez-vous s'il est possible de supprimer automatiquement toutes les lignes vides de divers tableaux dans une maquette InDesign ? À ma connaissance, rien pour supprimer des lignes vides avec du GREP.
J'ai trouver ceci sur un forum mais je ne sais pas comment créer un script dans indesign, Merci pour votre aide

The following script should remove any empty rows in all tables in a document:

EDIT: From prior experience, I know it's a better idea to count backwards when deleting, so here is the earlier script with the order reversed:

 

var myDocument = app.activeDocument;

for(var i=myDocument.textFrames.length-1; i>=0; i--){

    for(var j=myDocument.textFrames.tables.length-1; j>=0; j--){

        for(var k=myDocument.textFrames.tables.rows.length-1; k>=0; k--){

            myContents = 0;

            for(var l=myDo

This topic has been closed for replies.
Correct answer m1b

Hi @Antony5EAF, here's a little script that should work on basic tables. Please let me know if it works in your case.

- Mark

 

 

function main() {


    // process all tables in active document
    removeEmptyTableRowsAndColumns(app.activeDocument);


    /**
     * Removes empty Rows and Columns from
     * all tables in a document, or a single
     * table.
     * @author m1b
     * @version 2022-09-27
     * @param {Document|Table} docOrTable - an Indesign Document or Table.
     */
    function removeEmptyTableRowsAndColumns(docOrTable) {

        if (docOrTable.constructor.name == 'Document') {

            // process all tables in the document

            var tables = docOrTable.stories.everyItem().tables.everyItem().getElements();

            for (var i = 0; i < tables.length; i++)
                removeEmptyTableRowsAndColumns(tables[i]);

            return;

        }

        if (docOrTable.constructor.name !== 'Table')
            return;

        var table = docOrTable,
        empty = /^\s*$/;

        // remove empty rows
        for (var r = table.rows.length - 1; r >= 0; r--)
            if (empty.test(table.rows[r].cells.everyItem().contents.join('')))
                table.rows[r].remove();

        // remove empty columns
        for (var c = table.columns.length - 1; c >= 0; c--)
        if (empty.test(table.columns[c].cells.everyItem().contents.join('')))
                table.columns[c].remove();

    };


} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Remove Empty Columns and Rows');

 

Edit: changed the test for emptiness to handle more cases.

 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
September 27, 2022

Hi @Antony5EAF, here's a little script that should work on basic tables. Please let me know if it works in your case.

- Mark

 

 

function main() {


    // process all tables in active document
    removeEmptyTableRowsAndColumns(app.activeDocument);


    /**
     * Removes empty Rows and Columns from
     * all tables in a document, or a single
     * table.
     * @author m1b
     * @version 2022-09-27
     * @param {Document|Table} docOrTable - an Indesign Document or Table.
     */
    function removeEmptyTableRowsAndColumns(docOrTable) {

        if (docOrTable.constructor.name == 'Document') {

            // process all tables in the document

            var tables = docOrTable.stories.everyItem().tables.everyItem().getElements();

            for (var i = 0; i < tables.length; i++)
                removeEmptyTableRowsAndColumns(tables[i]);

            return;

        }

        if (docOrTable.constructor.name !== 'Table')
            return;

        var table = docOrTable,
        empty = /^\s*$/;

        // remove empty rows
        for (var r = table.rows.length - 1; r >= 0; r--)
            if (empty.test(table.rows[r].cells.everyItem().contents.join('')))
                table.rows[r].remove();

        // remove empty columns
        for (var c = table.columns.length - 1; c >= 0; c--)
        if (empty.test(table.columns[c].cells.everyItem().contents.join('')))
                table.columns[c].remove();

    };


} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Remove Empty Columns and Rows');

 

Edit: changed the test for emptiness to handle more cases.

 

Participating Frequently
September 27, 2022

Bonjour m1b et merci pour votre réponse. Je n'ai jamais rentré de code dans InDesign et je ne sais pas comment faire. Pouvez-vous me dire comment entrer ce script dans indesign s'il vous plaît ? Merci beaucoup.

 

Hello m1b and thank you for your answer. I've never entered code into InDesign and I don't know how to do it. Can you tell me how to enter this script in indesign please? Thanks a lot.

m1b
Community Expert
Community Expert
September 27, 2022

Hi @Antony5EAF,  no problem. Here are the steps.


1. Select the text from my post - just the "code" styled part
2. Make a new text file in Notepad or TextEdit (must be plain text—not rich text)
3. Paste
4. Save as "Remove Empty Rows and Columns.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

 

Let me know if you get stuck.

- Mark