Members-only tutorial

Watch the video and get the practice sheet with membership.

See membership options

How do I reference a different spreadsheet in Apps Script?

About this Tutorial

I'll show you how to get text from one spreadsheet file to another with Apps Script. Yes you can do this by Importrange() but you might want another way to do it.

Featured Formulas

Video Transcript

<div>0:01 A member of BetterSheets asked this question, uh Sharon asked this question actually, if we have a first sheet and a second sheet, how do we get information from one sheet to the other without import Learn how to use import functions in your spreadsheets effectively. Opens in new tab range Understand how to define and use ranges in your spreadsheet. Opens in new tab but with actually with Apps Script Discover the power of Apps Script for automating tasks in Google Sheets. Opens in new tab ?<br>0:13 Uh because absolutely we can get whatever is in a first spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab , let's say this one says hi there, and then the second spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab over here on the second tab, we want to do uh get that in a whatever is in a one.<br>0:25 So we'll do equals import Learn how to use import functions in your spreadsheets effectively. Opens in new tab range Understand how to define and use ranges in your spreadsheet. Opens in new tab , and we need the spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab URL, in this case the here, put that there.

We need the range Understand how to define and use ranges in your spreadsheet. Opens in new tab string Explore how to work with strings in your formulas and scripts. Opens in new tab which is going to be sheet one exclamation End Quotes.<br>0:40 And we get the information. We have to allow access Find out how to grant access for your scripts to run smoothly. Opens in new tab , but then we actually get whatever is there. We can say hello, we can say hello there, and in the second sheet it'll update there.<br>0:54 Cool, but how do we do this with uh Apps Script Discover the power of Apps Script for automating tasks in Google Sheets. Opens in new tab ? And here's the answer here.

Uhm, we're gonna use a function Learn about functions and how to create your own in Apps Script. Opens in new tab , get first AI, A1 AI, we're gonna get the text which is spreadsheetApp Understand the SpreadsheetApp service for Google Sheets. Opens in new tab .openByURL, and we're gonna put the URL in there.<br>1:13 Actually, let me walk through this whole thing because I think we can walk through this much simpler than just explaining it.<br>1:20 So what we're gonna need to do first is have some place to get it and some place to put it.<br>1:26 So where Dive into using query where clauses for data filtering. Opens in new tab we're gonna put the information is gonna be sheet1, A1 in the second spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab .

Again, we're gonna go up to extensions, Apps Script Discover the power of Apps Script for automating tasks in Google Sheets. Opens in new tab , and we're gonna open this file.<br>1:36 We're gonna write some function Learn about functions and how to create your own in Apps Script. Opens in new tab name, we'll call it getFirstAI. We need to put the information somewhere, but let's create a variable text equals Learn how to compare text values in your scripts. Opens in new tab , we will have some information there.<br>1:47 But how do we get that text into this second spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab ?

Is we're gonna do spreadsheetApp Understand the SpreadsheetApp service for Google Sheets. Opens in new tab .getActiveSpreadsheet.getSheetByName We're going to do Sheet1, getRange Understand how to retrieve specific ranges in your spreadsheets. Opens in new tab , a1, because we want to put a1 in a1, and then we're gonna do setValue Discover how to set values in your spreadsheet cells programmatically. Opens in new tab .<br>2:12 And why I put this variable text up here is because this variable down here is gonna be text. Whatever output we get from here, we're gonna set that value.<br>2:20 We're gonna do this all with abstract here. So what is this value up here?

We're gonna go back to the first spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab , get that URL again, and we're gonna do SpreadsheetApp Understand the SpreadsheetApp service for Google Sheets. Opens in new tab .<br>2:40 We're gonna paste the URL in there inside of quotes, and then we're gonna do pretty much the same thing. We're gonna do getRange Understand how to retrieve specific ranges in your spreadsheets. Opens in new tab , sorry, not getRange Understand how to retrieve specific ranges in your spreadsheets. Opens in new tab , getSheetByName, because now everything before this first dot like this, getSheetByName, getRange Understand how to retrieve specific ranges in your spreadsheets. Opens in new tab , setValue Discover how to set values in your spreadsheet cells programmatically. Opens in new tab , or getValue Learn how to retrieve values from your spreadsheet cells. Opens in new tab , is what we're gonna do.<br>3:02 But before that is different.

But after it, it's gonna treat this sheet the same as if we're opening the active spreadsheet Understand how to work with the active spreadsheet in your scripts. Opens in new tab .<br>3:11 So we're gonna do sheet1, And instead of setValue Discover how to set values in your spreadsheet cells programmatically. Opens in new tab , we're going to getValue Learn how to retrieve values from your spreadsheet cells. Opens in new tab . Now we're gonna save this, and over in our second sheet actually, I'm gonna delete that import Learn how to use import functions in your spreadsheets effectively. Opens in new tab range Understand how to define and use ranges in your spreadsheet. Opens in new tab .<br>3:28 We're gonna have nothing there. We're gonna have hello world in A1 on our first spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab . And this is the URL that we're grabbing.<br>3:36 This is the sheet name Learn how to reference and use sheet names in your scripts. Opens in new tab down here, sheet1. We can change that later, and I'll show you how.

But all we're gonna do is hit run.<br>3:43 When you do this for the very first time, you are going to have to accept the permissions, or set the permissions.<br>3:48 It says execution started, execution completed. Let's look at our second sheet, and we have hello world. And so if we update this, let's say we have some more information here.<br>3:58 It's not going to automatically update this. We will have to come back here and click run again.

