If you have ever pasted an HTML table with merged headers (colspan) or grouped category rows (rowspan) into a spreadsheet, you know what happens: columns shift left, headers collapse, and half the numeric rows end up under the wrong label.

When we built Table Grabber (a free, MIT-licensed Chrome Manifest V3 extension that runs 100% locally with only activeTab and scripting permissions), we wanted the exact same parser to run both in the browser popup and inside Node.js unit tests — with zero npm runtime dependencies and no jsdom.

Here are the four small engineering decisions that make HTML table extraction predictable in ~499 lines of vanilla JS.

1. Expand colspan and rowspan Into a Rectangular Grid

Before serializing to CSV, Markdown, or JSON, every table should become a strict rows × width string grid. The cleanest way to handle overlapping rowspan cells without lookahead passes is a pending[col] cursor that tracks how many rows remain for each spanned column:

function normalizeGrid(rows) { var grid = []; var headerFlags = []; var pending = []; // pending[col] = { text, header, left } rows.forEach(function (cells) { var out = []; var flags = []; var col = 0; function fillPending() { while (pending[col] && pending[col].left > 0) { out[col] = pending[col].text; flags[col] = pending[col].header; pending[col].left--; col++; } } fillPending(); for (var ci = 0; ci < cells.length; ci++) { var c = cells[ci]; fillPending(); for (var k = 0; k < c.colspan; k++) { out[col] = c.text; flags[col] = !!c.header; if (c.rowspan > 1) { pending[col] = { text: c.text, header: !!c.header, left: c.rowspan - 1 }; } col++; } } fillPending(); grid.push(out); headerFlags.push(flags); }); var width = grid.reduce(function (w, row) { return Math.max(w, row.length); }, 0); grid = grid.map(function (row) { var full = []; for (var i = 0; i < width; i++) full.push(row[i] == null ? '' : row[i]); return full; }); return { grid: grid, headerFlags: headerFlags, width: width }; } 

Why repeat the spanned text instead of leaving blanks? Because downstream CSV filters, pivot tables, and JSON object keys expect every row to carry its group value. We also cap colspan/rowspan at MAX_SPAN = 1000 inside attrInt() so malformed markup like rowspan="999999" cannot hang the tab.

2. Flatten Multi-Row <thead> Headers Without Duplicate Labels

Financial and SaaS pricing tables frequently stack two header rows (Q1 2027 spanning Revenue and Margin). Once normalizeGrid() expands the spans, combining multi-row headers into unique flat column names takes ten lines:

function toTableModel(caption, grid, headerRowCount) { var header = null; var body = grid; if (headerRowCount > 0 && grid.length > 0) { var hdrRows = grid.slice(0, headerRowCount); body = grid.slice(headerRowCount); header = hdrRows[0].map(function (_, col) { var parts = []; hdrRows.forEach(function (row) { var v = row[col]; if (v && parts[parts.length - 1] !== v) parts.push(v); }); return parts.join(' / '); }); } return { caption: caption || '', header: header, rows: body }; } 

That turns a 2-row grouped header into Q1 2027 / Revenue and Q1 2027 / Margin, while deduplicating vertically spanned headers (Metric spanning 2 rows stays Metric, not Metric / Metric).

3. Excel Formula-Injection Guard & Safe JSON Type Coercion

Two subtle bugs bite teams who pipe scraped web tables into Excel or JSON APIs:

  1. CSV formula injection: A scraped cell starting with =, +, -, or @ can execute as a spreadsheet formula when opened in Excel — yet negative numbers like -42.5% should stay numeric!
  2. 64-bit ID corruption: Naive Number(v) coercion corrupts 16+ digit Snowflake/order IDs into IEEE-754 approximations.

We guard both directly inside the serializers:

function csvField(value, delimiter, excelSafe) { var s = value == null ? '' : String(value); if ( excelSafe && /^[=+\-@\t\r]/.test(s) && !/^[-+]?\d+(?:[.,]\d+)*(?:e[-+]?\d+)?%?$/i.test(s) ) { s = "'" + s; } var needsQuote = s.indexOf(delimiter) !== -1 || /["\r\n]/.test(s) || /^\s|\s$/.test(s); return needsQuote ? '"' + s.replace(/"/g, '""') + '"' : s; } function coerce(v) { if (/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:e[-+]?\d+)?$/i.test(v) && v.length < 16) return Number(v); if (v === 'true') return true; if (v === 'false') return false; return v; } 

4. Zero Host-Permission MV3 Page Scanning (activeTab + scripting)

Instead of requesting <all_urls> or running a background content script on every tab, manifest.json requests only ["activeTab", "scripting"] and injects a self-contained scanPage() function on demand via chrome.scripting.executeScript({ func }). It skips hidden tables (el.getClientRects().length > 0), caps payload size (MAX_ITEMS = 150, MAX_HTML = 2_000_000), and infers a readable label from <caption>, aria-label, or the nearest preceding <h1>–<h6>.

Try It Live or Grab the Full Toolkits

  • Free Interactive Demo & Chrome Extension (MIT): Table Grabber Live Playground · GitHub Repo
  • Need offline CSV / JSON / YAML / Markdown / SQL INSERT / Base64 conversion in one zero-dependency PWA? Grab the Data Swiss-Army Converter Pro ($9 → $5.40 with code CYBER40 through Dec 2) — full source, offline PWA, and unit tests.
  • Automating extracted lead/ops tables inside Claude Desktop or Cursor? Check out the MCP Automation Starter Kit ($49 → $29.40 with code CYBER40 through Dec 2) — zero-dependency local Node.js MCP server.
  • Browse all free tools & kits at QuietStack Labs.