Copy link to clipboard
Copied
Hallo,
ich bekomme regelmäßig mehrseitige Tabellen aus Excel, die ich formatieren muss. Diese Tabelle hat immer zwei Spalten, eine mit Text und eine mit einer dazugehörenden Zahl.
Die Formatierung für Indesign ist folgendermaßen:
Ich möchte nun nach den jeweiligen Zellen suchen und diese in Zellenformate umwandeln. Folgende Schritte muss ich machen:
Meine Frage ist nun, ob ich das irgendwie automatisieren kann mit Eingaben in dem Suchen&Ersetzen-Menü? Oder ein Script (wobei ich mich hier gar nicht auskenne)?
Ich hoffe, ich konnte verständlich machen, was ich machen möchte und bin gespannt, ob Ihr mir dabei helfen könnt?
Zur Verdeutlichung habe ich einen Screenshot gemacht (vor und nach der Formatierung).
jd
Copy link to clipboard
Copied
Ich würde das gar nicht mit einer Tabelle machen. Absatzformate und nächstes Absatzformat, Eintrag und Ziffer getrennt durch Umschalt+Tabulator.
Hat es irgendeinen Grund, weshalb es eine Tabelle sein muss?
Sonst könnte man doch ganz einfach mit Tabellenformaten arbeiten. Kopfzeile, Body, Body rechte Spalte. Manuell die Unterüberschrift.
Copy link to clipboard
Copied
Hallo Willi,
danke für die Antwort, aber dann habe ich mich nicht so richtig ausdrücken können.
Es müssen leider Tabellen sein und die Tabellenzeilen, die du vorschlägst, sind natürlich auch so zugewiesen.
Aber es handelt sich um eine sehr lange Tabelle, die einen Tabellenkopf hat, der auch zugewiesen ist.
Nun kommen z.B. alle 6 sib 10 Zeilen eine Unterüberschrift, die wie oben beschrieben formatiert werden soll. Akutell wähle ich diese Zeile aus und weise Ihr mit einem Shortcut eine Formatierung zu.
Die Zeile direkt darunter bekommt dann eine weitere Formatierung, indem ich diese ebenfalls auswähle und zuweise.
Die 4-8 Zeilen darunter bleiben als Body formatiert, bis die nächste Zeile kommt, die als Unterüberschrift formatiert werden soll.
Meine Hoffnung war nun, dass ich nach wiederkehrenden Vorgaben dieser Zeilen (rechte Zelle der Zeile leer oder mit der Zahl »100« gefüllt) suchen und dann diese gesamte Zeile automatisiert eine Formatieren zuweisen kann.
Aber das scheint nicht so ohne weiteres möglich zu sein...
Trotzdem Danke für den Versuch.
Viele Grüße, jd
Copy link to clipboard
Copied
Das kann mit Absatz- und auch mit Objektformaten automatisiert erfolgen.
Copy link to clipboard
Copied
Die Frage ist halt wie?
Aber das Skript von Mark scheint hier die Lösung zu bringen...
Copy link to clipboard
Copied
Hi Jan, I've written a script that I hope will do what you need. Please adjust the variables with your correct Cell Style names, make a selection that includes one or more tables, and run script.
- Mark
Edit: by the way, if you want to apply to ALL tables in the document, change line 23 to "var tables = getTables(doc);"
/*
Set Table Row Styles With Criteria
by m1b
here: https://community.adobe.com/t5/indesign-discussions/tabellen-zellen-suchen-und-formatieren-mit-grep/m-p/12705550
*/
function main() {
// table cell style names:
var headCellStyleName = 'Head',
subheadCellStyleName = 'Subhead',
normalStyleName = "Normal";
var doc = app.activeDocument;
if (doc.selection.length == 0) {
alert('Please make a select one or more tables and run script again.');
return;
}
var tables = getTables(doc.selection);
for (var i = 0; i < tables.length; i++) {
for (var r = 0; r < tables[i].rows.length; r++) {
var row = tables[i].rows[r];
// set style based on criteria:
if (
// HEADING ROW
row.cells.lastItem().contents == ''
) {
setStyle(doc, row, 'cellStyle', headCellStyleName, true);
// merge heading row
row.cells.firstItem().merge(row.cells.lastItem());
} else if (
// SUBHEADING ROW
row.cells.lastItem().contents == '100'
) {
setStyle(doc, row, 'cellStyle', subheadCellStyleName, true);
} else {
// NORMAL ROW
setStyle(doc, row, 'cellStyle', normalStyleName, true);
}
}
}
// convenience function for getting tables
function getTables(items, found) {
if (items == undefined) return [];
if (found == undefined) found = [];
if (!items.hasOwnProperty('0')) items = [items];
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (
// item is a table
item.constructor.name == 'Table'
) {
found.push(item);
} else if (
// item contains tables
item.hasOwnProperty('tables')
&& item.tables.length > 0
) {
found = found.concat(getTables(item.tables));
} else if (
// parent is a table cell
item.hasOwnProperty('parent')
&& item.parent.constructor.name == 'Cell'
) {
found = found.concat(getTables([item.parent.parent]));
} else if (
// is in a story, which may contain tables
item.hasOwnProperty('parentStory')
) {
found = found.concat(getTables([item.parentStory]));
} else if (
// contains page items, which may be tables
item.hasOwnProperty('pageItems')
&& item.pageItems.length > 0
) {
found = found.concat(getTables(item.pageItems));
} else if (
// has a parent which might be a table
item.hasOwnProperty('parent')
&& item.parent.constructor.name != 'Document'
) {
found = found.concat(getTables([item.parent]));
}
}
return found;
}
// convenience function for setting styles
function setStyle(doc, item, styleType, styleName, clearOverrides) {
if (doc == undefined || item == undefined || styleName == undefined) return;
if (clearOverrides == undefined) clearOverrides = true;
switch (styleType.toLowerCase()) {
case 'cellstyle':
if (doc.cellStyles.itemByName(styleName).isValid) {
if (
!item.hasOwnProperty('appliedCellStyle')
&& item.hasOwnProperty('cells')
&& item.cells.length > 0
) {
item = item.cells.everyItem();
}
item.appliedCellStyle = doc.cellStyles.itemByName(styleName);
if (clearOverrides) item.clearCellStyleOverrides(false);
} else {
throw ('Cell Style "' + styleName + '" not found.');
}
break;
case 'paragraphstyle':
if (doc.paragraphStyles.itemByName(styleName).isValid) {
item.texts[0].paragraphs.everyItem().appliedParagraphStyle = doc.paragraphStyles.itemByName(styleName);
if (clearOverrides) item.texts[0].clearOverrides(OverrideType.ALL);
} else {
throw ('Paragraph Style "' + styleName + '" not found.');
}
break;
case 'characterstyle':
if (doc.characterStyles.itemByName(styleName).isValid) {
item.texts[0].paragraphs.everyItem().appliedCharacterStyle = doc.characterStyles.itemByName(styleName);
if (clearOverrides) item.texts[0].clearOverrides(OverrideType.CHARACTER_ONLY);
} else {
throw ('Character Style "' + styleName + '" not found.');
}
break;
default:
throw ('Unrecognised style "' + styleName + '" (' + styleType + ').');
break;
}
}
} // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Set Table Cell Styles");
Copy link to clipboard
Copied
Jan, I realise you may not know how to install a script. Here is what you can do:
1. Select the text from my post - be careful to get just the "code" styled part
2. Make a new text file in Notepad or TextEdit (must be plain text—not rich text)
3. Paste the code
4. Save as "Set Table Row Styles.js" (note: file extension is "js") to your desktop
5. In Indesign, open the Script panel, right-click on on User folder and choose "Reveal"
6. Move the file you saved in step 4 to the User folder that should now be visible.
7. Back in Indesign, the script should appear in the User folder of Scripts Panel (see Window menu/Utility)
8. Double click to run the script
Copy link to clipboard
Copied
Hello Mark,
that works almost perfectly, the headline is recognized and reformatted.
However, the subline is not yet recognized. The criterion for this subline is that there is always a "100" in the second column/cell.
Where do I have to adjust the script so that this line is also recognized?
Thanks for your great help
jd
Copy link to clipboard
Copied
Sorry for the confusion, but I posted the last message from my wife's computer...
Copy link to clipboard
Copied
First, can you try an experiment? Just swap the names of the head and subhead styles in the script and run it. That will tell you something. Then swap them back. Did the "head" style get applied: that means the problem is the "subhead" style isn't being found I think. Or did the "Subhead" style get applied: that means the problem is the following line:
row.cells.lastItem().contents == '100'
It checks if the cell's contents *exactly* contains '100' and nothing else. Can you confirm that the subhead row only has two cells and that the right cell only contains '100'. You can turn on 'show invisibles' to see if there are other whitespace characters or something messing it up. I can easily make the line less fussy but ideally I'd like to know why it's not working as it is.
If you get stuck, try changing that line to:
row.cells.lastItem().contents.search('100') != -1
This means it will find any contents that has '100' even if it has whitespace before or after, or even if it is '1000' or '2100'.
- Mark
Copy link to clipboard
Copied
Copy link to clipboard
Copied
With this line it works:
row.cells.lastItem().contents.search('100') != -1
Is it possible to change the name of the script and where do I have to make these adjustments
Copy link to clipboard
Copied
You can change the name of the script by just renaming the .js file. That will change the name in the scripts panel.
- Mark
Copy link to clipboard
Copied
Now it works. Great!
Thank you very much for this script and your help!!
Copy link to clipboard
Copied
That error message is because it cannot find the cell style "Normal". The script expects you to supply a cell style name for the rows that aren't head or subhead.
- Mark