Sub topic 13
Pulling Data from Another Spreadsheet with IMPORTRANGE
This chapter assumes you can write formulas that start with =, read A1 notation, use ranges such as B2:B10, and refer to cells on other sheets inside the same spreadsheet. If any of that is unfamiliar, review the earlier chapters on ranges and cross-sheet references first.
Why a separate function is needed
A reference like ='Sales Data'!B2 only works inside one spreadsheet file. A spreadsheet is the whole file, and a sheet is one tab inside it. You cannot write a sheet reference that points at a different file. IMPORTRANGE fills that gap. It reads a range from another spreadsheet and shows the values in the spreadsheet where you typed the formula.
Common uses:
- A team keeps raw data in one file, and managers build reports in another file.
- Several people maintain separate files, and one summary file collects them.
- You want to share only part of a data set. You share the report file and keep the source file private.
The syntax
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID","Sheet1!A1:C10")IMPORTRANGE takes two arguments, and both must be text in double quotes (or references to cells that contain such text).
| Argument | What it holds | Example |
|---|---|---|
| `spreadsheet_url` | The full URL of the source spreadsheet, or only its ID | `"https://docs.google.com/spreadsheets/d/SPREADSHEET_ID"` |
| `range_string` | The sheet name and range, joined by `!` | `"Sheet1!A1:C10"` |
SPREADSHEET_ID is a placeholder. The real ID is the long string of letters, digits, hyphens, and underscores between /d/ and the next / in the source file's address bar.
The formula above imports the block A1:C10 from the tab named Sheet1. The result spills into the cells to the right of and below the cell holding the formula, just as other array results do. The top left value lands in the formula cell.
Writing the range string
The range string follows these rules:
- Write the sheet name, then
!, then the range:"Sheet1!A1:C10". - If you leave out the sheet name, for example
"A1:C10", Sheets reads from the first tab of the source file. Tabs can be reordered, so naming the sheet is safer. - If the sheet name contains a space, you can write it without extra quotes inside the string:
"Sales Data!A1:C10". This differs from a same-file reference, where single quotes are required. Adding single quotes inside the string also works. - Open-ended ranges work:
"Sheet1!A2:C"imports everything from row 2 down in columns A to C, so rows added later are included. - A whole column such as
"Sheet1!B:B"also works.
Granting access the first time
The first time you connect two spreadsheets, the formula cell shows #REF!. When you click the cell, a message says the sheets need to be connected, with an Allow access button. Click it. The data loads at once.
Things to know about this step:
- You need at least view access to the source spreadsheet. If you do not have it, the error message says you lack permission, and clicking Allow access does nothing useful.
- Access is granted per pair of files. After you allow it once, other
IMPORTRANGEformulas from the same destination file to the same source file work without another prompt. - The permission belongs to the person who clicked Allow. When someone else opens the destination file, their own access to the source file is checked. People who cannot open the source file may see an error even though the formula looks correct. Share the source file with everyone who needs to see the imported data.
A worked example
Suppose a source spreadsheet has a tab named Orders with these columns:
| Row | A | B | C |
|---|---|---|---|
| 1 | Order | Quantity | Unit price |
| 2 | 1001 | 4 | 12.50 |
| 3 | 1002 | 2 | 30.00 |
| 4 | 1003 | 10 | 5.25 |
In a new report spreadsheet, type this in cell A1:
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID","Orders!A1:C")After you allow access, the four rows fill A1:C4 of the report. Because the range ends with C and not C4, a fifth order added to the source later appears in the report automatically.
The imported cells are read-only. You cannot type over one of them. You can still point other formulas at them, for example:
=SUM(B2:B)This sums the imported quantities if the import sits in A1:C of the report sheet. You can also multiply across the imported columns with an array formula, covered in an earlier chapter:
=ARRAYFORMULA(IF(B2:B="","",B2:B*C2:C))Put that in D2 and you get a line total for each order. The formula spills down on its own, so keep the cells below D2 empty.
Keeping the URL and range in cells
Long text inside a formula is hard to maintain. A better habit is to store each piece in a cell and refer to those cells. If A1 holds the URL and B1 holds Orders!A1:C, write:
=IMPORTRANGE(A1,B1)Now you change the source or the range by editing a cell, not the formula. Pair this with INDIRECT-style techniques only with care. IMPORTRANGE itself accepts cell references for both arguments, so you rarely need anything more.
You can also use a named range from the source file. If the source defines a named range called OrdersTable, write "OrdersTable" as the range string. This survives row and column insertions in the source file, because the name moves with the data. The range string then needs no !.
Combining IMPORTRANGE with other functions
IMPORTRANGE returns an ordinary block of values, so you can wrap it in other functions.
=SUM(IMPORTRANGE("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID","Orders!B2:B"))This adds all quantities directly, without showing the imported rows.
A frequent pattern filters the imported data with QUERY. In QUERY, imported columns are called Col1, Col2, and so on, because the import carries no column letters:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID","Orders!A1:C"),"select Col1, Col2 where Col2 > 3",1)The last argument, 1, tells QUERY that the first row is a header.
How it works behind the scenes
Imports refresh automatically. Updates in the source usually appear in the report within a short time, though not always instantly. Heavy use slows the file. Each IMPORTRANGE is a separate live link, so a file with dozens of them, or with imports of very large ranges, loads slowly and may show Loading... for a while.
A good practice is to import once into a helper tab and have all other formulas refer to that tab. One import with many readers is cheaper than many imports of the same range.
Common errors and fixes
| Message | Typical cause | Fix |
|---|---|---|
| `#REF!` with "You need to connect these sheets" | First use of this file pair | Click the cell, then **Allow access** |
| `#REF!` with "Array result was not expanded because it would overwrite data in D2" | Cells in the spill area are not empty | Clear the cells in the way, or move the formula |
| `#REF!` with "Imported content is empty" | The range holds no values | Check the range and sheet name |
| `#REF!` with "Internal error" or a not-found message | Wrong URL or ID, or the source was deleted | Copy the URL again from the source file |
| `#REF!` with "Unable to parse range" | The sheet name does not exist, or the range text is malformed | Check spelling, `!`, and the case of the tab name in the source |
| `#VALUE!` | An argument is not text, such as a URL typed without quotes | Put both arguments in double quotes or point at cells holding text |
| `#NAME?` | The function name is misspelled | Write `IMPORTRANGE` correctly |
| Loading... for a long time | Many imports or a very large range | Import once into a helper tab and narrow the range |
Other gotchas:
- If someone renames the source tab, every
IMPORTRANGEthat names it breaks. A named range in the source avoids this. - The import cannot go beyond the source's size. Asking for
A1:Z1000from a sheet with 10 columns can trigger a range error. - There are limits on how much data one file can import in total. Very large imports can fail. If that happens, narrow the range or split the work.
- Imported cells have no formatting from the source. Numbers and dates arrive as values, and you format them in the report.
- Sharing the report does not share the source. Viewers without access to the source can see an error in the formula cell.
Summary of decision points
- Use a plain sheet reference (
='Sales Data'!B2) when both sheets are in the same file. - Use
IMPORTRANGEwhen the data lives in another file. - Use an open-ended range when the source grows.
- Store the URL and range in cells if you will change them or reuse them.
- Import once per range, then refer to the imported cells.
Exercise
A source spreadsheet has a tab named Monthly Costs with amounts in B2 downward and more rows added each month. You want the total of those amounts in a different spreadsheet, and the total should include new rows automatically. Write the formula, using SPREADSHEET_ID as the placeholder for the ID. Then say what you will see the first time you enter it and what to do.
Show answer
=SUM(IMPORTRANGE("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID","Monthly Costs!B2:B")). The open-ended range B2:B includes new rows. The first time, the cell shows #REF! with a message that the sheets need to be connected. Click the cell and choose Allow access, and the total appears. You need at least view access to the source file for this to work.
Exercise
Your formula =IMPORTRANGE("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID","Orders!A1:C") shows #REF! with the message that the array result was not expanded because it would overwrite data. What is wrong and how do you fix it?
Show answer
The import needs to spill into cells to the right of and below the formula cell, and some of those cells are not empty. Clear the cells in the way, or move the formula to an empty area where the whole result fits.