Skip to main content

Transcript

In this video, you will write the "Vote" function.

If you do not know which part of the code you are responsible for, check with your group members.

Only watch the videos necessary to write your part of the code.

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.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 "VAR, space, sheet, space, equals, space, SpreadsheetApp." The autofill menu appears.

Select "getActive." Add open and closed parentheses.

Then, define where in the sheet the data will go.

Your data will go into the first tab in the active sheet.

Type ".getSheets." The autofill menu appears again.

Select "getSheets" from the menu.

Add open and closed parentheses.

Next, type open and closed square brackets with a zero 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.appendRow" and select "appendRow" from the autofill menu when it appears.

Add open and closed parentheses.

Next, type open and closed square brackets.

Inside the brackets, add "vote.poster, comma, space, vote.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 ContentService." just as you see it on the screen.

The autofill menu appears again.

Select "createTextOutput." Add open and closed parentheses.

Click in between the two parentheses.

Type "one single quote, alert, open parenthesis, double quote, another double quotation mark." Close the parentheses and add a semicolon, single quotation mark, and one more closed parenthesis to finish the statement.

Check to be sure your code looks exactly like what is on the screen.

Then, add the message you want your user to see after they vote between these quotation marks.

The alert will show all of the text you type.

Use whatever message you would like, as long as it is appropriate.

Check out these examples.

Make sure all of this text is between the parentheses, exactly as you see it 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.

Data can be written in basic text, Javascript, HTML, or many other computer languages.

This part of your code tells the computer that the data will be in Javascript and returns that data to the spreadsheet.

After the last parentheses, press Enter and begin typing: ".setMimeType, open parenthesis, ContentService.MimeType.JAVASCRIPT." Use the autofill menus to help you fill in the code precisely.

Add a 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 the 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.gs" from the 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.

Great! 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 the 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.

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.


Instructions

  1. Create the vote function and get the vote data
  2. Open the movie poster spreadsheet
  3. Add a new row to the spreadsheet tab and record the yes or no vote.
  4. Open an alert when a user casts a vote.
  5. Set the mime type.