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

Newbie Question #3: Modifying Table Data

Community Beginner ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

How do I navigate a table and add data to it? For instance:

var hero = ['Superman', 'Spiderman', 'Wonder Woman'];

var fName = ['Clark', 'Peter', 'Diana'];

var lName = ['Kent', 'Parker', 'Prince'];

to

TOPICS
Scripting

Views

341

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 , Jul 17, 2019 Jul 17, 2019

OK, I will tell you my philosophy of developing scripts. For each task, I almost always work with selections. I want to test each piece of my code on a selection if I can. So here, I am don't want to do two things (create a table, then fill it); I only want to test filling a table. So I insert a table, click in the table and test my code.

After I develop and test all of my individual tasks, then I worry about putting the pieces together. I will typically make a function for each task to facilitat

...

Votes

Translate

Translate
Community Expert ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

There are a lot of ways you can do this, but here is one way:

#target framemaker

var hero = ['Superman', 'Spiderman', 'Wonder Woman'];

var fName = ['Clark', 'Peter', 'Diana'];

var lName = ['Kent', 'Parker', 'Prince'];

var doc, tbl, row, cell, textLoc;

doc = app.ActiveDoc;

// Click in the table before running this.

tbl = doc.SelectedTbl;

// Populate the heading row.

row = tbl.FirstRowInTbl;

cell = row.FirstCellInRow;

while (cell.ObjectValid () === 1) {

    textLoc = new TextLoc (cell.FirstPgf, 0);

    if (cell.CellColNum === 0) {

        doc.AddText (textLoc, "Hero");

    }

    else if (cell.CellColNum === 1) {

        doc.AddText (textLoc, "First Name");

    }

    else if (cell.CellColNum === 2) {

        doc.AddText (textLoc, "Last Name");

    }

    cell = cell.NextCellInRow;

}

// Populate the body rows.

row = row.NextRowInTbl;

cell = row.FirstCellInRow;

while (cell.ObjectValid () === 1) {

    if (cell.CellColNum === 0) {

        populateColumn (cell, hero, doc);

    }

    else if (cell.CellColNum === 1) {

        populateColumn (cell, fName, doc);

    }

    else if (cell.CellColNum === 2) {

        populateColumn (cell, lName, doc);

    }

    cell = cell.NextCellInRow;

}

function populateColumn (cell, list, doc) {

   

    var counter, textLoc;

   

    counter = 0;

    while (cell.ObjectValid () === 1) {

        textLoc = new TextLoc (cell.FirstPgf, 0);

        doc.AddText (textLoc, list[counter]);

        counter = counter + 1;

        if (counter === list.length) {

            break;

        }

        cell = cell.CellBelowInCol;

    }

}

Votes

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 Beginner ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

Wow. More complicated than I thought. One initial question, since I am trying to create a new table and then modify that table from within the same script, the following does not make sense.

doc = app.ActiveDoc; 

// Click in the table before running this.

tbl = doc.SelectedTbl; 

Votes

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 ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

LATEST

OK, I will tell you my philosophy of developing scripts. For each task, I almost always work with selections. I want to test each piece of my code on a selection if I can. So here, I am don't want to do two things (create a table, then fill it); I only want to test filling a table. So I insert a table, click in the table and test my code.

After I develop and test all of my individual tasks, then I worry about putting the pieces together. I will typically make a function for each task to facilitate troubleshooting and reuse.

This methodology has helped me develop thousands of commercial scripts and compile hundreds of reusable functions.

Votes

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