Skip to main content
Marcos_Suárez
Known Participant
January 2, 2016
Answered

Delete the first 4 characters in a cell... (IND CS4)

  • January 2, 2016
  • 1 reply
  • 738 views

I need to delete the first 4 characters when the text of the cells begins with "<Cs".


Is it possible without using "Search/Replace"?

My script is something like this... 


var doc=app.documents[0];

var allTablesArray=doc.stories.everyItem().tables.everyItem().getElements();

/************************************************************************/

var TbCount = allTablesArray.length;

for(var Tb=0; Tb<TbCount; Tb++) {

  var Tab = allTablesArray[Tb];

  var numRows = Tab.rows.length - 1;

  for(var R=numRows; R>=0; R--) {

  var numCells = Tab.rows.cells.length - 1;

  for(var Td=numCells; Td>=0; Td--) {

  var xCell = Tab.rows.cells;

  var Content_3 = xCell.contents.slice(0,3);

  if (Content_3 == "<Cs") {

  //Remove the first 4 characters in the cell

  }

  if (Content_3 == "<Rs") {

  //Remove the first 4 characters in the cell

  }

  }

  }

}

//*****************

This topic has been closed for replies.
Correct answer Ronald63

Hi,

Try this...

var doc=app.documents[0];  

var allTablesArray=doc.stories.everyItem().tables.everyItem().getElements(); 

/************************************************************************/ 

var TbCount = allTablesArray.length; 

for(var Tb=0; Tb<TbCount; Tb++) { 

    var Tab = allTablesArray[Tb]; 

    var numRows = Tab.rows.length - 1; 

    for(var R=numRows; R>=0; R--) { 

    var numCells = Tab.rows.cells.length - 1; 

        for(var Td=numCells; Td>=0; Td--) { 

        var xCell = Tab.rows.cells; 

        var Content_3 = xCell.contents.slice(0,3); 

            if (Content_3 == "<Cs" || Content_3 == "<Rs") { 

                xCell.contents= Content_3.slice(4,Content_3.length);

            } 

        } 

    } 

}

1 reply

Ronald63Correct answer
Legend
January 2, 2016

Hi,

Try this...

var doc=app.documents[0];  

var allTablesArray=doc.stories.everyItem().tables.everyItem().getElements(); 

/************************************************************************/ 

var TbCount = allTablesArray.length; 

for(var Tb=0; Tb<TbCount; Tb++) { 

    var Tab = allTablesArray[Tb]; 

    var numRows = Tab.rows.length - 1; 

    for(var R=numRows; R>=0; R--) { 

    var numCells = Tab.rows.cells.length - 1; 

        for(var Td=numCells; Td>=0; Td--) { 

        var xCell = Tab.rows.cells; 

        var Content_3 = xCell.contents.slice(0,3); 

            if (Content_3 == "<Cs" || Content_3 == "<Rs") { 

                xCell.contents= Content_3.slice(4,Content_3.length);

            } 

        } 

    } 

}

Marcos_Suárez
Known Participant
January 2, 2016

Well, with a little change, it works...!!!

......

        var xCell = Tab.rows.cells;

        var ContentAll = xCell.contents;   

        var Content_3 = xCell.contents.slice(0,3);   

            if (Content_3 == "<Cs" || Content_3 == "<Rs") {   

                xCell.contents = ContentAll.slice(5,ContentAll.length);  

......

Thanks a lot...!!!

Community Expert
January 3, 2016

Hi Ronald and Marcos,

if you assign new contents, you will end up with wrong formatting perhaps.

I would do it this way, so the formatting is preserved:

var doc = app.documents[0];

var cells = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

for(var n=0;n<cells.length;n++)

{

  

    var contents = cells.contents;

  

    if((contents.match(/^<Cs/) || contents.match(/^<Rs/)) && contents.length > 3)

    {

          // Change the range if the character "<" is not counting as character:  

          cells.texts[0].characters.itemByRange(0,3).remove();

    };

  

};

See the following example showing formatted text.

Note 1: Ronald's original snippet removed all characters in the cell.

Note 2: Marcos' edited version is removing 5 characters.

The title of this thread is: "Delete the first 4 characters…"

My snippet is doing this.

Uwe