If you want to use the logic that you were following then you need to twek your for loop condition. The problem is that you are starting the loop with -1 and decreasing it so the condition i < a.cells.length will always be true as length will be positive and i will always be negative. So at one point when you outrun the number of cells it would result in a crash due to invalid cell object, if it would not crash you would end in a endless loop. Try the following
var a = app.selection[0]
var startIdx = 1 //Starting Number of Counting
if(a instanceof Cell && a.columnSpan == 1)
{
for(var i = -1; Math.abs(i) <= a.cells.length; i--)
{
if(a.cells[i].parentRow.rowType == RowTypes.BODY_ROW);
a.cells[i].contents = (startIdx++).toString()
}
}
-Manan
Thank alot @Manan Joshi , I also tried this and its Working also making startIdx = 0 and minus increasing from cells.length :
//Descender Version to Count Selected Column
//User must select the Column need to Count
var a = app.selection[0]
var startIdx = 0 //Starting Number of Counting
if(a instanceof Cell && a.columnSpan == 1)
{
for(var i = 0; i < a.cells.length; i++)
{
if(a.cells[i].parentRow.rowType == RowTypes.BODY_ROW);
a.cells[i].contents = (a.cells.length-startIdx++).toString()
}
}