Copy link to clipboard
Copied
For example:
I have a function that adjusts the width of selected table to the width of the textFrame.
Now I have selected a textFrame.
I used a loop.
I want each table to apply this once:
tbs[i].tabFitToTextFrame()
If you remove tbs[i] and use:
tabFitToTextFrame();
only the first tab will be modified.
I try Like this:
It doesn't seem to be working.
//var sel = app.documents[0].selection;
var doc = app.activeDocument;
var tbs = doc.selection[0].tables;
//var tbs = getTable(doc.selection);
alert(tbs);
//alert(sel.tables);
if (tbs.length > 0) {
for (var i = 0; i < tbs.length; i++) {
alert(tbs[i]);
tabFitToTextFrame();
}
}
function tabFitToTextFrame() {
var doc = app.activeDocument,
item = doc.selection[0];
var myTable = getTable(doc.selection);
parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];
factor = parentWidth / myTable.width;
for (col = 0; col < myTable.columns.length; col++)
myTable.columns[col].width *= factor;
}
function getTable(obj) {
if (obj == undefined)
return;
if ('Array' === obj.constructor.name) {
for (var i = 0, table; i < obj.length; i++) {
table = getTable(obj[i]);
if (table && table.isValid)
return table;
}
return;
}
if (obj.constructor.name == 'Cell')
return obj.parent;
if (obj.parent.constructor.name == 'Cell')
return obj.parent.parent;
if (
obj.hasOwnProperty('cells')
&& obj.cells.length > 0
)
return obj.cells[0].parent;
if (
obj.hasOwnProperty('tables')
&& 0 !== obj.tables.length
)
return obj.tables[0];
};
If I am understanding your requirements correct the following code would work
var doc = app.activeDocument;
var tbs = doc.selection[0].tables;
//var tbs = getTable(doc.selection);
alert(tbs);
//alert(sel.tables);
if (tbs.length > 0) {
for (var i = 0; i < tbs.length; i++) {
tabFitToTextFrame(tbs[i]);
}
}
function tabFitToTextFrame(myTable) {
parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];
factor = parentWidth / myTable.width;
for (
...
I think I've found the reason.
I have two myTable settings.
One is m1b's myTable = getTable(doc.selection);
This getTable seems to have an issue—it can't retrieve multiple tables. It's not a collection.
Copy link to clipboard
Copied
I updated the question.
Copy link to clipboard
Copied
If I am understanding your requirements correct the following code would work
var doc = app.activeDocument;
var tbs = doc.selection[0].tables;
//var tbs = getTable(doc.selection);
alert(tbs);
//alert(sel.tables);
if (tbs.length > 0) {
for (var i = 0; i < tbs.length; i++) {
tabFitToTextFrame(tbs[i]);
}
}
function tabFitToTextFrame(myTable) {
parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];
factor = parentWidth / myTable.width;
for (col = 0; col < myTable.columns.length; col++)
myTable.columns[col].width *= factor;
}
-Manan
Copy link to clipboard
Copied
Hi Manan Joshi.
That's it.
Thank you very much.
But I don't understand where the key difference lies.
I think I've tried your method before.
Copy link to clipboard
Copied
I think I've found the reason.
I have two myTable settings.
One is m1b's myTable = getTable(doc.selection);
This getTable seems to have an issue—it can't retrieve multiple tables. It's not a collection.
Copy link to clipboard
Copied
You did not need the getTables method as you already had the table collection in tbs variable. The getTables method returns back a table based on different input sent to it. Now you were always sending selection as input to the function and the selection is never changing so the function will keep on returning the same table everytime. Same input == Same output
-Manan
Copy link to clipboard
Copied
Strange, what did I change?
The width property setting is invalid.
Copy link to clipboard
Copied
Maybe you changes the selection or something that affects the input to the script
-Manan
Copy link to clipboard
Copied
It seems to be due to getTables again, sometimes returning null values.
Copy link to clipboard
Copied
But we concluded that your code would not need getTables. However, if you are using it then you need to handle the null check before the rest of the code is called up
-Manan
Find more inspiration, events, and resources on the new Adobe Community
Explore Now