Home > other >  Total Rows and Columns Count - Excel Office JS
Total Rows and Columns Count - Excel Office JS

Time:03-21

I've been using getRangeByIndexes as it seems to be the best way to use numbers to get Ranges in Office-JS. Problem I'm having is that when I need to use entire row/columns, I've resorted to the below. I read up and looks like number of rows/cols hasn't changed in a long time, but I know in vba I used rows.count to make sure the code was dynamic, whatever version of Excel it would use the number of rows on a spreadsheet.

Is there anything similar in Office-JS?

const Excel_Worksheet_Lengths_Obj = {
    "total_rows": 1048576,
    "total_cols": 16384,
}

var ws = context.workbook.worksheets.getActiveWorksheet()

var Method_Headers_Rng = ws.getRangeByIndexes(0,0,1,Excel_Worksheet_Lengths_Obj.total_cols)

CodePudding user response:

This will do it for you ...

const sheet = context.workbook.worksheets.getActiveWorksheet();

let rangeRows = sheet.getRange("A:A").load(["rowCount"]);
let rangeColumns = sheet.getRange("1:1").load(["columnCount"]);

await context.sync();

console.log("Row Count = "   rangeRows.rowCount);
console.log("Column Count = "   rangeColumns.columnCount);

Everything relating to the number of rows or columns looks to be done at the range level, not the worksheet level.

Passing in the first column to achieve the number of rows and the first row to achieve the number of columns does the trick. Not ideal but such is life.

  • Related