Sub topic 10
Pulling a CSV from a URL with IMPORTDATA
This chapter assumes you can use Google Sheets formulas, know what a CSV file is, and have read the earlier chapters on separators, locale, and type pitfalls. Those chapters explain why imported text sometimes turns into numbers or dates. IMPORTDATA runs into the same problems, and it adds a few of its own.
What IMPORTDATA does
The File > Import dialog copies a file into your spreadsheet once. IMPORTDATA works differently. It is a formula that fetches a CSV or TSV file from a public URL and fills the cells below and to the right of the formula with the contents. Because it is a formula, Sheets re-evaluates it and refreshes the data without any action from you.
Use IMPORTDATA when:
- The file is published at a stable public URL, such as an open-data portal, a public repository, or a file your own server exposes.
- You want the sheet to reflect the current file, not a snapshot.
- You don't need to edit the imported cells by hand.
Use File > Import instead when the file is on your computer, is an Excel workbook, or needs a one-time snapshot you will edit afterward.
Basic syntax
=IMPORTDATA("https://example.com/data.csv")Put the formula in one cell, usually A1. Sheets writes the first row of the file into the cells to the right of it, and the later rows below. The cell with the formula holds the first value of the file.
The full signature has an optional second and third argument:
=IMPORTDATA(url, [delimiter], [locale])| Argument | Required | Meaning |
|---|---|---|
| url | Yes | The address of the file, in quotation marks, including the protocol (`https://`). |
| delimiter | No | The character that separates columns. If you omit it, Sheets guesses from the file and its extension. |
| locale | No | A locale code such as `de_DE` or `en_US`, which controls how numbers and dates are parsed. |
The url can also be a cell reference. This is useful when you want to change the source without editing the formula:
=IMPORTDATA(B1)Here B1 holds the URL as text.
Setting the delimiter
Many European exports use semicolons because the comma is the decimal mark in those locales. If you import such a file without a delimiter, every row may land in a single column. Pass the delimiter as the second argument:
=IMPORTDATA("https://example.com/data.csv", ";")For a tab-separated file you can pass a tab character with CHAR(9):
=IMPORTDATA("https://example.com/data.tsv", CHAR(9))A delimiter must be a single character. You can't pass a multi-character separator such as ||.
Setting the locale
The third argument controls how Sheets reads numbers and dates in the file. Suppose a file contains 1.234,56 and 31/12/2026. Under a US locale, Sheets reads the first as text, because the dot and comma are in the wrong places. Under a German locale, it reads a number.
=IMPORTDATA("https://example.com/de-report.csv", ";", "de_DE")If you leave the locale out, Sheets uses the spreadsheet's own locale from File > Settings. This is a common source of surprising results. Two people can open the same sheet, and the cells will show the same values only if the sheet's locale is the same for both. The locale is a property of the spreadsheet, not of each viewer.
Refresh behavior
IMPORTDATA is not live in the sense of streaming. Sheets caches the result and refreshes it periodically. Google's documentation describes refresh for the IMPORT functions as roughly every hour, but this is not a guarantee, and you shouldn't rely on a specific interval.
A few consequences:
- If the source file changes, your sheet may show the old data for a while.
- Editing the formula forces a fresh fetch, so you can make a trivial edit to trigger one.
- Changing the URL in a referenced cell also triggers a new fetch.
- A common trick is to append a changing query string, such as
?v=1,?v=2. This only works if the server ignores the extra parameter. It does not force a refresh of the cached result for an unchanged URL by itself.
If you need a guaranteed schedule, use the Apps Script approach in the next chapter and a time-driven trigger.
Requirements for the URL
IMPORTDATA fetches the file from Google's servers, not from your browser. That has several effects.
- The URL must be public. The server must return the file to an anonymous request. A link that needs you to be signed in, such as a private Drive file or an intranet address, will fail.
- The URL must point to the raw file. A link to a web page that displays a CSV, such as a repository's file viewer page, returns HTML, not CSV. On code-hosting sites, use the "raw" link.
- It must be reachable from the internet. Addresses on your local network or
localhostdon't work. - The response must be plain delimited text. A redirect to a login page, a zipped file, or an Excel file won't parse.
For a Google Drive file, you can sometimes use a direct download link of the form https://drive.google.com/uc?export=download&id=FILE_ID, but only if the file is shared so that anyone with the link can view it. Large files may trigger a confirmation page instead of the data. For files in your own Drive, an Apps Script function is the more reliable route.
What the data looks like after import
IMPORTDATA applies automatic type conversion, the same kind you met in the chapter on separators and conversion. You can't switch it off with an option. This leads to the usual pitfalls:
- A column of ZIP codes such as
02134becomes the number2134. - A value like
3-4or1/2may become a date. - Long numeric identifiers may be shown in scientific notation, and digits beyond about 15 can be lost.
- Text with leading or trailing spaces stays as it is, which breaks lookups later.
Because the imported cells are the output of a formula, you can't fix them by typing over them. Typing in a cell that is part of the output range causes the formula to fail. Instead, leave the IMPORTDATA range alone and build a cleaned copy next to it or on another sheet.
To restore a five-digit code whose leading zeros were dropped:
=TEXT(A2, "00000")To remove stray spaces from a whole column of imported text so that comparisons match:
=ARRAYFORMULA(TRIM(A2:A))A tidy pattern is to keep the raw import on a sheet named raw, and to build the working table on a second sheet that refers to raw. If the source file changes, only the raw sheet is replaced, and your formulas continue to read from it.
Note that the number of columns in the source can change. If you refer to a fixed range such as raw!A2:F, a new seventh column will be missed. Check the headers occasionally, or compare the count with COUNTA(raw!1:1).
Combining IMPORTDATA with other functions
Since the result is an array, you can wrap it in other functions. This lets you reshape the data without a helper sheet.
Keep only the first three columns:
=CHOOSECOLS(IMPORTDATA("https://example.com/data.csv"), 1, 2, 3)Keep the rows where the third column is greater than 100, and skip the header row:
=QUERY(IMPORTDATA("https://example.com/data.csv"), "select Col1, Col3 where Col3 > 100", 0)In a QUERY over an array, columns are named Col1, Col2, and so on. The final 0 tells QUERY that there are no header rows. If the types in a column are mixed, QUERY keeps only the values of the dominant type and treats the others as empty. For that reason, check the column types before filtering on them.
You can also sort, as in =SORT(IMPORTDATA(...), 2, FALSE), or take a single value with INDEX. Each wrapped call still fetches the file, so avoid repeating the same IMPORTDATA call many times. Fetch it once into a raw sheet and refer to that sheet.
Errors and how to read them
| Message | Likely cause | What to try |
|---|---|---|
| `#N/A` with "Could not fetch url" | The URL is wrong, private, blocked, or the server was down. | Open the URL in a private browser window. If it asks for a login, it is not public. |
| `#VALUE!` with "Resource at url not found" | The server answered with a not-found response. | Check the spelling and the path. |
| `#REF!` with "Array result was not expanded because it would overwrite data" | Cells in the way of the output are not empty. | Clear the cells below and to the right of the formula, or move the formula. |
| Everything in column A | Wrong delimiter. | Pass the delimiter as the second argument. |
| Garbled characters such as `é` | The file's encoding is not what Sheets assumed. | Check the encoding in the source. The earlier chapter on encoding and locale describes the fix. |
| `#ERROR!` or "Imported content is empty" | The response is empty, or a login page or HTML came back. | Confirm that the URL returns raw delimited text. |
| Error mentioning a limit on import functions | The spreadsheet exceeds the quota for external fetches. | Reduce the number of IMPORT calls, or move the work into Apps Script. |
The exact wording of messages can differ between interface languages and over time, so match on the cause, not the string.
When the URL is right but the import still fails, test it in a browser first. The response should show plain rows of text with no HTML tags. If your browser downloads a file instead of displaying it, that is fine, and open the downloaded file in a text editor to check the delimiter and the header row.
Limits
IMPORTDATA shares limits with other Sheets import functions:
- A spreadsheet can hold at most 10 million cells in total across all sheets, so a very large CSV can fill it by itself.
- Very large files, in the order of tens of megabytes, may time out or fail to load even when the cell limit isn't reached.
- Too many IMPORT-family formulas in one spreadsheet can make it slow, and you may see a message about the number of external-data functions.
- The fetch has a time limit, so a slow server can cause intermittent failures.
If the file is large or the source is slow, use File > Import for a one-time load, or Apps Script for control over timing and error handling.
Choosing between methods
| Situation | Best method |
|---|---|
| One-time load of a file on your computer | File > Import |
| An Excel workbook with several sheets | Open or import the workbook, then convert |
| A public CSV that updates and should be reflected | IMPORTDATA |
| A private CSV in your Drive, or a schedule you control | Apps Script |
Worked example
Suppose a city publishes a monthly CSV of permits at https://example.com/permits.csv. The file uses semicolons, and its columns are permit_id, zip, issued, and fee. The fee column uses a decimal comma.
- Create a sheet named
raw. Inraw!A1, enter:
=IMPORTDATA("https://example.com/permits.csv", ";", "de_DE")- The locale
de_DEmakes Sheets read1.250,00as the number 1250. The issued dates are read as dates, if they use the day-first order. - On a second sheet named
clean, restore the ZIP codes in column B. Assuming the headers are in row 1:
=ARRAYFORMULA(IF(raw!B2:B="", "", TEXT(raw!B2:B, "00000")))- Check the import. Compare the number of rows in the file with
=COUNTA(raw!A2:A). Use=COUNTBLANK(raw!D2:D)to find fees that did not parse. Use=COUNTIF(raw!D2:D, "<>")-COUNT(raw!D2:D)to count cells in the fee column that are text, which signals a failed number conversion.
The result is a snapshot that refreshes on its own, and the raw data stays untouched.
Exercise
A colleague puts =IMPORTDATA("https://example.com/sales.csv") in A1. The file uses semicolons, and all the data appears in column A. They fix it, but now the number 1.234,50 shows as text. What two changes to the formula fix both problems, and why can't they just type corrected numbers into the cells?
Show answer
Pass the semicolon as the second argument and a locale that uses a decimal comma as the third, for example =IMPORTDATA("https://example.com/sales.csv", ";", "de_DE"). The delimiter splits the columns correctly, and the locale makes Sheets read 1.234,50 as the number 1234.5. They can't type over the output cells because those cells are the spilled result of the formula. Editing one blocks the array, and the formula returns a "would overwrite data" error. Any correction should go in a separate cleaned range that refers to the import.
Exercise
IMPORTDATA returns Could not fetch url for a link to a CSV in a company's private Drive folder. Give two reasons it fails and one other approach.
Show answer
The file is not public, so Google's servers can't fetch it anonymously. A Drive link may also return a sign-in or confirmation page, which is HTML and not CSV. Instead, use an Apps Script function that reads the file with DriveApp.getFileById, which runs with your own permissions, or download the file and use File > Import.