Skip to main content
New Participant
February 9, 2022
Answered

how to get range and sheet from linked excel?

  • February 9, 2022
  • 2 replies
  • 1571 views

hi forum,

 

i have taken over a book with about 600 pages and inside ther are about 100 excel-files linked. many of them with multiple sheets. i can get a nice list of all excel-links with the name and the path of the sourcefile from a script. but i need to know, which sheet and range is linked. can anyone tell me the right term like "app.activeDocument.link.item(i)...."

 

i'm new in this community. so please excuse, if i made mistakes.

thanks, dominik

my setting is Indesign 2022, Visual Studio Code 1.57.1 and javascript on a mac with monterey.

This topic has been closed for replies.
Correct answer m1b

Hi @nikl75, yes you are right it isn't straightforward for a new scripter—but I'm glad you gave it a try. Every script you attempt will teach you lots.

 

Here's what I came up with, thanks to the idea by @琥珀 猫太郎. Select the text frame containing the linked excel file and run script. It should show alert if it found the cell range. Let me know if it works on your system.

- Mark

 

/*
    Get Link Excel File Cell Range.js

    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/how-to-get-range-and-sheet-from-linked-excel/m-p/12741647

    (idea by forum user 琥珀 猫太郎)

*/


function main() {

    // for example, if you have selected the excel table's text frame
    var doc = app.activeDocument;
    var item = doc.selection[0];

    // get the cell range text (eg A:1,E:20)
    var cellRangeText = getTextFrameLinkedFileCellRanges(item, item);
    if (cellRangeText != undefined) {
        alert('Cell range is ' + cellRangeText);
    } else {
        alert('Could not find cell range.');
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Script');


// This function accepts a text frame.
// If it contains a linked excel document,
// then it exports textFrame as snippet to
// temp folder, then reads it, and finds
// and returns the cell range
function getTextFrameLinkedFileCellRanges(textFrame) {
    if (!textFrame.isValid || textFrame.constructor.name != 'TextFrame')
        return;

    // export snippet
    var snippet = exportAsSnippet(textFrame);

    // load snippet XML
    snippet.open("r");
    var snippetXML = XML(snippet.read());
    snippet.close();

    // get the element containing the cell range text
    var target = snippetXML.xpath('//ExcelImportPreferences/ListItem[9]');
    if (target != undefined)
        target = String(target);

    return target;
}


// This function exports a pageItem
// as snippet file (.idms)
// If not path is given, will save
// to user's temporary folder
function exportAsSnippet(item, path) {
    if (!item.isValid)
        throw 'Cannot export item as snippet. Item is invalid.';

    if (typeof item.exportFile !== 'function')
        throw 'Cannot export item as snippet. Item has no exportFile method.';

    // save to temp folder if no path argument given
    if (path == undefined)
        path = (Folder.temp) + '/snippet.xml';

    // export as indesign snippet
    item.exportFile(ExportFormat.INDESIGN_SNIPPET, File(path), false);
    var snippetFile = File(path);
    return snippetFile;
}

 

2 replies

Inspiring
February 9, 2022

When I export the frame where the excel is placed with the InDesign snippet, the ExcelImportPreferences element exists, but I don't know if there is a way to get this with ExtendScript.

nikl75Author
New Participant
February 10, 2022

I didn't knew about the InDesign snippets. here it looks so easy, but I don't get there with javascript/ExtendScript. I tried it with:

app.activeDocument.links.item(0).parent...

in the ExtendScript Toolkit, which gives me the STORY of the link, and I looked in many corners there, but i didn't found the settings.

m1b
m1bCorrect answer
Community Expert
February 11, 2022

Hi @nikl75, yes you are right it isn't straightforward for a new scripter—but I'm glad you gave it a try. Every script you attempt will teach you lots.

 

Here's what I came up with, thanks to the idea by @琥珀 猫太郎. Select the text frame containing the linked excel file and run script. It should show alert if it found the cell range. Let me know if it works on your system.

- Mark

 

/*
    Get Link Excel File Cell Range.js

    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/how-to-get-range-and-sheet-from-linked-excel/m-p/12741647

    (idea by forum user 琥珀 猫太郎)

*/


function main() {

    // for example, if you have selected the excel table's text frame
    var doc = app.activeDocument;
    var item = doc.selection[0];

    // get the cell range text (eg A:1,E:20)
    var cellRangeText = getTextFrameLinkedFileCellRanges(item, item);
    if (cellRangeText != undefined) {
        alert('Cell range is ' + cellRangeText);
    } else {
        alert('Could not find cell range.');
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Script');


// This function accepts a text frame.
// If it contains a linked excel document,
// then it exports textFrame as snippet to
// temp folder, then reads it, and finds
// and returns the cell range
function getTextFrameLinkedFileCellRanges(textFrame) {
    if (!textFrame.isValid || textFrame.constructor.name != 'TextFrame')
        return;

    // export snippet
    var snippet = exportAsSnippet(textFrame);

    // load snippet XML
    snippet.open("r");
    var snippetXML = XML(snippet.read());
    snippet.close();

    // get the element containing the cell range text
    var target = snippetXML.xpath('//ExcelImportPreferences/ListItem[9]');
    if (target != undefined)
        target = String(target);

    return target;
}


// This function exports a pageItem
// as snippet file (.idms)
// If not path is given, will save
// to user's temporary folder
function exportAsSnippet(item, path) {
    if (!item.isValid)
        throw 'Cannot export item as snippet. Item is invalid.';

    if (typeof item.exportFile !== 'function')
        throw 'Cannot export item as snippet. Item has no exportFile method.';

    // save to temp folder if no path argument given
    if (path == undefined)
        path = (Folder.temp) + '/snippet.xml';

    // export as indesign snippet
    item.exportFile(ExportFormat.INDESIGN_SNIPPET, File(path), false);
    var snippetFile = File(path);
    return snippetFile;
}

 

m1b
Community Expert
February 9, 2022

I've had a look through the object model and, sadly, it looks to me like there's no property or method that gives you that info.

- Mark

nikl75Author
New Participant
February 9, 2022

seems like. i searched there too, for hours. but theres this plugin (http://emsoftware.com/products/wordsflow/), which shows the sheet and range in the link-panel. so somehow it must be possible, 🤔 🤔

m1b
Community Expert
February 9, 2022

Sorry I was only looking at scripting API. Wordsflow plug-in would be written in C++ I think. It's much more involved than writing ExtendScript.

- Mark