Count Rows in an Excel Tab-Delimited File With JavaScript
Question details
Count rows in a tab-delimited file exported from Excel using JavaScript.

- Product
- Microsoft Excel and JavaScript
- Device & OS
- not provided
- Scenario
- A user wants to process an Excel-created tab-delimited text file and determine its row count.
- Observed behavior
- The desired result is a count of the file's nonempty records from a JavaScript environment.
Locate the exported tab-delimited file and decide whether to run JavaScript in Node.js or a browser. Check whether the first row is a header and whether any fields contain embedded line breaks.
Count nonempty lines with Node.js
Use this method to count nonempty physical lines in a local tab-delimited file.
A text file cannot execute JavaScript by itself. The script must read the file in an environment such as Node.js.
This method treats each nonempty physical line as one row. If quoted fields contain embedded line breaks, use a TSV parser that supports quoted multiline fields instead.
Create a file named count.js and add: const fs = require('fs'); const text = fs.readFileSync(process.argv[2], 'utf8'); const rows = text.split(/\r\n|\n|\r/).filter(line => line.trim().length > 0); console.log(rows.length);
In a terminal with Node.js installed, run node count.js "path/to/file.txt", replacing the path with your tab-delimited file. The terminal prints the number of nonempty lines.
If the first nonempty line is a header and you only want data rows, change the console.log line to console.log(Math.max(0, rows.length - 1)); and run the command again.

Count nonempty lines in a browser
Use a local web page when you prefer to select the file without installing Node.js.
Review Excel Data With WPS Spreadsheet
WPS Office is a free, lightweight alternative for reviewing spreadsheet data in a familiar interface. It supports common Microsoft Excel formats to make migration easier, though a separate JavaScript environment is still needed to run the row-counting script.

Frequently Asked Questions
Can a tab-delimited text file run JavaScript on its own?
No. Open the file from a JavaScript environment, such as a Node.js script or a browser page, and read its contents before counting rows.
Does the count include the header row?
The examples count the header if it is nonempty. In the Node.js example, use Math.max(0, rows.length - 1) to exclude the first nonempty line.
Why might splitting on line breaks give the wrong record count?
A quoted field can contain an embedded line break, so one logical record may span multiple physical lines. Use a TSV parser that supports quoted multiline fields for those files.
Are empty rows included in the examples?
No. The filter using line.trim().length excludes lines containing only whitespace, including tabs. Adjust the filter if your row-counting rules differ.