But, watch some other videos I have, and you can set up triggers to do this.<br>4:08 Instead of clicking run every single time, you can set up some triggers in order to do this every day, or based on some thing you do in the sheet.<br>4:16 There it is, more information. I want to show you some issues you might have with this. Let's say we do not get the entire URL.<br>4:27 Maybe we want to edit this URL in some way. Let's do just this, and see if this runs. If we edited that URL, hit run, and we have invalid argument Explore how to use arguments in your functions effectively. Opens in new tab URL.<br>4:38 See, this is the URL. If we use, this URL.

Let's use it, and just go to it. See what happens.<br>4:45 It takes us to the spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab , but for some reason it doesn't have this just slash edit slash or hashtag GID, but maybe it is just the last slash we need.<br>5:00 Let's see. Let's add a last slash, command s to save that update, hit run again, and it's fine.

Right, so it's not the entire URL we need, but we do need up and to and including that last slash, so we don't need this edit, we don't need a GID, Peace.<br>5:20 But we do need the URL, but there's another thing we can do here, what if we don't want the entire URL, we can use this ID, which is inside of the URL, let's see what we can do here, instead of getFirstA1, let's say getA2, and instead of openByURL, we're going to replace this with openByID, and let's<br>5:48 see if just this ID works, and again the ID we're using is this one cue, right here, this text in between in the D slash and the last, the edit.<br>5:59 We're gonna go over here.

Here, put it in quotes, hit command s, let's put something in A2, do we get this, and also let's change this to A2 where Dive into using query where clauses for data filtering. Opens in new tab we're gonna go, let's save it all, we're gonna select Discover how to use query select clauses for data retrieval. Opens in new tab A2.<br>6:18 Hit record Learn how to record actions in your spreadsheet for automation. Opens in new tab . Run and see if that works, and it works, actually we got A1, sorry, we needed to change A2 as well, so we need to change both ranges Understand the concept of ranges and how to manipulate them. Opens in new tab .<br>6:32 Let's run that again, double check it works, and it does.

So can we use this function Learn about functions and how to create your own in Apps Script. Opens in new tab in the inside of a sheet, just like we did with import Learn how to use import functions in your spreadsheets effectively. Opens in new tab range Understand how to define and use ranges in your spreadsheet. Opens in new tab .<br>6:43 Let's see, get first A1, we're going to hit enter, and we're going to get an error Find out how to troubleshoot common errors in Google Sheets. Opens in new tab , and it says you do not have permission to call, and it gives us the required permissions, this is Google APIs, this is line 3, but this is rather weird, right?<br>6:56 What this is, what this is literally saying is that, yes, we Do not.

have the permissions, but what is really happening in the background is that this type of function Learn about functions and how to create your own in Apps Script. Opens in new tab doesn't work inside of a function Learn about functions and how to create your own in Apps Script. Opens in new tab inside of a cell, so basically that cell doesn't have permission, whereas our function Learn about functions and how to create your own in Apps Script. Opens in new tab was and is working<br>7:18 perfectly from Apps Script Discover the power of Apps Script for automating tasks in Google Sheets. Opens in new tab and within Apps Script Discover the power of Apps Script for automating tasks in Google Sheets. Opens in new tab , but if we put that function Learn about functions and how to create your own in Apps Script. Opens in new tab inside of a cell, that cell doesn't really have permission, so it's getting sort Learn how to sort data in your spreadsheets efficiently. Opens in new tab of in the way, and it's a little bit daunting to see this kind of uhm error Find out how to troubleshoot common errors in Google Sheets. Opens in new tab , and if you're getting this error, you're probably going to be watching<br>7:35 this video if you get this uhm required permissions, Google APIs, or whatever.

Or this exception, you do not have permission to call SpreadsheetApp Understand the SpreadsheetApp service for Google Sheets. Opens in new tab .openByUrl.<br>7:49 Ah, but again, the fix is, just use it in Apps Script Discover the power of Apps Script for automating tasks in Google Sheets. Opens in new tab , and run it with some trigger Discover how to set up triggers for automated tasks in Google Sheets. Opens in new tab , set this value here, sometimes you may try to write this with like return, text, and that's how a custom function Learn about functions and how to create your own in Apps Script. Opens in new tab usually works, but we don't need a custom function Learn about functions and how to create your own in Apps Script. Opens in new tab in this case, we can just set, get the information,<br>8:07 and set it using this SpreadsheetApp Understand the SpreadsheetApp service for Google Sheets. Opens in new tab .getActiveSpreadsheet.getSheetByName, get range Understand how to define and use ranges in your spreadsheet. Opens in new tab , and set value.

And this is the trick here, to setting the value inside the function Learn about functions and how to create your own in Apps Script. Opens in new tab , instead of doing it through the through a custom function Learn about functions and how to create your own in Apps Script. Opens in new tab , like this.<br>8:22 But we do, we can get this, and I'm really excited you watched this whole video. If you want to see more, uh, Apps Script Discover the power of Apps Script for automating tasks in Google Sheets. Opens in new tab , uh, check out my other, uh, courses, we have Spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab Automation 101, we have Learn to Code, there's we have Master Spreadsheet Get insights into managing and manipulating spreadsheets. Opens in new tab Automation on Udemy, if you want to see the entire<br>8:43 20-hour course, goes really deep into APIs and all that stuff.

Um, but yeah, I'm really excited, I hope this has helped you a lot, Sharon especially.</div>