Trying to change color of all tables in a document
New to JavaScript and scripting, and just wanted some more clarity on how everyItem() method works. I have this code:
var document = app.activeDocument;
var swatches = document.swatches;
var allitems = document.stories.everyItem()
var alltables = allitems.tables.everyItem()
var allcells = alltables.cells;
var currentcell = allcells.firstItem()
var cell;
while (true) {
cell = currentcell;
switch (cell.contents) {
case "Achiever":
case "Consistency":
case "Focus":
case "Arranger":
case "Deliberative":
case "Responsibility":
case "Belief":
case "Discipline":
case "Restorative":
cell.fillColor = swatches.itemByName("Strengths Purple");
break;
}
}
if (cell === allcells.lastItem()) {
break;
}
currentcell = allcells.nextItem(cell);
}
This works, but when it finds an item that matched what I wanted, it changed the matching cell number in all tables in the document. Upon some digging in ES ToolKit, I found that for some reason the cell objects produced by allcells.nextItem() actually seem to be multiple cell objects? i.e. currentcell.contents returns an array of about 8 items -- the first row of every table in the document. On the next iteration it contains the second row of every table in the document.
I was wondering how to further separate this out so I can iterate over every cell individually. Do I need to call everyItem() once more on currentcell?

(I also tried a version with a for loop that iterates over the seemingly multiple cells in each cell object, but this version would not assign fill color properly.)
var document = app.activeDocument;
var swatches = document.swatches;
var allitems = document.stories.everyItem()
var alltables = allitems.tables.everyItem()
var allcells = alltables.cells;
var currentcell = allcells.firstItem()
var cell;
while (true) {
cell = currentcell;
for (var i = 0; i < cell.contents.length; i++) {
switch (cell.contents[i]) {
case "Achiever":
case "Consistency":
case "Focus":
case "Arranger":
case "Deliberative":
case "Responsibility":
case "Belief":
case "Discipline":
case "Restorative":
cell.fillColor[i] = swatches.itemByName("Strengths Purple"); //This simply doesn't work
break;
}
}
if (cell === allcells.lastItem()) {
break;
}
currentcell = allcells.nextItem(cell);
}
Thank you! And I know there might be a better way to do this with findWhat, but I'm interested in understanding why this particular problem occurs.