Copy link to clipboard
Copied
Hi.
I wonder if it is possible to import selected parts from an excel file into InDesign.
I have an excel-file with all of our products data and now I am going to make a pricelist out of it but not with all products. I would like to be able to choose which product that will be imported. Preferable by our article numbers. Is this possible with javascript or some other method?
I see before me an InDesign document with tables where I fill in the article number and the rest of the table row information is imported from the excel-file. Is it possible or whats best practice?
The file is being updated continuously​ which means that products will change rows from time to time, if this has an impact of how to do it most efficiently.
Copy link to clipboard
Copied
Yes, it's possible. The sample script below is one simple example of how it could be done. However, when data changes, you would need to rerun the script to create an updated table. There may be more elegant solutions, possibly one using a scriptable spreadsheet (it's been awhile since I supported the PC side with VisualBasic so can't remember if Excel is scriptable).
(*Requires text frame holding table to be selected
Works quite well for a medium sized table with only 2 columns*)
property newList : {}
tell application "Adobe InDesign CC 2018"
set selList to selection
if selList is not {} and class of item 1 of selList is text frame then
my readCSVFile()
set selRef to item 1 of selList
tell document 1
set x to table 1 of selRef
tell x
repeat with i from 1 to count of rows
set theData to contents of cell 1 of row i
set matchData to my getMatch(theData)
set contents of cell 2 of row i to matchData
end repeat
end tell
end tell
else
activate
display alert ("Requres text frame with table selection")
end if
end tell
(*Matches item from table with first element of list items read from the file chosen*)
on getMatch(theData)
repeat with i from 2 to length of newList
set theListItem to item i of newList
set theItem to item 1 of theListItem
ignoring white space
if theItem = theData then
set theReturn to item 2 of theListItem
return theReturn
exit repeat
end if
end ignoring
end repeat
return ""
end getMatch
(*Reads comma return text file exported from spreadsheet*)
on readCSVFile()
set rowDelimiter to return
set colDelimiter to ","
set fileAlias to choose file
set fileRef to open for access fileAlias
try
set theData to read fileRef as list using delimiter rowDelimiter
close access fileRef
on error errStr
close access fileRe
activate
display alert ("unable to read file")
return
end try
set AppleScript's text item delimiters to colDelimiter
repeat with i from 1 to length of theData
set thisList to {text items of item i of theData}
set newList to newList & thisList
end repeat
set AppleScript's text item delimiters to ""
end readCSVFile
Copy link to clipboard
Copied
Of course, you do know that you can link an Excel file as part of its being imported. That way, the table will update when the data changes. The problem would then be to have some way for the table to be modified (to only include the items you want) before importing. Then each time data changes in Excel a modified file is created to replace the original modified file (and update in InDesign).