Skip to main content
Known Participant
January 24, 2018
Answered

write contents of table to a text file

  • January 24, 2018
  • 1 reply
  • 502 views

I have a need to export the tables from all the documents of a book. It would be nice to also increment a counter so that the number of each table also gets exported along with the text. I have tried _.write() as well as _.exportFile(). The _.write() creates the files but they are empty and the _.exportFile() approach throws a "no a function" error. Here are the two versions. Note: I don't run both at the same time. I just include them here to make for easier commenting. I suspect I am failing to address the proper constructor type or something. Any help would be very much appreciated.

var myDoc = app.activeDocument;

var myTables = myDoc.stories.everyItem().tables.everyItem().getElements();

var fname = app.activeDocument.name;

for (var d=0; d<myTables.length; d++)

{

   var a = new File("~/Documents/table"++".txt");

   var b = myTables.contents;

   var d = b.join("\r");

 

  

   a.open("w");

   a.write(d);

   a.close(); 

   d.exportFile(ExportFormat.TEXT_TYPE, a, false);

}

This topic has been closed for replies.
Correct answer -hans-

in line 8 you tried to define variable 'd' new ... though it already exists ...

exportFile is associated to a story not a table nor string ...

1 reply

-hans-Correct answer
Inspiring
January 24, 2018

in line 8 you tried to define variable 'd' new ... though it already exists ...

exportFile is associated to a story not a table nor string ...

IowaBoy9Author
Known Participant
January 24, 2018

Thank you Hans!

I changed it as shown below and now it works perfectly.

var myDoc = app.activeDocument;

var myTables = myDoc.stories.everyItem().tables.everyItem().getElements();

var fname = app.activeDocument.name;

for (var d=0; d<myTables.length; d++)

{

   var a = new File("~/Documents/table"++".txt");

   var b = myTables.contents;

   var e = b.join("\r");

 

  

  

   a.open("w");

   a.write(e);

   a.close(); 

  }