Copy link to clipboard
Copied
God of all gods, come up with an good idea together.
Hi m1b.
I am sorry, I still couldn't help myself. Confusion is torture.
When there is only one row in the header, it can be directly converted to a header.
like this:
var doc = app.activeDocument,
selectedRows = getRows(doc.selection[0]);
var headerRows = convertToRowType(selectedRows, RowTypes.HEADER_ROW);
But when there are cross row columns, conversion is not allowed.
Sometimes it needs to be converted to BODY_ROWS, and sometimes it needs to be converted to HEADER_ROWS.
Hi @dublove ,
please do comments on my code here in this thread and not in a different one.
You said in this other thread:
"The code doesn't make sense, my goal is to identify the selected row (and then I'll do the table header conversion).
But it seems to return the selected table?"
In this very thread you said:
"My true purpose is add:
Step 2: Convert the selec
Copy link to clipboard
Copied
You're working with InDesign scripting and appear to be trying to convert selected rows in a table to header rows.
Copy link to clipboard
Copied
Yes, do you have any good ideas?
Copy link to clipboard
Copied
@dublove
Credits: @Peter Kahrel (CE)
https://community.adobe.com/t5/indesign-discussions/script-to-convert-first-row-of-table-to-header-r...
var myTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
for (var i = 0; i < myTables.length; i++) {
var firstRow = myTables[i].rows[0];
try {
firstRow.rowType = RowTypes.HEADER_ROW;
} catch (e) {
alert ('Problem in table ' + firstRow.contents);
alert (e);
}
}
Copy link to clipboard
Copied
You misunderstood me.
You want to convert the currently selected rows to HEADER_ROWS.
What you discussed before was just a simple rows.
This is almost impossible for span rows(spread rows).
Ah, m1b understands me best.
Copy link to clipboard
Copied
Hi @dublove , It sounds like you are expecting row 1 to have 9 cells, but it only has 5:
//selected frame’s first table’s first row
var c = app.activeDocument.selection[0].tables[0].rows[0].cells
$.writeln(c.length)
//returns 5
for (var i = 0; i < c.length; i++){
c[i].fillColor = "Black"
c[i].fillTint = 20
};
Copy link to clipboard
Copied
Note that I selected two rows at the same time.
At this point, cross-rows(spead rows) merging is involved. You need to cancel the merge first, then convert to HEADER_ROWS (or BODY_ROWS), and then restore the merge.
Copy link to clipboard
Copied
Hi @rob day
Come up with a new idea.
Copy link to clipboard
Copied
Not sure what you are trying to do, but if it’s convert the first 2 rows of your example to headers, that throws an error
var t = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
$.writeln(t[0].rows[0].rowType)
//returns BODYROW
t[0].rows[0].rowType = RowTypes.HEADER_ROW
//error cannot set row type
Copy link to clipboard
Copied
This is the key question I want to ask.
When there is cross-rows(spread rows), the table rows do not allow transfers the rows to HEADER_ROWS.
How to solve this bug.
Copy link to clipboard
Copied
Is this possible?
Continue researching this.
Move rows that span columns (spread rows) to the HEADER_ROWS.
Copy link to clipboard
Copied
Let's all think of ways to solve this problem. Maybe you have a better idea.
Copy link to clipboard
Copied
Hi rob day.
This is a major bug.
m1b has proposed a solution.
First, cancel the cell merge, then restore the merged cells.
This solves the problem of copyable header rows.
However, it still does not solve the problem of converting to HEADER_ROWS.
Copy link to clipboard
Copied
Looks like the error is happening because you have split the rows. If I run this in a table that has not been split:
var c = app.activeDocument.stories.everyItem().tables.everyItem().rows.everyItem().getElements()
try {
c[0].rowType = RowTypes.HEADER_ROW
c[1].rowType = RowTypes.HEADER_ROW
alert("Rows 1 & 2 have been converted to headers" )
}catch(e) {
alert(e)
}
Run on your table with split rows:
Copy link to clipboard
Copied
Hi @rob day
Normal rows without split rows are converted to HEADER_ROWS, which is not difficult.
This is not a problem.
The issue we are discussing is:
How to convert split rows to HEADER_ROWS.
How to resolve this bug.
m1b attempted to resolve this by first canceling the merge and then restoring it, but was unsuccessful.
Copy link to clipboard
Copied
Is that possible?
A .First, cancel the merged cells.
B. Then switch to HEADER_ROWS.
C. Finally, restore the merged cells.
However, it seems that A to C cannot be interrupted.
Copy link to clipboard
Copied
You would have to create the headers before you merge the cells
//get the rows of table 1 of story 1
var r = app.activeDocument.stories[0].tables[0].rows.everyItem().getElements()//
try {
//set the first 2 rows to headers
r[0].rowType = RowTypes.HEADER_ROW
r[1].rowType = RowTypes.HEADER_ROW
//merge cell 1 of row 1 with cell 1 of row2
r[0].cells[0].merge(r[1].cells[0])
alert("Rows 1 & 2 have been converted to headers" )
}catch(e) {
alert(e)
}
Copy link to clipboard
Copied
Can you convert the table header?
Unable to set row type.
I don't think you understand what I mean.
Wait, let me explain again.
Copy link to clipboard
Copied
What I mean is,
I plan to select the first two rows. Convert their row type to HEADER_ROWS.
Note that I selected them by dragging. But that's not critical to solving this bug. The key is whether the conversion to HEADER_ROWS can be performed during the decomposition process.
If there are no cross-rows, the conversion is fine.
Check out m1b's original post; he set up a function called getRows();
I can get the length of the selected rows, so I can operate on any row.
If there are merged cells, it will prompt that the row type cannot be set.
In m1b's solution, A. first unmerge the cells, C. then merge the cells again.
I think if we can (i.e., after A unmerges the cells), B. immediately convert to HEADER_ROWS, C. then merge the cells again.
That would solve the problem, but A and C seem to be inseparable.
Copy link to clipboard
Copied
This code always fails because the cells are merged before the headers are set
//get the rows of table 1 of story 1
var r = app.activeDocument.stories[0].tables[0].rows.everyItem().getElements()//
try {
r[0].cells[0].merge(r[1].cells[0])
r[0].rowType = RowTypes.HEADER_ROW
r[1].rowType = RowTypes.HEADER_ROW
alert("Rows 1 & 2 have been converted to headers" )
}catch(e) {
alert(e)
}
This works on a table with no header rows set or cells merged
//get the rows of table 1 of story 1
var r = app.activeDocument.stories[0].tables[0].rows.everyItem().getElements()//
try {
//set the heads before merging
r[0].rowType = RowTypes.HEADER_ROW
r[1].rowType = RowTypes.HEADER_ROW
r[0].cells[0].merge(r[1].cells[0])
alert("Rows 1 & 2 have been converted to headers" )
}catch(e) {
alert(e)
}
Copy link to clipboard
Copied
@rob day Please refer to my previous reply.
These two pieces of code don't work either.
My question is, does it work with span rows?
It should work in all cases.
My goal is to fix the bug.
Copy link to clipboard
Copied
These two pieces of code don't work either.
You would have to start with a new table or construct the table via scripting. This makes a new table on page1:
//using millimeters
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;//
var d = app.activeDocument
//add a text frame on page 1 of active document
var f = d.textFrames.add ({ geometricBounds: [25,25,150,150]})
//add a 6 column table to the text frame with 2 header rows
var t = f.tables.add ({columnCount:6, bodyRowCount:3, headerRowCount:2, width:125})
//get the rows
var r = t.rows
//merge header cells
r[0].cells[0].merge(r[1].cells[0])
r[0].cells[1].merge(r[1].cells[1])
r[0].cells[2].merge(r[1].cells[1])
//header color
r[0].properties = ({fillColor:"Black", fillTint:20})
r[1].properties = ({fillColor:"Black", fillTint:20})
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
Copy link to clipboard
Copied
This is fixed and unchangeable.
It cannot be applied to any table.
Copy link to clipboard
Copied
Hi rob day.
It's still better to cancel the merge based on m1b and then restore it.
Currently, body to header works fine, but header to body doesn't.
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now