Skip to main content
dublove
Legend
September 27, 2024
Question

How to modify this script to target the current textbox or selection

  • September 27, 2024
  • 2 replies
  • 211 views

This script fills empty cells of a table with a certain character (e.g. -).
It now affects the entire document.
Would anyone like to help modify it by adding the options: whole document, current article, current selection?
Thanks a lot.

 

var myDocument = app.documents.item(0);
var myStory = myDocument.stories;
for (var s=0; s<myStory.length;s++){
var myTable = myStory.item(s).tables;
for (var t=0; t<myTable.length;t++)
for (var i=0; i<myTable[t].columns.length; i++){
for (var j=0; j<myTable[t].columns.item(i).cells.length; j++){
if (myTable[t].columns.item(i).cells.item(j).contents == ""){
myTable[t].columns.item(i).cells.item(j).contents ="—"; //The character you want to fill
}
}
}
}

 

This topic has been closed for replies.

2 replies

Robert at ID-Tasker
Legend
September 27, 2024

@dublove 

 

The internal two loops - as you are going through all cells anyway - you can just loop through the cells of the Table:

 

for (var j=0; j<myTable[t].cells.length; j++)
{
  if (myTable[t].cells.item(j).contents == "")
  {
    myTable[t].cells.item(j).contents ="—"; //The character you want to fill
  }
}

 

dublove
dubloveAuthor
Legend
September 28, 2024

Okay, thanks a lot, I'll try it sometime.

Community Expert
September 27, 2024

For a single selection the story can be accessed using the following code.

app.selection[0].parentStory

You can modify your code accordingly, in this case you don't need the loop as there is only a single story

-Manan

-Manan