Monday, August 29, 2005

 

Another Script -- Select Rest of Column

I'm processing the results of this morning's script and it turns out that I need to be able to extend a selection in a table from the current cell to the last populated cell in the same column. This script does just that (although be warned that I have completely glossed over the issue of merged cells which will almost certainly cause this script to malfunction if it encounters any):
//DESCRIPTION: Select to last used cell in column

var myCell = app.selection[0];
var myTable = myCell.parent;
if (myTable.constructor.name == "Cell") {
 // Text is selected in a cell, so:
 myCell = myCell.parent;
 myTable = myTable.parent;
}
if (myTable.constructor.name != "Table") {
 errorExit("Please select a cell and try again.");
}
var myName = myCell.name;
var myRow = myName.split(":")[1];
var myCol = myName.split(":")[0];
// Find row reference of last populated cell in column
var myLim = myTable.rows.length;
theLast = -1;
for (var j=myLim - 1; j >= 0; j--) {
 if (myTable.cells.item(String(myCol) + ":" + String(j))) {
  theLast = j;
  break;
 }
}
app.select(myTable.columns[Number(myCol)].cells.itemByRange(Number(myRow),theLast));

function errorExit(message) {
 if (app.version != 3) { beep() } // CS2 includes beep() function.
 if (arguments.length > 0) {
  alert(message);
 }
 exit(); // CS exits with a beep; CS2 exits silently.
}
No time for much discussion right now. Notice the use of itemByRange to address the sequence of cells in the column of interest. Notice also that cell names take the form c:r where "c" stands for the column number and "r" for the row number.

Comments:
Dave! Glad to see you here. I've seen your face (well actually just your moniker,) on the Adobe forums for a long time and have always appreciated your comments.

I'll be looking you up regulary, but if you want to have a laugh, here's my blog ÜberDilf
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?