Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Scripts for Table Header and Footer

Return to Member Forum

  • Author
    Posts
    • #85583

      Hi,

      I am trying to create a script that will convert the last row of a table to a footer row (defined in a cell style within a table style) and do this once per text frame (throughout the entire document) and also add a header row once per page, to the first table on that page, again throughout the entire document.

      Here is my script so far:

      app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, “Process Table”);
      function checkUserSelection ()
      {
      var a_table = checkWhichTable();
      if (a_table == null)
      {
      if (confirm(“No table selected. Do you want to process *all* tables?”) == false)
      return;
      allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
      for (aTable=0; aTable<allTables.length; aTable++)
      {
      processTable (allTables[aTable]);
      }
      } else
      {
      processTable (a_table);
      }
      }

      function processTable(table)
      {
      // do something here!

      table.breakFooters = HeaderFooterBreakTypes.ONCE_PER_TEXT_FRAME;

      }

      function checkWhichTable()
      {

      // ensure the user made a selection

      if (app.selection.length != 1)
      return null;
      var currentTable = app.selection[0];
      if (currentTable.hasOwnProperty(“baseline”))
      {

      currentTable = app.selection[0].parent;

      }
      while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column)
      currentTable = currentTable.parent;
      if (!(currentTable instanceof Table))
      {

      // No table selected

      return null;
      }
      return currentTable;
      }

      This was taken from: https://creativepro.com/tackling-tables-through-scripting.php and seems to work. The bit I am struggling with is what to put after //do something here!

      If I run this script as it is in InDesign it adds an override to every table with the style applied (I only have one style for this document) but makes no visual change to the table.

      Any help would be much appreciated! I don’t expect anyone to go ahead and spend ages writing JavaScript for me but any pointers in the right direction would be great.

      Thanks!

    • #85627
      Ari Singer
      Member

      This should convert all your tables’ last rows to footer rows:

      app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Process Table");
      function main(){
          allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
          for (aTable=0; aTable<allTables.length; aTable++)
          allTables[aTable].rows[-1].rowType = RowTypes.FOOTER_ROW;
          }
      
    • #85631

      Thanks Ari S, this worked a charm!

      Do you know what I would use to exempt the last table on each page from being converted?

      Thanks!

    • #85669

      William, the script from Ari collects every table in every story. That’s very easy. To find the last table on every page, you must loop through every page and compare the vertical position of your frames with tables, since there exists no “last table on page” propertie.

      best
      Kai

    • #85695

      Thanks for your help Kai, this sounds quite challenging but will be good scripting practice.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.
Forum Ads