• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

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

New Here ,
Dec 03, 2022 Dec 03, 2022

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!

TOPICS
Scripting , UXP Scripting

Views

274

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 04, 2022 Dec 04, 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

Likes

Translate

Translate
Community Expert ,
Dec 03, 2022 Dec 03, 2022

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

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

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 04, 2022 Dec 04, 2022

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

Perfect!

Thank you very much!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 04, 2022 Dec 04, 2022

Copy link to clipboard

Copied

LATEST

You're welcome!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines