2 Replies Latest reply on Aug 3, 2015 6:44 AM by davidn65673556

    Help with a script to find text in a Table Cell and apply Cell style

    davidn65673556

      Hi,

      I’m building up the script that Jongware wrote in his post here http://indesignsecrets.com/tackling-tables-through-scripting.php – I’m trying to create a variable in which I can add a number of different bits of text, in this instance it’s different UK locations i.e. ‘London’, ‘South East’, ‘Scotland’ etc. I just need the script to apply the Cell Style – ‘District Cell’ – to any cell that contains any of the text in the variable. Below is the script, if anyone can help I’d be thankful.

       

      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;
      }
      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!
      
      
      //Find Text in Cell and apply Cell Style
      var textInCell=['London', 'Scotland', 'South West'];
      for (i=0; i<table.cells.length; i++)
      {
      if (table.cells[i].texts[0].contents==textInCell)
      table.cells[i].appliedCellStyle = "District Cell";
      }
      
      
      
      
      }