IMPORTING TABLES

in previous posts we discuss ways in which Adobe InDesign supports importing tables:

  • importing tables from an Excel file
  • importing tab/return delimited text and creating tables using convert to table

You can also import a table formatted as HTML and tables written in CALS table format.

Both of these two formas are actually imported as XML files. If you are not familiar with XML, don’t panic, we will be covering the topic of working with XML in the next few blog posts.

Although working with an imported HTML table takes a little doing, you may want to take a look at what you will be working with. If you have an HTML file with a table defined, follow along:

  1. First you will need to change the file so it can be imported as an XML file:
  2. change the file extension from HtML to XML
  3. delete everything outside of the <body>…</body>   tags.
  4. with an InDesign document open, choose Import XML from the File menu
  5. select the HTML file (now with an XML file extension)

The file should import as an XML file. Nothing happens? Don’t panic. An imported XML file does not get placed to the page automatically. Instead, it’s structure is placed in InDesign’s XML Structure pane (View > Structure…). Click on the twisty triangles next to each of the XML elements to disclose its contents If all went will, you will end up with a structure similar to the following:

body 
    table
	colgroup
	    col
	thead
	tfoot
	tbody
	    tr
		td

You may also have entries preceded with bullets next to them. These are XML attributes, ids and classes, that define css styling. To create a table from this XML structure takes a little doing, but it can be done. We will leave this topic until after we have looked at working with XML. For now, we will tackle the CALS tables format.

CALS TABLES

A file having tables in the CALS tables format also imports into an InDesign document using import XML. XML is a plain text tagging system similar to HTML. The difference is that XML tags can be defined by the user (“Extendable” Markup language). To provide for consistency, the tagging used is defined in a document called a DTD (Document Type Definition). The DTD for CALS tables is very robust.

For the person creating CALS tables, this is not exactly a walk in the park. But if you are given XML files with tables structured as CALS tables, the work has been done for you. CALS tables import into InDesign as tables, with little work on your side required. Most importantly, you just need to make sure the XML import preference import CALS tables is set to true.

If importing the file manually, choose Import XML…from InDesign’s File menu. Make sure Show XML Import Options is checked in the Open dialog and choose the file to import. When the XML Import Options dialog displays, be sure to check Import CALS tables as InDesign tables before clicking OK. The file with the table is imported and can be viewed in the XML Structure Panel (View > Structure). From there the table can be placed on the page.

If this last instruction left your head spinning, the following script will do the work for you (well, most of it, anyway). As part of the process, the script sets the XML import preference import CALS tables to true. Remember that when you change application preferences, a document needs to be created after the preference(s) is changed in order for the document to recognize the setting. For that reason, the script creates the document for you. Consequently, you will need a document preset named “Letter” (for an 8-1/2 x 11 inch document with primary text frame set to false and margins of 1/2 inch).

Suggested Documnet Preset setup

Import CALS Table (AppleScript)

--define XML file to import
set fileRef to choose file with prompt "Select XML file for import"
tell application "Adobe InDesign CC 2015"
	--set XML import preferences
	tell XML import preferences
		set import style to merge import
		set import CALS tables to true
		set repeat text elements to false
		set create link to XML to false
	end tell
	--create the document
	set docRef to make document with properties {document preset:"Letter"}
	--import the file; create references to XML root element and table style
	tell docRef
		import XML from fileRef
		set rootElement to XML element 1
		set tableStyleRef to table style "[Basic Table]"
	end tell
	--place the XML file
	tell page 1 of docRef
		set frameRef to place XML using rootElement place point {".5 in", ".5 in"}
	end tell
	--resize the table and style
	tell table 1 of frameRef
		set width to "7 in"
		set width of column 1 to "3 in"
		set width of column 2 to "2 in"
		set width of column 3 to "2 in"
		set applied table style to tableStyleRef
		clear table style overrides
	end tell
end tell 

Import CALS Table (ExtendScript)

//get reference to XML file to import
var fileRef = File.openDialog("Select XML file");
//set XML import prefrences
var importPrefs = app.xmlImportPreferences;
importPrefs.importCALSTables = true;
importPrefs.importStyle = XMLImportStyles.MERGE_IMPORT;
//create document with reference to first page and table style
var docRef = app.documents.add(true, "Letter");
var pageRef = docRef.pages.item(0);
var tableStyleRef = docRef.tableStyles.item("[Basic Table]");
//import the XML file and place at defined place point
docRef.importXML(fileRef)
var rootElement = docRef.xmlElements.item(0);
var frameRef = pageRef.placeXML(rootElement,[".5 in", ".5 in"]);
//resize the table and style
var tableRef = frameRef.tables.item(0);
tableRef.width = "7 in";
tableRef.columns.item(0).width = "3 in";
tableRef.columns.item(1).width = "2 in";
tableRef.columns.item(2).width = "2 in";
tableRef.appliedTableStyle = tableStyleRef;
tableRef.clearTableStyleOverrides();

Copy and paste the script into your favorite script editor. When you run the script you will be asked to choose an XML file to import. That’s it. Notice that when the script places the CALS table, InDesign creates its own text frame. A reference to this frame is returned to the script in the variable frameRef. The reference to the table then is table 1 of the frame (frameRef).

If you don’t have an XML file with a CALS table defined, you can copy from the screenshot below. Enter the text using a plain text editor. Save it with an .xml file extension. Make sure the file is encoded as Unicode (UTF-8). You can then use this file for testing with the script.

The result of running the script with the CAL table code above results in our sample table as shown below.

ONWARD AND UPWARD

Finish the sample script of your choice by adding code with a try/on error trap to make sure the file chosen by the user is an XML file. See if you can add styling for the text. In the next post we will take advantage of the XML structure to add text styling. Until then, look up “CALS table” on the web and get an overview of how powerful this format can be.