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

This script for adding rows seems to be a bit off, please help fix it!

Guide ,
Dec 25, 2024 Dec 25, 2024

This script is used to determine if there is already a table header and add a row if there isn't.

Since this script doesn't require the table to be selected in full, I like it.
I found the script to be correct if there is only one table.
However, if there are more than two tables, my cursor is already inside the second table, but mytable still points to the previous table table[0].

 

 

//first mytable in the active document
var mytable=app.activeDocument.textFrames[0].parentStory.tables[0]
var firstRow = mytable.rows[0];
//to header and add 1 row;
if (firstRow.rowType !== RowTypes.HEADER_ROW)
{
dupeTopRow(mytable);
mytable.rows.add(LocationOptions.BEFORE, mytable.rows[0]);
}
else{
alert("header exist")
exit ();
}

 

 

 

 

TOPICS
Bug , Feature request , How to , Scripting
1.7K
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 ,
Dec 25, 2024 Dec 25, 2024

When I changed it to this.

 

 

 

 

var myTable=app.activeDocument.selection[0].tables[0]
var firstRow = myTable.rows[0];

 

 

 

 


It then says var firstRow = myTable.rows[0];

is an invalid object

 

So you have to add it.
.parentStory.tables[0]

 

var myTable=app.activeDocument.selection[0].parentStory.tables[0]
var firstRow =myTable.rows[0];

 

But if there are more than two tables in a text box, the problem arises again, and the script only recognizes the first table in the text box.

How to target only the current table

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 ,
Dec 25, 2024 Dec 25, 2024

You have threaded frames so it would be a single parentStory for both the frames in the thread. Hence the following statement would point to the 1st table irrespective of where your cursor is placed.

 

var mytable=app.activeDocument.textFrames[0].parentStory.tables[0]

 

With respect to your second approach you are trying to find table in your selection. However, if you just have your cursor in the table then it would not return any table as the selection is just an insertion point. However if you get to the parent of the selection then you will get to cell and if you check its parent then you will get the table.

 

var cell = app.activeDocument.selection[0].parent
var myTable = cell.parent

 

-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
Guide ,
Dec 26, 2024 Dec 26, 2024

I didn't get the function.
Tired.

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 ,
Dec 26, 2024 Dec 26, 2024

@dublove

 

You should really learn the internal structure(s) of the Document.

 

@Manan Joshi gave you the answer - so what's the point of your reply and why are you saying it's wrong? 

 

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 ,
Dec 26, 2024 Dec 26, 2024

I've provided examples. head1-3.idml
There are more than 2 tables in the same text box.
After running the script, the second table doesn't change, it says “Header already exists”.
 It is still pointing to the first table in the textbox.

Thank you~

 

 

 

 

//var myTable=app.activeDocument.selection[0].parentStory.tables[0]
var cell = app.activeDocument.selection[0].parent;
var myTable = cell.parent;
var firstRow =myTable.rows[0];
//to header and add 1 row;
if (firstRow.rowType !== RowTypes.HEADER_ROW)
{
dupeTopRow(myTable);
myTable.rows.add(LocationOptions.BEFORE, myTable.rows[0]);
}
else{
alert("header exist")
exit ();
}

 

 

 

 

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 ,
Dec 26, 2024 Dec 26, 2024

You should be really paying attention to how you format your Tables:

 

RobertatIDTasker_1-1735217262427.png

 

 

It's a SECOND Table in the Story - what @Manan Joshi mentioned earlier.

 

And you've marked header to be skipped - red frame.

 

Because it's skipped - it's invisible - my IDT can't get parent page - and you have set two rows as a header - green frame.

 

 

Like I've said before - learn the internal structure of things in InDesign and pay attention how you set-up things.

 

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 ,
Dec 26, 2024 Dec 26, 2024

thank you very much

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 ,
Dec 26, 2024 Dec 26, 2024
quote

It's really hard to determine the table where the cursor is.


By @dublove

 

No, it isn't - parent of the Cell is a Table - @Manan Joshi already showed you that.

 

It doesn't matter if you hide your header rows - they are still first rows internally - and that's what scripting reports to you - when you refer to the FIRST row - [0].

 

So if you want to know which rows are "visually first" - you need to check if the "skip first" is checked - and then how many rows is set as a header.

 

And as I've told you in another post - if you want to know which row is "visually first" in the part of the Table that spans across multiple TextFrames - you need to check ParentPage of the Text in a Cell - to make it quicker, you can iterate only through all Cells in the first Column.

 

 

And again - check internal structures of things in InDesign - and relationships between them:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

 

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 ,
Dec 26, 2024 Dec 26, 2024

I just want to add a row at the table top. Repeat for the second  row.
That alone.

 

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 ,
Dec 26, 2024 Dec 26, 2024

At the top of what? 

 

If you have a reference to the Table - it doesn't matter how many Tables is in the same Story. 

 

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 ,
Dec 26, 2024 Dec 26, 2024

I seem to have made a mistake.
I'm sorry.

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 ,
Dec 26, 2024 Dec 26, 2024

Yes, you did.

 

RobertatIDTasker_0-1735227206376.png

 

