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

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

Advocate ,
Sep 27, 2024 Sep 27, 2024

Copy link to clipboard

Copied

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

Views

70

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

 

Votes

Translate

Translate

Report

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
Advocate ,
Sep 27, 2024 Sep 27, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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