8 Replies Latest reply on Aug 7, 2015 4:53 AM by davidn65673556

    Help with script that finds content in a cell and apply cell style to row

    davidn65673556

      Hi


      I’m trying to add a section to my table formatting script that will 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 – SponsorCells – to all the cells on the whole row. I’ve currently got this going on, but it isn’t working:


      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!
      
      
      //Set 1st Row Height
      table.rows[0].height = "30mm";
      
      
      //Find Text in Cell and apply Cell Style to Row 
      var  
        myCellText=['Budget', 'Budget:', 'Budget: (E)'];  
       var myRegEx = new RegExp("^("+myCellText.join("|")+")$");
        for (i=0; i<table.cells.length; i++)
        {
          if (table.cells[i].texts[0].contents.match(myRegEx))
            table.cell[i].appliedCellStyle = "SponsorCells";
        }
      
      
      //end - do something here!
      }  
      


      i can get the script to apply the 'SponsorCells' cell style to the individual cells that contain the word 'Budget' etc. but I need ever cell on that row to have the cell style applied. I've tried re-writing line 56 which applies the style to a cell, but i can't seem to make it work. Any help would be great.


      Separate to this issue, i'd like to have a line of code similar to line 46, that defines the 1st rows height, but I'd like it to say 'If a cell has 'Cell Style A' applied then set height to '10mm'. If someone could through this in as a bonus help, things would be double great.


      Thanks in advance !