Skip to main content
Participant
December 4, 2022
Answered

How to apply paragraph style ti lines read from a csv file.

  • December 4, 2022
  • 1 reply
  • 1096 views

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!

This topic has been closed for replies.
Correct answer m1b

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

1 reply

m1b
Community Expert
Community Expert
December 4, 2022

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

Participant
December 4, 2022

Hi Mark,

Thank you for your reply.

Unfortunately, it is not working for me...

Line 32 - "row" is undefined. I think it should be "record". I changed it but still get an error.

I attached a sample csv and a screenshot of the error.

Thank you for your help.

Peter

 

 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
December 4, 2022

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