Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Guide ,
Sep 27, 2024 Sep 27, 2024

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
}
}
}
}

 

TOPICS
Bug , Feature request , How to , Scripting
190
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 27, 2024 Sep 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 27, 2024 Sep 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
  }
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 27, 2024 Sep 27, 2024
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines