Sync Calendar to Sheets

Learn how to sync your Google Calendar events directly into Google Sheets using a simple Apps Script. This tutorial walks you through creating a custom menu and functions to automate the process.

So you want to sync your calendar to sheets. So you have a calendar that looks like this. You want all of these events inside of your spreadsheet here, our file. So we want to have the name. We want to have the start and end and description. We're going to go up to extensions appscript and type a little bit of appscript here. We're going to rename it to sync cow to sheets. And everything we have to do is going to be in here. I'm going to add a couple of extra things that are just going to help us along the way. For example, over at betterheets.co/snippets,

I can grab this function on open and I can paste it in here. And it's a custom menu . So, I'm going to call this cal calendar sync. this create menu and this will show up in our sheet. I'll show you how. But first, we need to actually have something here which is going to be sync cal sync calendar. I'm going to delete this second item. If we need it, we'll have it. But now there's going to be a function we have to create called function sync cal. It has to be exactly like this with some parentheses and some curly brackets. And inside of those curly brackets, we're going to do all of our special stuff. We're going to get a variable sheet equals spreadsheet app.getactive sheet. That's just going to be whatever sheet we're on right now.

We're going to do this sort of a little dirty like we're just going to get this done. We need a variable calendar. Calendar uh is going to be our email. So we can get our email a couple of ways. We can get spreadsheet . Get active spreadsheet . That's the file. Get owner . Get email. So, we can just grab our own email from here or we can type this in if we want. We can just say email.com. Whatever our email is, you can write it like that. For my purposes right now, if you want to just like copy and paste this kind of thing, this is going to be much easier to get the email. And it has to be the email of the appcript owner and the sheet owner and the calendar owner have to all be the same. You can't

have one appcript looking at another calendar. Just FYI, I'm going to end up getting the uh events from a calendar variable cal equals a capital C calendar app D. And that ID is going to be our calendar ID that we put up here. Again, could be just our email address. I'm getting email address here. We're going to get some events. Cal dot and we need a start time and an end time. And that's like start date and end date . Do start time is new date. Variable end time is new date as well. Now, here's the thing. We can and should edit one of these, right? We can say, hey, we're going to look at

the month in the past or the month in the future. If we want the month in the past, we do start time set minus30. Okay. However, if we want the month ahead, we don't do that. We do end time dot set date end time dot plus 30. Okay? Or we can do both. which means from today we're looking at 30 days in the past and 30 days in the future. For the purposes of this exact video, I'm only going to do 30 days in the future. Okay, let's move this up a little bit. Now, the last thing we need and the final thing is a for loop. I'm going to

type it out without explaining much, but we're just going to start at zero. We're going to iterate until we get through the events.length, however many there are. Uh, and we're going to iterate one. I ++ just adds one each time. And we're going to need to grab each event some data from it. And that data we're going to end up going sheet dot and in brackets we want title, start, end, and probably description. And I'll write that out. Description. So these are the four things we're going to get here. Variable title equals event and I in square brackets. So each one we're going to iterate. The first one get the title. We're going to say get title variable start equals events. I

going to get start time here again. Um even though it says start time, I think it's going to come out as a date and a time. We can always come back in here and edit it a little bit and change the thing. Like if you just want the date, if you just want the time, but for our sort of quick purposes, we're just going to get the start time and the end time. Get end time. And the description is the same events. I in square brackets get description. What's really cool is you can just do events get and you can see all of these things we can get. We can get the event type, guest list, ID, guest by email, original calendar ID, title, visibility.

We can get uh location as well. Um lots of cool things, right? Date created if we want, but you might not want all of those little pieces. So, I'm going to save this. Now, what's going to happen is I can actually run this right now. I can go up to run, select sync cal and run it. And I will come back to this. However, I want to show you what this onopen does. So, I'm going to close my app script . I'm going to save it. Make sure you save it. Close my app script . And I'm going to refresh my sheet. Now, when I refresh my sheet, there's going to be something up here next to help. And it's calendar sync. Oh, I spelled sync calendar wrong. So, go up to extensions appcript. Edit that. But this allows you before I edit the

text here. This allows you to access this appcript from your sheet. And it's a really really cool thing. And it's just a little bit of tiny bit of code, but you need to spell calendar correctly. Okay. Save. Now I'm going to refresh it again. The very first time I run it, I'm going to have to authorize it. And I can probably do it through here. Yep. Authorization required . And it only has to happen the first time. Select all . Continue. I need to do one thing which is move my append row inside of the for loop. Let's run it again. Calendar sync sync calendar. And there we got all of the things that are on my calendar here really, really quickly. It runs very quickly. Again, I

can just take this comment out and I can comment the end time here. And now I'm going to append all of the things that are in the past. So maybe I start a new sheet. I grab my headers . I add those calendar sync. Sync calendar. And it's everything that was in the past 30 days. If I want, you know, 90 days, maybe I can go add a new sheet. Paste that. Calendar sync. Sync calendar. Pretty cool, right? You can get all of your calendar items with this script only if you want to sync calendar for say you want different functions for different sets of times. So I'm

going to actually take this function copy and paste it. I'm going to add another item to my sync calendar past 30 days past 30. So, I'm going to rename this and set this to 30. New one. I'm going to go here, next 30 days. I'm going to say that. And then next 30 days. So, now I have two different functions . I've I've copied the entire uh function . I've changed actually I need to change this. I need to comment out this. Put in the end time 30 days. Perfect. And this is 30 days. Now again to close my appcript, refresh my sheet and once I do that,

my calendar sync up here has two different sync calendars, past 30 days, next 30 days. So I can keep those nice and neat in sheets and update them all the time. Pretty cool, right? Extensions appcript. A little bit of appcript goes a long way. I hope you enjoy adding this function on open as well and learning how to write the script and learning how to create more than one script. You're watching better sheets here on YouTube. You have two options here for your next video. Before you go, comment down below if you have any question whatsoever and I'll make future videos from those questions. But for right here, right now, one of these videos could be your next Google Sheet.