Members-only tutorial
Watch the video and get the practice sheet with membership.
About this Tutorial
Sheet Resources
Video Transcript
<div>0:00 Here's how to close a form after a certain number Learn about using numbers in your Apps Script for form responses. Opens in new tab of responses when the form is attached to a Google Sheet, and we're going to use Apps Script Discover how to use Apps Script to automate Google Sheets and Forms. Opens in new tab here.<br>0:08 So, first off, we need a sheet and a form. Let's create the form. This is when they're attached and there's only one form for the sheet.<br>0:17 So we're going to create a form here. We're just going to ask name. And we're going to publish Understand the publishing process for Google Forms and Sheets. Opens in new tab .
Now, this is really good for things that are like attendees or a certain number Learn about using numbers in your Apps Script for form responses. Opens in new tab of people have to respond and then no more responses.<br>0:37 Maybe signing up for a coupon code or something like that. It's a publish Understand the publishing process for Google Forms and Sheets. Opens in new tab and so we have a publish Understand the publishing process for Google Forms and Sheets. Opens in new tab sheet where Explore the WHERE clause in queries for filtering data. Opens in new tab accepting responses right now we have a link.<br>0:48 Here, let's copy that link and put in some responses. And let's just check that the form works totally fine. We have our form responses here.<br>0:59 We get an attendee, let's submit another response. Click submit and everything is working fine. It is accepting responses.
Let's close it after a certain amount.<br>1:12 So we'll have to write some App Script Discover how to use Apps Script to automate Google Sheets and Forms. Opens in new tab . That's over in extensions App Script Discover how to use Apps Script to automate Google Sheets and Forms. Opens in new tab . That'll bring up this menu. Uh, the first thing we're gonna write is, let's just check how many responses we have.<br>1:27 And then once we have our limit Find out how to limit query results in Google Sheets. Opens in new tab , we're going to close it. So let's call this. Let's check form. We'll have an event because we'll have the form response.<br>1:42 Actually, we don't even, I don't even think we have to have that. Um, event.
Uh, first off, Let's just get the form URL Get insights on how to retrieve and use form URLs in your scripts. Opens in new tab .<br>1:57 Variable form URL Get insights on how to retrieve and use form URLs in your scripts. Opens in new tab equals, Let's spreadsheetapp Learn about the SpreadsheetApp service in Google Apps Script. Opens in new tab .getactofspreadsheetgetformURL. This will get us the URL of this form here, so we don't have to keep, uh, we don't have to go and copy-paste it anywhere.<br>2:15 Let's actually get the form, uh, through formapp. So variable form equals, formapp.open... a URL, and we have our form URL Get insights on how to retrieve and use form URLs in your scripts. Opens in new tab right there.<br>2:27 And let's see. We're going to use the Boolean Understand Boolean values and their use in programming logic. Opens in new tab , um, is accepting responses and get responses linked, so let's, logar.log.<br>2:41 Form.is accepting responses. This will give us a true or false.
Uh, logar.log. Form.get responses dot length.<br>2:55 Okay, let's check this. And actually, let's have this, um, actually, we're running right now just to check it and see what occurs.<br>3:08 Just make sure we're getting the right thing. It is true, it's accepting responses, and we have two right. Now, so we now can get the number Learn about using numbers in your Apps Script for form responses. Opens in new tab of responses we have, and no, is it accepting responses or not.<br>3:19 So, if, and let's get this length out of the log and as a variable variable response count equals this. So, if, response- response count is greater than 50.
Actually, let's do it less.<br>3:42 Let's, uhh, you can put any number Learn about using numbers in your Apps Script for form responses. Opens in new tab here. Uhh, greater than three. Then what are we going to do? We're going to close this form. Form dot set accepting responses and we'll check, change.<br>4:01 This to false. And then just to log at logger dot log, we'll put in quotes accepting and put a response here, this one.<br>4:21 Form is accepting responses. Is it accepting responses or not? Cool. So, if we run this right now, nothing will happen because this response count is not greater than 3.
But we want to run this every time that a response is sent.<br>4:40 So we do need to do one more step. Let's go over to triggers. And on the bottom right, add trigger Discover how to set up triggers in Google Apps Script for automation. Opens in new tab .<br>4:48 We're gonna choose- Switch Learn how to use switch statements for conditional logic in scripts. Opens in new tab function Get to know functions and their importance in programming. Opens in new tab to run. If you have multiple functions Explore different types of functions in Google Apps Script. Opens in new tab , choose which one it is here. Check form. We're going to select Understand the SELECT clause in queries for data retrieval. Opens in new tab event type on form submit Learn how to handle form submissions in Google Apps Script. Opens in new tab and click save.<br>5:00 So now the very first one we put in here, once we have this, will not close the form because we'd have exactly 3. But what- it should close the form once we have- Have a good day.<br>5:13 3. So let's say Carl signs up.
And we can go back to our app script Discover how to use Apps Script to automate Google Sheets and Forms. Opens in new tab and look at our executions and see is it executing correctly?<br>5:23 It's completed. Maybe there's a log here. There, it's true, it's accepting. And let's now submit another response. Dave. Is kinda come to.<br>5:40 And let's see, is this going to close the form? It says completed. This one. Open. Fire off. And now we go back to our form actually before we looked at those executions.<br>6:07 Now this form isn't accepting. So let's submit another response. And now the form is no longer accepting responses.
So this totally works.<br>6:17 Let's refresh this last one. Just double check what this log says. Yup. Accepting full. Great. So now we can come- back to our form and we can manage it and- and choose to turn it back on.<br>6:30 Not accepting or accepting responses. But now the cool thing is that it will close it automatically after a certain number Learn about using numbers in your Apps Script for form responses. Opens in new tab of responses.<br>6:43 That's pretty awesome. So if it's greater than three, it closed. closed.
So it didn't close it on the third one.<br>6:51 So if you wanted it closed on the third one, you just say greater than or equal to three, in your app script Discover how to use Apps Script to automate Google Sheets and Forms. Opens in new tab .<br>6:59 Here, and, yeah, that's it. So going over this, we have two things we need to do, right? We need to create this function Get to know functions and their importance in programming. Opens in new tab and put it as a trigger Discover how to set up triggers in Google Apps Script for automation. Opens in new tab for on form submit Learn how to handle form submissions in Google Apps Script. Opens in new tab .<br>7:10 This works if you have one form in your- sheet. If you have multiple forms Discover the features and functionalities of Google Forms. Opens in new tab , you just need to get the URL of each one and then choose which one you want to open by URL here.<br>7:20 You can put in a text here of your URL, form URL Get insights on how to retrieve and use form URLs in your scripts. Opens in new tab . Totally fine.
I've find it a little more compact and a little more interesting to get the form URL Get insights on how to retrieve and use form URLs in your scripts. Opens in new tab automatically here without having to copy-paste the string Understand string manipulation and usage in programming. Opens in new tab here.<br>7:33 But up to you how you want to do it. And we're just using this if, uh, I have for counting the response count and saying, you know, we can do greater than or equal to if we want to here, just to make sure we have a very specific amount.<br>7:48 Thanks for watching.</div>