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

Help me finish my script to format a cell in a table header?

Participant ,
Oct 29, 2020 Oct 29, 2020

Hello!

I am writing a script and need a bit of help to get it finished.

I want the script to go through a whole document and look in the last cell in all the header rows for all the tables in the document and look for the specified text. If it finds the text, I want it to apply a cell style. If it doesn't find the text I am looking for, I want it to simply ignore that cell and table. Does that make sense?

Any help would be appreciated!

Thanks!

if(app.documents.length == 0){
    alert("There are no documents open.");
    exit();
    }
var myDoc = app.activeDocument;
var myTables = myDoc.stories.everyItem().tables.everyItem();
var myRows = myTables.rows.firstItem();
var myCells = myRows.cells.lastItem();
for(var i = 0; i < myDoc.stories.everyItem().tables.length; i++)
{
    if (myCells.contents[i] == "Price") {
        myCells.appliedCellStyle = "Cell Style 1";
    }
   if (myCells.contents[i] == "Qty") {
        myCells.appliedCellStyle = "Cell Style 1";
    }   
}

 

TOPICS
Scripting
544
Translate
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

Advocate , Oct 29, 2020 Oct 29, 2020

Try this snippet for your reference:

if(app.documents.length == 0){
    alert("There are no documents open.");
    exit();
    }
var myDoc = app.activeDocument;
for(var s = 0; s < myDoc.stories.length; s++){
    for(var t = 0; t < myDoc.stories[s].tables.length; t++){
        var tempTable = myDoc.stories[s].tables[t];
        if(tempTable.rows[0].rowType.toString() == "HEADER_ROW"){
            if(tempTable.rows[0].cells[-1].contents == "Price"){
                tempTable.rows[0].cells[-1].appl
...
Translate
Participant ,
Oct 29, 2020 Oct 29, 2020

I forgot to mention that the problem I am having with this script so far is that it is applying the cell style to all of the cells and not just ones that I have the text I am searching for. That's not what I want.

Translate
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
Advocate ,
Oct 29, 2020 Oct 29, 2020

Try this snippet for your reference:

if(app.documents.length == 0){
    alert("There are no documents open.");
    exit();
    }
var myDoc = app.activeDocument;
for(var s = 0; s < myDoc.stories.length; s++){
    for(var t = 0; t < myDoc.stories[s].tables.length; t++){
        var tempTable = myDoc.stories[s].tables[t];
        if(tempTable.rows[0].rowType.toString() == "HEADER_ROW"){
            if(tempTable.rows[0].cells[-1].contents == "Price"){
                tempTable.rows[0].cells[-1].appliedCellStyle = "Cell Style 1";
                }
            else if(tempTable.rows[0].cells[-1].contents == "Qty"){
                tempTable.rows[0].cells[-1].appliedCellStyle = "Cell Style 1";
                }
            }
        }
    }

Best

Sunil

Translate
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
Participant ,
Oct 30, 2020 Oct 30, 2020
LATEST

Thank you very much! This helps me out!

Translate
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