Copy link to clipboard
Copied
Hello, everyone. Hello @Peter Kahrel.
I Use a script to get the current mouse-selected rows and repeat them.
I Saw a posting above on "creativepro.com" , at the bottom of the page, where Peter Kahrel gave a solution at the end.
I mimicked one and for some reason it prompted an error.
https://creativepro.com/topic/get-selected-table-rows-with-extendscript/
Help me look at it.
Thank you very much.
Below is the code I cobbled together.  There's also an attachment.
var cell = app.selection[0].parent.parentRow.cells[0];
var myTable = cell.parent.parent;
var selectedRows = myTable.rows.itemByRange(cell,myTable.cells[-1]).select();
//var myTable = cell.parent
//firstRow = myTable.rows[0];
if (firstRow.rowType !== RowTypes.HEADER_ROW)
{
dupeTopRow(myTable);
myTable.rows.add(LocationOptions.BEFORE, selectedRows);
/**
* duplicate the first row af a myTable
* @ param the myTable
* @ return void
*/
function dupeTopRow(t){
var newRow = t.rows.add(LocationOptions.BEFORE, t.selectedRows);
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 ();
}
Copy link to clipboard
Copied
Too many parents:
var cell = app.selection[0].parent.parentRow.cells[0];
First "parent" - gets you the Table - so you need to drop it, because Table doesn't have "parentRow":
var cell = app.selection[0].parentRow.cells[0];
But then - you still have a problem:
And if you want to ping Peter - you need to make a space before "@".
Copy link to clipboard
Copied
OK, I've checked the original code - you haven't read first line of Peter's reply:
"This works: select an an insertion point in any cell, then do this:"
But your selection is bunch of Cells.
Copy link to clipboard
Copied
I read it wrong.
I thought that this would get the currently selected rows.
Copy link to clipboard
Copied
I used a dumb way to duplicate rows 1 and 2.
Couldn't this be done with just one function?
It would be easier if you could make a judgment based on the rows selected by the mouse.
Please guide me.
//dub header
var cell = app.activeDocument.selection[0].parent
var myTable = cell.parent
firstRow = myTable.rows[0];
//duplicat row1
if (firstRow.rowType !== RowTypes.HEADER_ROW)
{
dupeTopRow(myTable);
/**
* 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]);//past here
var newCell = newRow.cells
var lr = t.rows[1].cells//copy which row
for (var i = 0; i < newCell.length; i++){
newCell[i].properties = lr[i].properties;
};
}
//dublicat row2
dupeTopRow2(myTable);
function dupeTopRow2(b){
var newRow2 = b.rows.add(LocationOptions.BEFORE, b.rows[1]);//past here
var newCell2 = newRow2.cells
var nr = b.rows[3].cells//copy which row
for (var j = 0; j < newCell2.length; j++){
newCell2[j].properties = nr[j].properties;
};
}
}
else{
alert("header exist")
exit ();
}
Copy link to clipboard
Copied
Can you just stick to one post/thread instead of creating several new posts around the same issue? It is difficult for us to track and manage.
Copy link to clipboard
Copied
Sorry, I will correct and avoid.
It doesn't seem to be the same problem, just using the same example code.
Copy link to clipboard
Copied
Moved from other thread.
Copy link to clipboard
Copied
Are you a forum administrator, and can you merge posts.
So how do you abbreviate the above code?
Directly, duplicate Row1-row2.
My this is running, , it's so unscientific, and I always feel bad about it.
I even wrote 2 scripts, one for the 1-row table header and one for the 2-row table header.
It would be nice to automatically duplicate the row currently selected by the mouse.
Copy link to clipboard
Copied
This is a good example of not adapting an existing script. You got hung up with trying to select a row. But that's irrelevant, because you always want to deal with a table's first and second rows. So you need to get a reference to a table, insert two rows after the second row, then duplicate the cells contents of the first row to the third row, and the contents of the cells in the second row to those in the fourth one.
Here's a fairly naive script that follows that line.
// Select an insertion point anywhere in the table
// (i.e. click anywhere in the table)
table = app.selection[0].parent.parent;
// Add a row after the first row
table.rows.add (LocationOptions.AFTER, table.rows[1]);
// Duplicate the contents of the cells in the first row
// to the cells in the third row
cells1 = table.rows[0].cells;
cells2 = table.rows[2].cells;
for (i = 0; i < cells1.length; i++) {
cells1[i].texts[0].duplicate (
LocationOptions.AFTER,
cells2[i].texts[0].insertionPoints[0]
)
}
// Add a row after the third row
table.rows.add (LocationOptions.AFTER, table.rows[2]);
// Duplicate etc.
cells1 = table.rows[1].cells;
cells2 = table.rows[3].cells;
for (i = 0; i < cells1.length; i++) {
cells1[i].texts[0].duplicate (
LocationOptions.AFTER,
cells2[i].texts[0].insertionPoints[0]
)
}
Copy link to clipboard
Copied
Thank you very much.
This one is logically easier to understand