Search code examples
excelscriptingadobe-indesign

Using scripting to open InDesign file and update table data from an excel document that is located in the folder


My company makes data reports that are often replicated a couple hundred times. I've created a document that has table data attached to an excel file and can be updated through the links panel. My question is, is there a way to update the data in the tables just by running a script, pointing to the excel file within the same folder?

There are more complicated actions that need to be completed in the file, but this would be a great starting point!

Thank you in advance!


Solution

  • Here is the script that takes list of INDD files (full paths!) from the clipboard opens them, updates links, saves PDFs (in the same folders with the same names as opened documents), saves and closes the INDD files:

    var settings = save_setting();
    
    var list = get_list_from_cliboard();
    
    while (list.length) {
        var doc = app.open(list.shift());
        update_links(doc);
        save_pdf(doc);
        doc.close(SaveOptions.YES);
    }
    
    restore_settings(settings);
    
    
    // functions ------------------------------------------------------------------
    
    function get_list_from_cliboard() {
        var temp_doc = app.documents.add();
        app.paste();
        try { var contents = temp_doc.pages[0].textFrames[0].parentStory.contents.split('\r') }
        catch(e) { var contents = [] }
        temp_doc.close(SaveOptions.NO);
        return contents;
    }
    
    function update_links(doc) {
        var links = doc.links.everyItem().getElements();
        while(links.length) {
            var link = links.pop();
            if (link.status == LinkStatus.LINK_OUT_OF_DATE) link.update();
        }
    }
    
    function save_pdf(doc) {
        var name = doc.name.replace(/\.indd/g, '.pdf');
        var file = File(doc.fullName.parent + '/'  + name);
        var preset = app.pdfExportPresets.item("[High Quality Print]");
        app.activeDocument.exportFile(ExportFormat.PDF_TYPE, file, false, preset);
    }
    
    function save_setting() {
        var settings = {
            checkLinksAtOpen: app.linkingPreferences.checkLinksAtOpen,
            viewPDF: app.pdfExportPreferences.viewPDF,
        };
        app.linkingPreferences.checkLinksAtOpen = false;
        app.pdfExportPreferences.viewPDF = false;
        return settings;
    }
    
    function restore_settings(settings) {
        app.linkingPreferences.checkLinksAtOpen = settings.checkLinksAtOpen;
        app.pdfExportPreferences.viewPDF = settings.viewPDF;
    }
    

    You can copy in Excel the list of paths to the INDD files run the script and get the PDF files and updated INDD files.