Skip to main content
Inspiring
July 16, 2014
Answered

Can you make a script for modify cells?

  • July 16, 2014
  • 3 replies
  • 771 views

Hi, every one,

I am looking for a script can do this:

If a slection a range of cell, the script can:

(1) let the even number of column apply to character style “Color+Bold”
(2) fill with colour of “\d% of PANTONE .+”

before modify:

after modify:

SOMETIMES NEED TO

(3) fill with boundary line colour of “PANTONE .+”

John

This topic has been closed for replies.
Correct answer Kai Rübsamen

Hi John,

this is the first time, that I write a script, that should style only selected cells. So maybe a bit to complicated, but for me works this one.

You could enter in the lines 14-16 your informations. The script will have a look at your selection and will formating every second column in the selection.

It should work with selected rows, but also if you select only parts of e.g. column 3-6 or if you select the whole table.

It will not check, if your entered color or characterstyle is valid.

if ( app.selection.length > 0 && ( app.selection[0].constructor.name == "Cell" || app.selection[0].constructor.name == "Table" ) ) {

    if ( app.scriptPreferences.version >= 6 ) {

        app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "style every second column in selection" );        

    }

    else {

        main();

    }

} else {

    alert ( "Nothing or wrong selection!" );

}

function main() {

   

    var myColor = "Black";

    var myFillTint = 20;

    var myCharStyleName = "bold";

    var curSel = app.selection[0];

    var allCells = curSel.cells;

    var startCol = curSel.cells[0].name.split(":")[0]*1;  

    var endCol = curSel.cells[-1].name.split(":")[0]*1;  

    var counter = startCol + 1;

    for ( var i = 0 ; i < allCells.length; i++ ) {

        var curCell = allCells;

        var curCol = curCell.name.split(":")[0]*1;

        if ( curCol == counter ) {

            curCell.fillColor = myColor;

            curCell.fillTint = 20;

            curCell.texts[0].appliedCharacterStyle = myCharStyleName;

            counter = counter + 2;

        }

       

        if ( counter > endCol ) {

            counter = startCol + 1;

        } // end if

    } // end for

} // end main

3 replies

JohnwhiteAuthor
Inspiring
July 17, 2014

realy perfect

JohnwhiteAuthor
Inspiring
July 19, 2014

Hi, Kai Rübsamen

Can you help me to add a function into it?

I want to make the dollar sign in 2014 column apply character style "Bold+Italic", and 2013 column apply character style "Italic".

but somethines It will in other row, not always in second row, so I expect it has a dropdown list or checkbox let me tell it dollar sign in which row.

for be specify details please see this link:

How Can I add a some functions into script?

Regard,

John

JohnwhiteAuthor
Inspiring
July 17, 2014

Thank you Kai Rübsamen,

thank you very much, appreciate!

John

Kai Rübsamen
Kai RübsamenCorrect answer
Participating Frequently
July 17, 2014

Hi John,

this is the first time, that I write a script, that should style only selected cells. So maybe a bit to complicated, but for me works this one.

You could enter in the lines 14-16 your informations. The script will have a look at your selection and will formating every second column in the selection.

It should work with selected rows, but also if you select only parts of e.g. column 3-6 or if you select the whole table.

It will not check, if your entered color or characterstyle is valid.

if ( app.selection.length > 0 && ( app.selection[0].constructor.name == "Cell" || app.selection[0].constructor.name == "Table" ) ) {

    if ( app.scriptPreferences.version >= 6 ) {

        app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "style every second column in selection" );        

    }

    else {

        main();

    }

} else {

    alert ( "Nothing or wrong selection!" );

}

function main() {

   

    var myColor = "Black";

    var myFillTint = 20;

    var myCharStyleName = "bold";

    var curSel = app.selection[0];

    var allCells = curSel.cells;

    var startCol = curSel.cells[0].name.split(":")[0]*1;  

    var endCol = curSel.cells[-1].name.split(":")[0]*1;  

    var counter = startCol + 1;

    for ( var i = 0 ; i < allCells.length; i++ ) {

        var curCell = allCells;

        var curCol = curCell.name.split(":")[0]*1;

        if ( curCol == counter ) {

            curCell.fillColor = myColor;

            curCell.fillTint = 20;

            curCell.texts[0].appliedCharacterStyle = myCharStyleName;

            counter = counter + 2;

        }

       

        if ( counter > endCol ) {

            counter = startCol + 1;

        } // end if

    } // end for

} // end main

JohnwhiteAuthor
Inspiring
July 17, 2014

Hi, Kai Rübsamen,

It was amazing,you wrote this script, it runs very well.

thank you so much,

but what if I need to add a boundry line 0.5 pt
100% “PANTONE \d”

like this:

John

Kai Rübsamen
Participating Frequently
July 17, 2014

That was easier, than I thought before. Try this:

if ( app.selection.length > 0 && ( app.selection[0].constructor.name == "Cell" || app.selection[0].constructor.name == "Table" ) ) {

    if ( app.scriptPreferences.version >= 6 ) {

        app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "style every second column in selection" );        

    }

    else {

        main();

    }

} else {

    alert ( "Nothing or wrong selection!" );

}

function main() {

   

    var myColor = "Cyan";

    var myFillTint = 20;

    var myCharStyleName = "bold";

    var myStrokeWeight = 0.5;

    var curSel = app.selection[0];

    var allCells = curSel.cells;

    var startCol = curSel.cells[0].name.split(":")[0]*1;  

    var endCol = curSel.cells[-1].name.split(":")[0]*1;

    var startRow = curSel.cells[0].name.split(":")[1]*1;  

    var endRow = curSel.cells[-1].name.split(":")[1]*1; 

    var counter = startCol + 1;

    for ( var i = 0 ; i < allCells.length; i++ ) {

        var curCell = allCells;

        var curCol = curCell.name.split(":")[0]*1;

        var curRow = curCell.name.split(":")[1]*1;

        if ( curCol == counter ) {

            with ( curCell ) {

                fillColor = myColor;

                fillTint = myFillTint;

                texts[0].appliedCharacterStyle = myCharStyleName;

                rightEdgeStrokeWeight = myStrokeWeight;

                rightEdgeStrokeColor= myColor;

                leftEdgeStrokeWeight = myStrokeWeight;

                leftEdgeStrokeColor= myColor;                

            }

            if ( curRow == startRow ) {

                curCell.topEdgeStrokeWeight = myStrokeWeight;

                curCell.topEdgeStrokeColor= myColor;

            }

            else if (curRow == endRow ) {

                curCell.bottomEdgeStrokeWeight = myStrokeWeight;

                curCell.bottomEdgeStrokeColor= myColor;

            }

           

            counter = counter + 2;

        }

       

        if ( counter > endCol ) {

            counter = startCol + 1;

        } // end if

    } // end for

} // end main