Total Rows and Columns Count - Excel Office JS - excel

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)

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

Officescript how to set value in table cell (row/column) by row index

pretty simple question but I can't seem to find what I am looking for and wondering if it is possible this way, just starting using Officescript/typescript. In a part of my code, I get the index of the row with a value that matches (cRow is the index of row I am interested in).
rowValue = collectionTable.getColumnByName("SomeCol").getRangeBetweenHeaderAndTotal().getValues()[cRow]
And then I run some checks on that row and want to update some other things based on the inputs.
So what I am expecting to do is something like the following, changing getValues for setValues:
collectionTable.getColumnByName("UpdateMe").getRangeBetweenHeaderAndTotal().setValues()[cRow]
OR
let col = collectionTable.getColumnByName("SomeCol").getIndex();
let cell = collectionTable.getCell(requestRow,col);
cell.setValue(value);
But doesn't seem to work that way .. From what I can tell, setValues works on ranges but can't quite find how to get the range/row by index number and set a value in one cell. I see all examples doing it with the letter and number but don't want to do it that way if possible.
Thanks for the help!
You can use getCell with the cRow variable after getRangeBetweenHeaderAndTotal() to get a specific cell range. Once you have that range, you can read its value using getValue(). And you can write a value to that range using setValue(). You can see an example of how to do that below:
function main(workbook: ExcelScript.Workbook)
{
let cRow: number = 1;
let tbl: ExcelScript.Table = workbook.getTable("table1");
let rowCell: ExcelScript.Range = tbl.getColumnByName("SomeCol").getRangeBetweenHeaderAndTotal().getCell(cRow,0);
let rowValue: string = rowCell.getValue() as string;
rowCell.setValue("some updated value");
}
Can get the value by using the worksheet cell, but I would really like to reference the table itself to not have any issues if things are moved around and such..
let col = collectionTable.getColumnByName("SomeCol").getIndex();
// if table starts at beginning of sheet, or else have to offset col and row..
let cell = dataSheet.getCell(requestRow + 1, col);
cell.setValue(value);

Node.js xlsx get number of non-empty rows

I use xlsx in Node.js application to get the number of total rows. Here is the code
const range = xlsx.utils.decode_range(sheet['!ref']);
const totalRows = (range.e.r - range.s.r) + 1;
The problem is it also counts formatted cells with empty text. I only want to get number of rows with non-empty text. How can I do it using xlsx or is there any other library that can count number of rows containing non-empty text?
I know the thread is old but if anyone still looking for an answer, you can use the below code to ignore formatted blank cells:
var arr = xlsx.utils.sheet_to_row_object_array(sheet,{blankrows : false, defval: ''});
const totalRows = arr.length+1;

In Google Spreadsheets, if cell A > cell B, replace Cell B with Cell A

I currently have a column labeled "Current Rank" in column A, and a column labeled "Highest Rank" in column B. If current rank > highest rank, I'd like to replace highest rank with current rank. Is there any way to do this while getting around the self-reference errors?
You can list all the scores and use the formula =max() to find the biggest one, but to actually replace the cell "B" with the bigger value, the only way is with a script, Google's version of Excel macros. You'd have to make something like this, and run it after you put in the new values (this is my first google script, there is most certainly cleaner way, but this code gets the job done)
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range=sheet.getRange("A1:A100")
var j=0;
for(var i in range){
if(range.getValues()[j]!=undefined){
if(sheet.getRange("B" + (j+1)).getValue()<range.getValues()[j][0]){
sheet.getRange("B" + (j+1)).setValue(range.getValues()[j][0]);
}
}
j++;
}
}
To make a script, open tools>script editor.
You need a script, but this is not a hard one.
An onEdit trigger is a little cleaner, works automatically. Open the editor via tools, paste this into an empty file. You're done. This script will evaluate column a and column b every time a change is made on the sheet.
Enter data into column A, and the max result will appear in column B (this script assumes there is a header row, data on row 1 won't get evaluated.)
function onEdit(e) {
var ss = e.source;
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
for(var i = 1; i < values.length; i++) {
if (values[i][0] > values[i][1]) {
sheet.getRange(i + 1, 2, 1, 1).setValue(values[i][0]);
}
}
}

Read from a specific row onwards from Excel File

I have got a Excel file having around 7000 rows approx to read. And Excel file contains Table of Contents and the actual contents data in details below.
I would like to avoid all rows for Table of Content and start from actual content data to read. This is because if I need to read data for "CPU_INFO" the loop and search string occurrence twice 1] from Table of Content and 2] from actual Content.
So I would like to know if there is any way I can point to Start Row Index to start reading data content for Excel File , thus skipping whole of Table Of Content Section?
As taken from the Apache POI documentation on iterating over rows and cells:
In some cases, when iterating, you need full control over how missing or blank rows or cells are treated, and you need to ensure you visit every cell and not just those defined in the file. (The CellIterator will only return the cells defined in the file, which is largely those with values or stylings, but it depends on Excel).
In cases such as these, you should fetch the first and last column information for a row, then call getCell(int, MissingCellPolicy) to fetch the cell. Use a MissingCellPolicy to control how blank or null cells are handled.
If we take the example code from that documentation, and tweak it for your requirement to start on row 7000, and assuming you want to not go past 15k rows, we get:
// Decide which rows to process
int rowStart = Math.min(7000, sheet.getFirstRowNum());
int rowEnd = Math.max(1500, sheet.getLastRowNum());
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
} else {
// Do something useful with the cell's contents
}
}
}

Matlab number of rows in excel file

is there a command of Matlab to get the number of the written rows in excel file?
firstly, I fill the first row. and then I want to add another rows in the excel file.
so this is my excel file:
I tried:
e = actxserver ('Excel.Application');
filename = fullfile(pwd,'example2.xlsx');
ewb = e.Workbooks.Open(filename);
esh = ewb.ActiveSheet;
sheetObj = e.Worksheets.get('Item', 'Sheet1');
num_rows = sheetObj.Range('A1').End('xlDown').Row
But num_rows = 1048576, instead of 1.
please help, thank you!
If the file is empty, or contains data in only one row, then .End('xlDown').Row; will move to the very bottom of the sheet (1048576 is the number of rows in a Excel 2007+ sheet).
Test if cell A2 is empty first, and return 0 if it is.
Or use Up from the bottom of the sheet
num_rows = sheetObj.Cells(sheetObj.Rows.Count, 1).End('xlUp').Row
Note: I'm not sure of the Matlab syntax, so this may need some adjusting
You can use MATLAB's xlsread function to read in the spreadsheet. This obtains the following fields:
[numbers strings misc] = xlsread('myfile.xlsx');
if you do a size check on strings or misc, this should give you the following:
[rows columns] = size(strings);
testing this, I got rows = 1, columns = 10 (assuming nothing else was beyond 'A' in the spreadsheet).

Resources