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

Shading straddled cells

New Here ,
May 28, 2015 May 28, 2015

Copy link to clipboard

Copied

I have a table with 10 columns and 2 body rows. Before the script is run, cell 1 in the first table row (which contains text) is straddled over all cells in the row (cells 1-10). My script so far deletes the second row then unstraddles the top row and restraddles it over cells 1-7, leaving 3 cells unstraddled (cells 8,9 & 10)

I then want to straddle these 3 cells together (straddle cell 8 over 8, 9 and 10) and shade them. Below is the script I have so far which gets me as far as selecting the three cells to be straddled and shaded. Is it possible to straddle and shade a selection? Also, would someone please advice me for future posts how I post as code just to keep things tidy?


Thanks

var doc = app.ActiveDoc;

var tbl = doc.SelectedTbl;

        //Delete Row

        var row = tbl.FirstRowInTbl.NextRowInTbl;

        row.Delete ()

      

        if(tbl.ObjectValid() == true)

    {

        alert("Row has been deleted");

    }

      

      

        // Remove and re-apply the straddle

       var header=tbl.FirstRowInTbl.FirstCellInRow;

       if (header.ObjectValid()) {

       header.UnStraddleCells(1, 10);

       header.StraddleCells(1, 7);

       alert("Station Header Restraddled.");

        }

      

        else

        {

          

        alert("No cells to unstraddle.");

      

        }

      

      

       //Select the three cells I want to shade

       tbl.MakeTblSelection (0, 0, 7, 9);

     

      //Apply Green shading to cells

     var pgf = doc.TextSelection.beg.obj;

     var cellsForShading = pgf.InTextObj;

      // Get the "PBgreen" color object.

     var color = doc.GetNamedColor ("PBGreen");

   

     // Set the cell's properties to 100% PBGreen.

     cellsForShading .CellOverrideShading = color;

     cellsForShading .CellUseOverrideShading = true;

     cellsForShading .CellOverrideFill = 0;

     cellsForShading .CellUseOverrideFill = true;

      

       {

        tbl = tbl.NextTblInDoc;

        }

TOPICS
Scripting

Views

473

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 ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

If you use the Advanced Editor, you can use syntax highlighting on your code, which will format it with line numbers, indents, etc.

First of all, you don't have to select cells in order to straddle them (or shade them, etc.). Here is a simple script that you can use to experiment with the table containing your insertion point. Please let me know if you have any questions or comments.

Rick

#target framemaker

var doc = app.ActiveDoc;
var tbl = doc.SelectedTbl;

// Get the 8th cell in the second row.
// Cells are numbered from 0 so, use 7.
var row = tbl.FirstRowInTbl.NextRowInTbl;
var cell = getCell (row, 7);
if (cell) {

    // Straddle 1 row and 3 columns.
    cell.StraddleCells (1, 3); // (rows, columns)
    doc.Rehyphenate (); // Required so that straddles draw correctly on the screen.
}

function getCell (row, colNum) {

    var i, cell;

    cell = row.FirstCellInRow;
    while (cell.ObjectValid () === 1) {
        if (cell.CellColNum === colNum) {
            return cell;
        }
        cell = cell.NextCellInRow;
    }
}

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 ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

Here is how I am doing shading. Again, it is not necessary to select any cells before doing this. Also note my preference for using separate functions rather than having all of my code in one function. This facilitates easier troubleshooting and code reuse. -Rick

#target framemaker

var doc = app.ActiveDoc;
var tbl = doc.SelectedTbl;

// Get the 8th cell in the second row.
// Cells are numbered from 0 so, use 7.
var row = tbl.FirstRowInTbl.NextRowInTbl;
var cell = getCell (row, 7);
if (cell) {
    // Shade 3 cells starting with "cell".
    shadeCells (cell, 3, doc);
    doc.Redisplay (); // Refresh the screen.
}

function shadeCells (cell, num, doc) {
   
    var color, i;
   
    // I am going to use Yellow.
    color = doc.GetNamedColor ("Yellow");
    for (i = 0; i < num, cell.ObjectValid (); i += 1) {
        cell.CellOverrideShading = color;
        cell.CellUseOverrideShading = true;
        cell.CellOverrideFill = 0; // solid
        cell.CellUseOverrideFill = true;
        cell = cell.NextCellInRow;
    }
}

function getCell (row, colNum) {

    var i, cell;

    cell = row.FirstCellInRow;
    while (cell.ObjectValid () === 1) {
        if (cell.CellColNum === colNum) {
            return cell;
        }
        cell = cell.NextCellInRow;
    }
}

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
New Here ,
Jun 05, 2015 Jun 05, 2015

Copy link to clipboard

Copied

This works perfectly thanks! The functions that you've created here are available to call up whenever throughout this script? I find it odd that the function parameters come after the conditions. In my head it would make more sense to define the function before it is used in  Is this a standard way of working?

Cheers

Chris

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 ,
Jun 05, 2015 Jun 05, 2015

Copy link to clipboard

Copied

LATEST

Yes, the functions can be called from anywhere in the script. It is pretty much standard to define all of the functions at the end of the script.

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