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

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Help with a script to find text in a Table Cell and apply Cell style

Viewing 2 reply threads
  • Author
    Posts
    • #77094
      David Nightingale
      Participant

      Hi,

      I’m building up the script that Jongware wrote in his post here https://creativepro.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”;
      }

      }

    • #77098

      You were rather close :) But the problem is that you cannot easily iterate over a text array and check if one of its elements matches other text. Your straightforward == will not do anything useful – at best, it checks if the contents is equal to the text array in its entirety.

      Try this: convert the array into a regular expression with each phrase separated by an OR, and test your cell contents with that:

      function __processTable(table)
      {
        // do something here!
      
        //Find Text in Cell and apply Cell Style
        var textInCell=['London', 'Scotland', 'South West'];
        var myRegEx = new RegExp("\\b("+textInCell.join("|")+")\\b");
        for (i=0; i<table.cells.length; i++)
        {
          if (table.cells[i].texts[0].contents.match(myRegEx))
            table.cells[i].appliedCellStyle = "District Cell";
        }
      }
    • #77111
      David Nightingale
      Participant

      Hey that’s great. I’ve tweak the GREP a little so what is selected is more exact.

      var
      textInCell=[‘Corporate’, ‘Scotland’, ‘South West’, ‘London South & South East’, ‘Powered Access’, ‘Rail’, ‘Traffic’, ‘Leada Acrow’, ‘PSS Hire’, ‘FLG Services’, ‘Health & Safety & Training’];
      var myRegEx = new RegExp(“^(“+textInCell.join(“|”)+”)$”);
      for (i=0; i<table.cells.length; i++)
      {
      if (table.cells[i].texts[0].contents.match(myRegEx))
      table.cells[i].appliedCellStyle = “District Cell”;
      }

      I’ve got one other element to add in to my table formatting. I want to find a cell with the word ‘Budget’ (but this may be written as ‘Budget:’ or ‘Budget: (E)’ – but without the speech marks) and apply the Cell Style – SponsorCellStyle – to the whole row. I’ve currently got this going on, but it isn’t working:

      //Find Text in Cell and apply Cell Style to Row
      for (i=0; i<table.cells.length; i++)
      {
      if (table.cells[i].texts[0].contents == “Budget:”)
      table.rows.everyItem().appliedCellStyle= “Sponsor/Owner Cells”;
      }

      Could you possibly help with this last bit?

Viewing 2 reply threads
  • You must be logged in to reply to this topic.
>