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

How to count selected cells with scripting

Community Beginner ,
Jan 02, 2025 Jan 02, 2025

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.");
}
}
}());
TOPICS
How to , Scripting
406
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

LEGEND , Jan 02, 2025 Jan 02, 2025

 

 

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

 

Then:

 

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

 

 

Translate
LEGEND ,
Jan 02, 2025 Jan 02, 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);

RobertatIDTasker_0-1735842146025.png

 

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
Community Beginner ,
Jan 02, 2025 Jan 02, 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.

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
LEGEND ,
Jan 02, 2025 Jan 02, 2025

 

 

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

 

Then:

 

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

 

 

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
Community Beginner ,
Jan 03, 2025 Jan 03, 2025

A little obvious, I would say... But it took me quite a while.

 

Now I understand my mistake!

 

Thanks a lot, Robert!

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
LEGEND ,
Jan 03, 2025 Jan 03, 2025
LATEST

You are welcome.

 

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