Skip to main content
New Participant
January 2, 2025
Answered

How to count selected cells with scripting

  • January 2, 2025
  • 1 reply
  • 345 views

Hi, everyone!

I am a beginner in the scripting world and, as I try to study and understand JavaScript, I analyze existing scripts and work a lot with cutting, pasting, and rewriting things.

Recently I put together a functional script to change the color, weight and style of the inner horizontal lines of selected cells of a table. It worked pretty well, but I would like to check if there is more than one cell selected before run the script and I couldn't find a way to do this...

Whenever I try to capture the number of selected cells, the script always returns the value "1", regardless of the quantity. Is there a way to change this behavior? I haven't been able to find any information about it so far, which is why I'm starting my participation here in the community.  🙂

 

Ps: if I try to change the (selectedCells.length != 0) to (selectedCells.length > 1), it just goes to the second-to-last "else" block.


The code in question is right below.

Thank you in advance for any help!

---

 

(function () {
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS; // Ensures the unit of measurement

function main() {
// Checks if the selection is valid
if (app.selection != 0 && app.selection[0].constructor.name === "Cell") {
var selectedCells = app.selection;

// Checks if the selection covers more than one cell
if (selectedCells.length != 0) {
for (var i = 0; i < selectedCells.length; i++) {
var cell = selectedCells[i];

var appLanguage = app.locale;

// Apply styles to the selection
if (app.activeDocument.swatches.item("Grafite") == null) {
alert("The swatch \"Grafite\" does not exist.");
break;
} else {
if (appLanguage == Locale.PORTUGUESE_LOCALE) {
cell.innerRowStrokeType = app.activeDocument.strokeStyles.item("Pontilhado japonês");
} else if (appLanguage == Locale.ENGLISH_LOCALE) {
cell.innerRowStrokeType = app.activeDocument.strokeStyles.item("Japanese Dots");
} else {
alert("This script does not work in your language.");
break;
}
cell.innerRowStrokeWeight = 1; // The unit of measurement is in pts
cell.innerRowStrokeColor = app.activeDocument.swatches.item("Grafite");
}
}
} else {
alert("Please select at least two vertically adjacent cells.");
}
} else {
alert("Invalid selection. Please select at least two vertically adjacent cells.");
}
}
}());
Correct answer Robert at ID-Tasker

 

 

var mySelectedCells = app.selection[0].cells;

 

Then:

 

for (var i = 0; i < mySelectedCells.length; i++) {
var myCell = mySelectedCells[i];
... 
};

 

 

1 reply

Robert at ID-Tasker
Braniac
January 2, 2025

If type of your 1st / [0] item of the selection is a "Cell" - then you'll get access to the collection of selected cells like this:

 

alert(app.selection[0].cells.length);

 

and then:

alert(app.selection[0].cells[3].contents);

 

FZanAuthor
New Participant
January 2, 2025

Hello, Robert. Thanks a lot for the response!


Actually, I need to make the selection manually because the tables vary too much in size and structure and I just want to know exactly the number of selected cells, not the content itself (just like in the video attached).

 

I need to select what I want to change and run the script, then the lines are changed.

 

When I try to return the selection as

$.writeln(selectedCells.length);

I always get cell = 1, even though I have 6 cells selected (in the example sent). My intention was just to add another error control that would display an alert if only one cell was selected.

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Braniac
January 2, 2025

 

 

var mySelectedCells = app.selection[0].cells;

 

Then:

 

for (var i = 0; i < mySelectedCells.length; i++) {
var myCell = mySelectedCells[i];
... 
};