5. Build the Vote Function
In this video, you will write the “Vote” function.
If you don’t know which part of the code you are responsible for, check with your group members.
The vote function will allow the user to choose whether they want to see the movie or not.
Your code will do three things: Connect to the movie data spreadsheet, add a new row to the spreadsheet tab and record the “yes” or “no” vote, display a pop-up message to confirm the user’s vote was recorded.
First, open the "vote dot gs" file.
The vote function appears at the top of the script editor.
Define a “sheet” variable.
Starting inside the first curly brace, define a new variable called “sheet” by typing: v-a-r, space, sheet, space, equals, space, SpreadsheetApp dot.
The autofill menu appears.
Select “getActive Spreadsheet."
Then, define where in the sheet the data will go.
Your data will go into the first tab in the active sheet.
Type “dot getSheets.”
The autofill menu appears again.
Select “getSheets” from the menu.
Next, type open and closed square brackets with a 0 in between.
Close the statement with a semicolon.
This tells the computer to record the data in the first tab of the spreadsheet.
Now that you’re connected to the spreadsheet, record the user’s vote.
Press Enter to start a new line.
Start typing “sheet dot appendRow” and select “appendRow” from the autofill menu when it appears.
Replace “rowContents” with an open and a closed square bracket.
Inside the brackets, add “vote dot poster, comma, space, vote dot vote.”
Close the statement with a semicolon.
These instructions tell the computer to record the poster name and the user’s vote -- yes or no -- into two columns in the spreadsheet.
Next, tell the computer to display an alert, or a pop-up message, that tells the user their vote was recorded.
On a new line, type “return Content Service” just as you see it on the screen.
Select “create Text Output, colon, Text Output."
Click in between the two parentheses.
Type: one single quote, alert, open parenthesis, double quote.
Then, add the message you want your user to see after they vote and close the text with another double quotation mark.
The alert will show all of the text you type between these quotation marks.
Use whatever message you would like, as long as it is appropriate.
Check out these examples.
Close the parentheses and add a semicolon, single quotation mark, and one more closed parenthesis to finish the statement.
Make sure all of this text is between the parentheses, exactly as you see on your screen.
At this point, you have coded instructions to: Connect to the movie data spreadsheet, Add a new row to the spreadsheet tab and record the “yes” or “no” vote.
Display a pop-up message to confirm the user’s vote was recorded.
The final step is to set the “Mime Type” of the alert box.
Add a closed parenthesis and semicolon to close the line.
Now, click “save” and try running your program.
If you get a reference error, your partners may not be finished with their part of the code.
This is a common problem when collaborating on a program.
To fix it, programmers use “tests,” which are separate programs used to try out sections of code.
Testing only your section ensures that a bug in one person’s code will not break the entire program.
Your starter project contains a test file.
Open “tests dot gs” from the left menu.
Do not alter any part of this code.
Select the “test vote” function and run the code.
Then, check the code log. See if the program returns any errors.
In this example it doesn't.
If the log returns an error message, go back to the vote file and check your code carefully.
Ask your neighbor or the teacher if you need more help.
If the log returns an error message like, "Your vote function doesn't appear to be adding the vote result to the end of the spreadsheet," or, "You haven't defined a function named 'Vote' yet," go back to your vote file and check your code carefully.
If you need help, ask a neighbor or your teacher.
Once you’re finished writing the vote code, check on your group members to see if they need help.
Then, when you are all ready, move on to the next video to publish your group’s code.
Now, it’s your turn.
Write your code to perform the following functions: Create the vote function and get the vote data.
Open the movie poster spreadsheet.
Add a new row to the spreadsheet tab and record the “yes” or “no” vote.
Open an alert when a user casts a vote.
And set the mime type so the computer can read your users’ responses.
When everyone in your group has finished, move on to the next video where you will publish your website.
- Create the vote function and get the vote data
- Open the movie poster spreadsheet
- Add a new row to the spreadsheet tab and record the yes or no vote.
- Open an alert when a user casts a vote.
- Set the mime type.
- When everyone in your group has finished, move on to the next video where you will publish your website.