Others

Count Rows in an Excel Tab-Delimited File With JavaScript

Steven ParkerSteven Parker Nov 24, 2026 868 views

Question details

Count rows in a tab-delimited file exported from Excel using JavaScript.

Count Rows in an Excel Tab-Delimited File With 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.
Before you start

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.

Solution 1Recommended

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.

1
Create a script

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);

2
Run the script

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.

3
Exclude the header if needed

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 with Node.js
Blank rows: Lines containing only spaces or tabs are excluded. Remove the .filter(...) call if your definition of a row requires a different treatment of blank lines, and account for any trailing line break.
Free Microsoft Office alternative

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.

Review spreadsheet rows and headers before exporting or processing data.Open and edit Microsoft Excel XLS and XLSX files.Use a familiar spreadsheet interface for an easier transition from Microsoft Office.Work with a free, lightweight office suite.
microsoft office alternative - wps office

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.