javascriptadobe-indesign

Need to Add An Unmerge Function to an Existing InDesign Javascript


I have a working javascript I use that goes through a folder of indd files and finds tables with a certain word, and then extracts the text from specified cells in the table. It's worked perfectly (shout out to Yuri Khristich!), but I've come across a series of documents where the table has merged cells, which is causing errors.

I'd like to modify the script so that once it finds the table, it selects all of the cells and performs the Unmerge function. We run this script on duplicates as a precautionary measure, so I have no need to re-merge the cells after completion.

I found this snippet in my searches, but it only works on a single file that is already open. It seems like it's what I need I just don't know what I need to change on it to make it work within my other script:

app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().unmerge();

Below is the extraction script that I need to modify.

// dialog to select a folder if there are no open documents
if (app.documents.length == 0) {
    var from_folder = true;
    var folder = Folder.selectDialog('Select a folder with INDD files...');
    var files = folder.getFiles('*.indd');
    if (files.length == 0) {
        alert('No INDD files was found in the folder')
        exit();
    }
}

// if there are open documents process them
else {
    var from_folder = false;
    var files = app.documents;
}

// get the data from all tables in each of the documents
var all_data = [];
for (var i = 0; i < files.length; i++) {
    if (from_folder) var doc = app.open(files[i]);
    else var doc = files[i];
    var data = get_data_from_doc(doc);
    if (data) while (data.length) all_data.push(data.shift());
    if (from_folder) doc.close();
}

// if there is data, put it on the page of a new document
if (all_data.length > 0) {
    var doc = app.documents.add();
    var frame = doc.pages[0].textFrames.add();
    frame.geometricBounds = doc.pages[0].bounds;
    frame.contents = all_data.join('\r');
    frame.parentStory.texts[0].convertToTable();
} else {
    alert('No data was found');
}


// functions ------------------------------------------------------------------

function get_data_from_doc(doc) {
    var tables = get_all_tables_from_doc(doc);
    if (!tables || tables.length == 0) return;
    var data = get_data_from_all_tables(tables, doc);
    if (data && data.length > 0) return data;
}

function get_all_tables_from_doc(doc) {
    var stories = doc.stories;
    var all_tables = [];
    for (var i = 0; i < stories.length; i++) {
        var tables = stories[i].tables;
        for (var j = 0; j < tables.length; j++) all_tables.push(tables[j]);
    }
    if (all_tables.length > 0) return all_tables;
}

function get_data_from_all_tables(tables, doc) {
    var data = [];
    for (var i = 0; i < tables.length; i++) {
        var table = tables[i];
        var data_row = get_data_from_table(table, doc);
        if (data_row) data.push(data_row);
    }
    if (data.length > 0) return data;
}

function get_data_from_table(table, doc) {
    var cell = get_cell_with_word(table, 'CHARACTERISTICS');
    if (cell) {
        var index = cell.parentRow.index;
        var cell_1 = table.rows[index+5].cells[1].contents;
        var cell_2 = table.rows[index+6].cells[1].contents;
        var cell_3 = table.rows[index+1].cells[3].contents;
        var cell_4 = table.rows[index+1].cells[3].contents;
        var cell_5 = table.rows[index+2].cells[3].contents;
        var cell_6 = table.rows[index+3].cells[3].contents;
        var cell_7 = table.rows[index+5].cells[3].contents;
        var cell_8 = table.rows[index+6].cells[3].contents;
        var cell_9 = table.rows[index+7].cells[3].contents;
        var cell_10 = table.rows[index+8].cells[3].contents;
        var cell_11 = table.rows[index+9].cells[3].contents;
        return [doc.name, cell_1, cell_2, cell_3, cell_4, cell_5, cell_6, cell_7, cell_8, cell_9, cell_10, cell_11].join('\t');
    }
}

function get_cell_with_word(table, word) {
    var cells = table.cells;
    for (var i = 0; i < cells.length; i++) {
        if (cells[i].contents.toUpperCase() == word) return cells[i];
    }
}

And here is a screenshot of one of the tables if it helps visualize what I'm working with. Screenshot of table


Solution

  • As cybernetic-nomad suggests, rework that line to work in your get_data_from_doc function.

    function get_data_from_doc(doc) {
        // the reworked unmerge line follows
        doc.stories.everyItem().tables.everyItem().cells.everyItem().unmerge();
        var tables = get_all_tables_from_doc(doc);
        if (!tables || tables.length == 0) return;
        var data = get_data_from_all_tables(tables, doc);
        if (data && data.length > 0) return data;
    }