You want to modify 2nd Table - but the first line of your code - still refers to 1st table in the parent Story:

var myTable=app.activeDocument.textFrames[0].parentStory.tables[0];

what @Manan Joshi already pointed out.

 

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 ,
Dec 26, 2024 Dec 26, 2024

I was the one who got the function wrong.
I added a condition and it messed it up
I'm sorry everyone.

 

 

//first myTable in the active document
//https://community.adobe.com/t5/indesign-discussions/javacript-how-to-duplicate-a-table-header-row-in-indesign-server-version/m-p/12926231
//var myTable=app.activeDocument.textFrames[0].parentStory.tables[0]
//var firstRow =myTable.rows[0];

var cell = app.activeDocument.selection[0].parent
var myTable = cell.parent
firstRow = myTable.rows[0];
//copyhe

if (firstRow.rowType !== RowTypes.HEADER_ROW)
	{
	dupeTopRow(myTable);
	myTable.rows.add(LocationOptions.BEFORE, myTable.rows[0]);
/**
* duplicate the first row af a myTable 
* @ param the myTable 
* @ return void 
*/
function dupeTopRow(t){
    var newRow = t.rows.add(LocationOptions.BEFORE, t.rows[0]);
    var newCell = newRow.cells
    var lr = t.rows[1].cells
    for (var i = 0; i < newCell.length; i++){
        newCell[i].properties = lr[i].properties;
    };   
}
}
else{
alert("header exist")
exit ();
}

 

 

 

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 ,
Dec 26, 2024 Dec 26, 2024

Hi  @Manan Joshi @Robert at ID-Tasker 

Thank you very much.

I learned a lot more.

 

Is there a simple statement to get the currently selected row?
For example, I currently have selected Row 1,2 of myTable .
How to get it by command?
var cell = app.activeDocument.selection[0].parent
var myTable = cell.parent
selectedRow =

 

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 ,
Dec 26, 2024 Dec 26, 2024

Can you start checking: 

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

 

before you post your questions?? 

 

In 99% of cases - answer(s) to your questions are already there:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Cell_2.html

 

parentColumn

Column

readonly

The parent column of the cell.

parentRow

Row

readonly

The parent row of the cell

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 ,
Dec 26, 2024 Dec 26, 2024

I'm not a professional, I'm just a typesetter, I don't script, I just put it together and modify it slightly to my needs.

Sometimes I don't even know the professional name of a program.

 

But if you could hint at it, that would be much appreciated and I'll search the entire web as you point out.

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 ,
Dec 26, 2024 Dec 26, 2024

I already gave you link and explanation? 

 

Link is for the properties of the Cell object - you SHOULD study ALL of them - and then you've two properties that might be helpful - 2nd right now and 1st maybe in the future. 

 

I'm sorry, but you can't expect from anyone here to drop everything and find answers for you - especially for free - when you need a solution for your work - that I'm pretty sure isn't doing for free? 

 

 

Then, there is also Google - there are so many example codes that if you type "indesign js cell parent row" you'll find something.

 

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 ,
Dec 26, 2024 Dec 26, 2024

I see that @Robert at ID-Tasker was able to explain my point. Frankly I would not have been able to elaborate any better. Anyhow, @dublove now that you have some understanding of how things work with tables and some code snippets as well. I would suggest like @Robert at ID-Tasker, that you spend some time making sense of it all. These are simple things anyone can understand provided they are committed to spend some time. Then you have Google, ChatGPT to help as well.

 

The problem currently is two fold, one you are jumping to conclusions without putting an effort to understand what is being said and trying things. Second as soon as you are able to put something together you come up with a different ask without putting in effort to try it with your new knowhow. This approach is never gonna work

-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
Guide ,
Dec 26, 2024 Dec 26, 2024

Hearing you say that makes me think it's okay again.
Thank you very much.
Sometimes a little prompting is appreciated.

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 ,
Dec 30, 2024 Dec 30, 2024

Hi Manan Joshi.
Today I suddenly realized that “this piece of code” is inefficient to execute?

var cell = app.activeDocument.selection[0].parent
var myTable = cell.parent


If I have many tables in my textbox, I feel it queries (traverses) each table.

It takes 30 seconds.
But if there are only 2-3 tables, it does it in 2 seconds.

Is there a more efficient way to determine the current table.

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 ,
Dec 30, 2024 Dec 30, 2024

@dublove

 

No. It's just how InDesign works. 

 

Less contents = faster processing. 

 

It might help if you Save your file with a new name - to remove Undo History and re-organise it - if you've been working on your file for a long time. 

 

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 ,
Dec 30, 2024 Dec 30, 2024

I have the illusion that it looked for all the text boxes, and all the tables in the text boxes. Question.

I'll recheck 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
LEGEND ,
Dec 30, 2024 Dec 30, 2024
quote

I have the illusion that it looked for all the text boxes, and all the tables in the text boxes. Question.

I'll recheck it sometime.


By @dublove

 

?? 

 

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 ,
Dec 30, 2024 Dec 30, 2024

I suspect it looks for all text boxes, and every script in every text box.
When I have a free moment, I'll check each sentence

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