Copy link to clipboard
Copied
After running the script for a table:
1. Apply the 'tab' table style to the entire table
2. Convert the first row to a header
3. Add a blank row at the end of the table, convert it to "footer", and apply the "footer" cell style to the blank row, and set the footer behavior to 1.058mm;
Thank you.
Hi @dublove5CFE, here's a script to do what you want I think.
- Mark
/**
* Adjust selected tables.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/can-you-help-me-write-a-script-for-table-which-can-save-a-lot-of-work/m-p/13861606
*/
function main() {
var doc = app.activeDocument,
tables = getSelectedTables(doc),
tableStyle = getByName(doc.allTableStyles, 'Tabs'),
footerStyle = getByName(doc.allCellStyles, 'Footer');
if (tabl
...
Copy link to clipboard
Copied
Hi,
All the above you can actualy do with a correct built table style. So I wonder about the context?
1. Is it already a table or just content that you want to transform into a table. You need to have different approaches depending on.
Copy link to clipboard
Copied
> All the above you can actualy do with a correct built table style.
Well, you can't insert rows using a table style, or convert row types.
Copy link to clipboard
Copied
Hi @dublove5CFE, here's a script to do what you want I think.
- Mark
/**
* Adjust selected tables.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/can-you-help-me-write-a-script-for-table-which-can-save-a-lot-of-work/m-p/13861606
*/
function main() {
var doc = app.activeDocument,
tables = getSelectedTables(doc),
tableStyle = getByName(doc.allTableStyles, 'Tabs'),
footerStyle = getByName(doc.allCellStyles, 'Footer');
if (tables.length == 0)
alert('No tables selected.');
for (var i = tables.length - 1; i >= 0; i--) {
var table = tables[i];
// apply table style
table.appliedTableStyle = tableStyle;
// set all rows height
table.rows.everyItem().autoGrow = true;
table.rows.everyItem().minimumHeight = '10mm';
// convert first row to header
if (table.rows[0].rowType == RowTypes.BODY_ROW)
table.rows[0].rowType = RowTypes.HEADER_ROW;
// adjust footer row
var footerRow = table.rows.lastItem();
if (footerRow.contents.join('').length > 0)
footerRow = table.rows.add();
if (footerRow.rowType !== RowTypes.FOOTER_ROW)
footerRow.rowType = RowTypes.FOOTER_ROW;
footerRow.autoGrow = false;
footerRow.height = '1.058mm';
footerRow.cells.everyItem().appliedCellStyle = footerStyle;
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Adjust Tables');
/**
* Returns array of Tables,
* based on document's selection.
* @author m1b
* @version 2023-06-15
* @param {Document} doc - an Indesign Document.
* @returns {Array<Table>}
*/
function getSelectedTables(doc) {
if (
doc == undefined
|| doc.selection == undefined
)
return [];
var sel = doc.selection,
tables = [];
for (var i = 0; i < sel.length; i++) {
if (sel[i].hasOwnProperty('tables'))
for (var j = 0; j < sel[i].tables.length; j++)
tables.push(sel[i].tables[j]);
if (sel[i].parent.constructor.name == 'Table')
tables.push(sel[i].parent);
if (sel[i].parent.parent.constructor.name == 'Table')
tables.push(sel[i].parent.parent);
if (sel[i].constructor.name == 'Table')
tables.push(sel[i]);
}
return tables;
};
/**
* Returns a style matching name.
* @author m1b
* @version 2022-08-14
* @param {Array<style>} styles - an array or collection of styles.
* @param {String} name - the name to match.
* @returns {style}
*/
function getByName(styles, name) {
for (var i = 0; i < styles.length; i++)
if (styles[i].name == name)
return styles[i];
};
Edit 2023-06-14: set all rows' heights and footer row's height.
Edit 2023-06-15: now correctly adds footer row.
Edit 2023-06-15: footer row only added if last row isn't empty. Improved getSelectedTables function.
Edit 2023-06-16: now correctly adjusts footer row even if already a footer row.
Copy link to clipboard
Copied
Oh, my dear m1b.
Thank you very much. It works, but there are some issues.
After running, it will display as follows:
No blank lines added at the bottom, I need to add blank lines at the bottom.
And set the exact row height to 1.058mm, which is accurate, not the minimum value.
Another request: Can you set the height of the table body row to 10mm? I don't know why this cannot be set in the style.
Whether the cursor can be in the table is equivalent to selecting the entire table.
Thank you.
Copy link to clipboard
Copied
Hi @dublove5CFE, I have updated the script above to hopefully match your needs. Let me know how it goes.
- Mark
Copy link to clipboard
Copied
Hi m1b, thank you very much.
You still haven't understood what I mean.
You need to add a blank row at the end of the table and then manipulate the blank row.
It is not an operation on the 'G' line.
Set the height of the last blank row to an accurate 1.058mm and apply the 'footer' cell style.
Copy link to clipboard
Copied
Footer (newly added blank line) line height 1.058, all other lines are 10mm
Copy link to clipboard
Copied
Sorry @dublove5CFE I somehow removed the line that actually added the new row. Please try the updated code.
- Mark
Copy link to clipboard
Copied
Great, you're too great.
Thank you.
table.rows.everyItem().minheight = '10mm';
How to write the minimum line height of 10mm.
What I want is not an exact (absolute) value
Also, before running the script, can it be possible to not select all tables, and the cursor in the table is also equivalent to selecting all tables
Copy link to clipboard
Copied
table.rows.everyItem().minimumHeight = '10mm';
I understand this.
I cannot edit my post, there is no 'edit' option。
-----------I also hope to know about this one below--------------
Also, before running the script, can it be possible to not select all tables, and the cursor in the table is also equivalent to selecting all tables
Copy link to clipboard
Copied
So do you want the script to adjust all tables in document?
(By the way, I have improved the getSelectedTables function and made the correction to the footerRow logic.)
- Mark
Copy link to clipboard
Copied
Curious and strange.
I have manually dragged the 'Footer row' before,
The script is no longer valid for this line. Why is this?
The file is here.
Copy link to clipboard
Copied
table.rows.everyItem().minimumHeight = '10.0mm';
This doesn't seem completely correct?
You set the minimaHeight to 10mm.
After operation, the display shows that the row height is an accurate value (absolute value?) of 5.233 millimeters.
You need to manually change the precision (absolute?) option to 'minimum'.
Why is this?
Copy link to clipboard
Copied
How to determine the final line,
If the last row is empty, there is no need to add a new empty row, and the operation can be performed directly on this row.
If there is content in the last line, add a blank line and then operate on this line.
Copy link to clipboard
Copied
Hi @dublove5CFE, can you check if I've corrected these problems in the code above?
Copy link to clipboard
Copied
Hi~m1b
Perfect~
Thank you very, very much
Copy link to clipboard
Copied
Excellent! 🙂
Copy link to clipboard
Copied
Hello, m1b.
Have you seen this.
Manually dragged rows will not be affected by the script.
------
Curious and strange.
I have manually dragged the 'Footer row' before,
The script is no longer valid for this line. Why is this?
The file is here.
Copy link to clipboard
Copied
Hi @dublove5CFE, thanks for the demo file. I think the problem is that my script doesn't adjust the footer row if there is already a footer row. I changed the logic now—see updated code above.
- Mark
Copy link to clipboard
Copied
Tested,Perfect.
Absolute awesome~
Thank you very much.