Copy link to clipboard
Copied
Hi, what am I doing wrong?
I am trying to apply a paragraph style based on data read from a csv file.
If the 11th field in the csv data line says "bold" I would like to apply a "bold" paragraph style to that line.
My code:
var csv_file = File('Test.csv');
csv_file.open('r');
var contents = csv_file.read();
csv_file.close();
var lines = contents.split("\n");
var NoOfLines = lines.length;
var data = [];
var myDoc = app.activeDocument;
var myTextFrame = MyDoc.TextFrames[0];
var BOLD = MyDoc.paragraphStyles.item("bold");
for (var i = 0; i < NoOfLines; i++) {
var line = lines[i];
var record = line.split(",");
data.push(record);
}
for (var i = 1; i < NoOfLines; i++){
var line = lines[i];
myTextFrame.contents += line + "\r";
if (data[i][11] == "bold") {
MyDoc.textFrames[0].paragraphs[i].appliedParagraphStyle = BOLD;
}
}
BOLD style gets applied to all the text in the frame. Any suggestions?
Thanks!
Oops sorry I introduced that bug. Anyway it was handy to get your sample data file, I've updated the script above to work with your sample data file (as well as remove my bug). See how that goes.
- Mark
Copy link to clipboard
Copied
Hi @mpp_piotrp, you are almost there! I've made some changes to your script so please have a careful look through and see what I've done differently. (Edit: fixed a bug I introduced and added a little more documentation.)
function main() {
// read csv file
var csv_file = File('~/Desktop/Test.csv');
if (!csv_file.exists) {
alert('CSV file doesn\'t exist');
return;
}
csv_file.open('r');
var contents = csv_file.read();
csv_file.close();
var rows = contents.split("\n");
var dataRowCount = rows.length;
// add contents to document
var myDoc = app.activeDocument;
var myTextFrame = myDoc.textFrames[0];
var BOLD = myDoc.paragraphStyles.item("bold");
var NORMAL = myDoc.paragraphStyles.item("regular");
for (var i = 0; i < dataRowCount; i++) {
var record = rows[i].split(",");
if (
myTextFrame.characters.length > 0
&& myTextFrame.characters[1].contents !== '\r'
)
myTextFrame.insertionPoints[-1].contents = '\r';
// this is where you format the line
var lineContents = record.join(' ')
// remove double-spaces
.replace(/\s+/g, ' ');
myTextFrame.insertionPoints[-1].contents = lineContents;
if (!myTextFrame.paragraphs[i].isValid)
continue;
if (record[10] == "bold")
myTextFrame.paragraphs[i].appliedParagraphStyle = BOLD;
else
myTextFrame.paragraphs[i].appliedParagraphStyle = NORMAL;
}
}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Enter CSV');
Some of the problems were due to incorrect capitalisation (eg. MyDoc vs myDoc are two different objects). I also put a test to check if the csv file exists. I also used InsertionPoints rather than replacing the whole contents (because replacing the whole textframe's contents will apply the first character's paragraph style to the whole text). I also changed some of your variable names to distinguish better between objects, eg. data lines I called rows so they can't be confused with indesign paragraph lines. That is up to you. I think it is a good practice to apply a paragraph style to every paragraph, not just the "bold" ones, so I made a style called "regular", too.
Well I hope that gives you a boost to take it to the next level. You are doing really well so far.
- Mark
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Oops sorry I introduced that bug. Anyway it was handy to get your sample data file, I've updated the script above to work with your sample data file (as well as remove my bug). See how that goes.
- Mark
Copy link to clipboard
Copied
Perfect!
Thank you very much!
Copy link to clipboard
Copied
You're welcome!