How to use IMPORTHTML in Google Sheets (and what to do when it comes back empty)

2026-12-01来源 dev.toEnglish

IMPORTHTML is the quickest way to get a table from a web page into Google Sheets. One formula, no add-ons, and the sheet keeps itself up to date. When it works, it is great. When it fails, the error messages do not tell you much.

This tutorial walks through one real example from start to finish, shows how to keep only the columns you want, and then covers what to do when the formula returns an error or nothing at all.

What you need

  • A Google account and a blank Google Sheet.
  • The address of a public web page with a table on it.

We will use the Wikipedia page "List of countries and dependencies by population":

https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population 

Step 1: write the formula

Click cell A1 and type:

=IMPORTHTML("https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population", "table", 1) 

Press Enter. The first time you use an import formula in a spreadsheet, Sheets shows a yellow bar that says "Some formulas are trying to send and receive data from external parties", and the cell shows #REF!. Click Allow access in that bar. Only an editor of the sheet can do this, and on a phone you need a desktop browser.

After a second or two you get the whole table: Location, Population, % of world, Date, Source and Notes. The first row under the header is the World total, then one country or territory per row.

The three parts:

Part What it means
"https://..." The page address, in quotes, including https://. It can also be a cell reference, like A1.
"table" What to look for. Use "table" for tables and "list" for bulleted or numbered lists.
1 Which one. 1 is the first table in the page's HTML, 2 the second, and so on.

Tables and lists are counted separately. So "table", 1 and "list", 1 can both exist on the same page and be two different things.

Step 2: find the right index

On this page the population table is table 1. On many pages it is not. Navigation boxes, sidebars and hidden layout tables all count, even when you cannot see them.

The quick way: try 1, then 2, then 3, until the right data appears. The careful way: right click the page, choose View page source, press Ctrl+F (Cmd+F on a Mac) and search for <table. Each match is one index, in order. Use the page source, not Inspect: Inspect also shows tables that scripts add after the page loads, and IMPORTHTML never sees those.

Step 3: keep only the columns you need

IMPORTHTML always returns the whole table. Wrap it in another function to trim it.

Only the second column (Population):

=INDEX(IMPORTHTML("https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population", "table", 1), 0, 2) 

0 means "all rows", 2 means "column 2".

Columns 1 and 2, with the header kept as a header:

=QUERY(IMPORTHTML("https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population", "table", 1), "select Col1, Col2", 1) 

Inside QUERY, imported columns are called Col1, Col2 and so on. The last 1 tells QUERY the first row is a header.

To skip the first 10 rows, add offset 10 to the end of the query text, as in "select Col1, Col2 offset 10". To keep the top 20, add limit 20. If you use both, write limit before offset.

Step 4: decide if it should update

The formula is live. Google's help says import functions like IMPORTHTML recalculate every hour. That is useful for prices or rankings you want to watch.

If you want a fixed copy instead, select the range, copy it, then use Edit > Paste special > Values only. The data stays, the formula is gone, and nothing changes later.

A small clean-up tip: Wikipedia tables often bring footnote markers like [4] or [b] into the cells. Find and replace changes cell contents, not what a formula returns, so do this on the pasted values copy. Use Edit > Find and replace, tick Search using regular expressions, find \[\w+\] and replace with nothing.

When IMPORTHTML fails

The key fact: the page is fetched by Google's servers, not by your browser. The formula only sees the HTML that comes back. It does not log in as you, and it does not run the page's scripts.

So the table you see and the table IMPORTHTML sees can be two different things.

What you see Usual cause What to try
"Imported content is empty" The table is built by JavaScript after the page loads, so it is not in the HTML Check the page source (below). If the data is not there, use one of the browser options further down
"Could not fetch url" The site blocks automated requests, or the page needs a login Open the address in a private window. If it asks you to sign in, IMPORTHTML cannot reach it
The wrong table, or a list of menu links The index points at a different table or list Count up the index, and check you used "table" or "list" correctly
"Imported content does not have query with given index" The index is higher than the number of tables (or lists) in the HTML Lower the index. Count the <table matches in View page source
Data looks like cards or tiles The page uses a grid of boxes, not real table tags IMPORTHTML only reads real tables and lists
Only the first page of results Each results page is a separate address One formula per page, or a browser option below

The one test worth knowing: right click the page, choose View page source, and press Ctrl+F (Cmd+F) to search for a value you can see in the table. If it is not in the source, IMPORTHTML will never find it, whatever index you try.

IMPORTXML will not save you here either. It can pick out single elements with an XPath, but it reads the same HTML source, so it fails on the same pages.

Getting the data anyway

When the table only exists in your browser, read it from your browser.

Option 1: copy and paste

Select the table on the page with your mouse, copy, and paste into cell A1. For a simple table the columns usually land in the right places. It breaks on long tables that load as you scroll and on card layouts.

Option 2: one line in the browser console

This works in Chrome, Edge and Firefox, on any page you can see, including pages you are signed in to.

  1. Open the page and wait for the table to appear.
  2. Right click the table and choose Inspect. This opens the developer tools.
  3. Open the Console tab and paste this line, then press Enter:
copy([...document.querySelectorAll("table")[0].rows].map(r => [...r.cells].map(c => c.innerText.trim().replace(/\s+/g, " ")).join("\t")).join("\n")) 
  1. Click cell A1 in your sheet and paste. Each column lands in its own Sheets column, because the line copies the table as tab-separated text.

[0] means the first table on the page. Change it to [1], [2] and so on for the others, the same idea as the IMPORTHTML index. If the browser warns you about pasting into the console, read the line first, then follow its prompt to allow pasting. Only paste code you understand.

The catch: this only reads real <table> tags, and only what has loaded so far. It also does not refresh like IMPORTHTML does.

Option 3: a browser extension

For card layouts, lists that are not tables, and data spread over several pages, a scraper extension saves the fiddling. Search the Chrome Web Store or Firefox Add-ons for "table scraper" and pick one that shows a preview before you export and keeps the data in your browser. You open the page, click its button, check the preview, and copy the rows into Sheets or download a CSV file. Like the console line, it reads the page in your browser, so it does not refresh by itself.

Which one should you use?

Situation Use
A plain table that shows up in View page source IMPORTHTML
You want the sheet to update on its own IMPORTHTML (if the data is in the source)
"Imported content is empty" and it is a real table The console line
The page needs a login Copy and paste, or the console line
Cards, product grids, or results over several pages A scraper extension

Whatever you use, check the site's terms of use before you collect its data, and keep requests to a sensible number.

This article was created with the help of AI. Every formula in it was tested in a live Google Sheet on 1 Dec 2026.

原文:https://www.a8q9.cn/en/article/how-to-use-importhtml-in-google-sheets-and-what-to-do-when-it-comes-back-empty-0e1f38.html

相关文章