Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Im trying to do a kind of personality quiz for school using google script and html but it keeps showing me an error message. :
- This is the google script code (code.gs):
- function doGet() {
- return HtmlService.createHtmlOutputFromFile('index.html')
- .setSandboxMode(HtmlService.SandboxMode.IFRAME);
- }
- // Use this code for Google Docs, Forms, or new Sheets.
- function onOpen() {
- SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
- .createMenu('Dialog')
- .addItem('Open', 'openDialog')
- .addToUi();
- }
- function openDialog() {
- var html = HtmlService.createHtmlOutputFromFile('index.html')
- .setSandboxMode(HtmlService.SandboxMode.IFRAME);
- SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
- .showModalDialog(html, 'Dialog title');
- }
- function postit(answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9) {
- var dataS = SpreadsheetApp.getActiveSpreadsheet();
- var sheet = dataS.getSheetByName("Sheet1");
- var lastRow = sheet.getLastRow() + 1;
- // The row below the last row of data
- sheet.getRange(lastRow, 1).setValue(new Date()) // adds the time
- var useremail = Session.getActiveUser().getEmail();
- sheet.getRange(lastRow, 2).setValue(useremail) // adds the EMAIL
- // Writes in the form entries
- // repeat for each answer sent
- // add one to the number each time for a new column
- sheet.getRange(lastRow, 3).setValue(answer1)
- sheet.getRange(lastRow, 4).setValue(answer2)
- sheet.getRange(lastRow, 5).setValue(answer3)
- sheet.getRange(lastRow, 6).setValue(answer4)
- sheet.getRange(lastRow, 7).setValue(answer5)
- sheet.getRange(lastRow, 8).setValue(answer6)
- sheet.getRange(lastRow, 9).setValue(answer7)
- sheet.getRange(lastRow, 10).setValue(answer8)
- sheet.getRange(lastRow, 11).setValue(answer9)
- // PASTE THE BIG BLOCK OF CODE IN STEP 7.5 BELOW THIS ....
- // This is the bit to customise, it write the message depending on the survey.
- // LIST OF POSSIBILITIES
- // 0 1 2 3 4 5
- var people = ["Peter Parker","Miles Morales","Peter Porker","Spider Noir","Gwen Stacy","Peni Parker"]
- // You need as many zeros as things in list
- var score = [0,0,0,0,0,0] // 4 zeros, because 4 people in my list, you may need to add more
- // You will score point for the answer that matches the person
- // score[..]++ means add one
- if(answer1 == "Tobey Maguire") {
- score[2]++ // means add one to the score of people[2] in the list = Ringo
- }
- if(answer1 == "Tom Holland") {
- score[0]++
- }
- if(answer1 == "Shameek Moore (the actor for Miles Morales)") {
- score[1]+=2 // means add 2
- }
- if(answer1 == "Andrew Garfield") {
- score[3]+=3 // means add 3
- }
- if(answer2 == "God") {
- score[2]++
- }
- if(answer2 == "Your father") {
- score[1]++
- }
- if(answer2 == "Your mother") {
- score[0]+=2
- }
- if(answer2 == "Your teacher") {
- score[3]+=2
- }
- if(answer2 == "Social Media") {
- score[4]+=4 // means add 3
- }
- if(answer2 == "Your friend") {
- score[5]+=5 // means add 3
- }
- if(answer3 == "Skilled Assasin") {
- score[1]+=2
- }
- if(answer3 == "Glorius Knight") {
- score[3]+=3
- }
- if(answer3 == "Ultimate Protector") {
- score[2]++
- }
- if(answer3 == "Unmatched Strategist") {
- score[0]+=2
- }
- if(answer3 == "Helpful Medic") {
- score[4]++ // means add 3
- }
- if(answer3 == "Genius Inventor") {
- score[5]++ // means add 3
- }
- if(answer4 == "Spidey Sense") {
- score[1]++
- }
- if(answer4 == "Spider Webs") {
- score[3]++
- }
- if(answer4 == "Wall-Crawling") {
- score[0]++
- }
- if(answer4 == "Enhanced Strength") {
- score[2]++
- }
- if(answer4 == "Healing Factor") {
- score[4]++ // means add 3
- }
- if(answer4 == "Superhuman Agility") {
- score[5]++ // means add 3
- }
- if(answer5 == "Doctor Octopus") {
- score[1]++
- }
- if(answer5 == "Green Goblin") {
- score[3]++
- }
- if(answer5 == "Mysterio") {
- score[0]++
- }
- if(answer5 == "Sandman") {
- score[2]++
- }
- if(answer5 == "Venom") {
- score[4]++ // means add 3
- }
- if(answer5 == "Electro") {
- score[5]++ // means add 3
- }
- if(answer6 == "Sports") {
- score[1]++
- }
- if(answer6 == "Listening to music") {
- score[3]++
- }
- if(answer6 == "Hanging out") {
- score[0]++
- }
- if(answer6 == "Sleeping") {
- score[2]++
- }
- if(answer6 == "Studying") {
- score[4]++ // means add 3
- }
- if(answer6 == "Gaming") {
- score[5]++ // means add 3
- }
- if(answer7 == "Red") {
- score[1]++
- }
- if(answer7 == "Brown") {
- score[3]++
- }
- if(answer7 == "Blue") {
- score[0]++
- }
- if(answer7 == "Green") {
- score[2]++
- }
- if(answer7 == "Purple") {
- score[4]++ // means add 3
- }
- if(answer7 == "Yellow") {
- score[5]++ // means add 3
- }
- if(answer8 == "Rabbit") {
- score[1]++
- }
- if(answer8 == "Snake") {
- score[3]++
- }
- if(answer8 == "Frog") {
- score[0]++
- }
- if(answer8 == "Owl") {
- score[2]++
- }
- if(answer8 == "Penguin") {
- score[4]++ // means add 3
- }
- if(answer8 == "Crocodile") {
- score[5]++ // means add 3
- }
- if(answer9 == "Snow") {
- score[1]++
- }
- if(answer9 == "Rain") {
- score[3]++
- }
- if(answer9 == "Sunny") {
- score[0]++
- }
- if(answer9 == "Windy") {
- score[2]++
- }
- if(answer9 == "Cloudy") {
- score[4]++ // means add 3
- }
- if(answer9 == "Foggy") {
- score[5]++ // means add 3
- }
- var message = "Thanks for filling in my form! <br/><br/>"
- // Summarise the answers and work out who had the most
- biggest = -1 // will score the best score
- var best_id // will be the ID of the best one
- for(i=0;i<6;i++) { // 4, because there are 4 things in my list - you will need to change this number
- message = message + people[i] + " likeness score is " + score[i] + "<br/>"
- if (score[i]>biggest) {
- best_id = i;
- biggest = score[i]
- }
- }
- // This bit writes the result in the 14th column of your sheet
- sheet.getRange(lastRow, 14).setValue(people[best_id])
- // Let's announce the winner
- message = message + "<br/>It looks like you are " + people[best_id]
- // This is the bit that sends EMAILs (optional)
- // this sets the message to HTML (don't change)
- var advancedArgs = {htmlBody:message};
- // The sends the EMAIL to the student's EMAIL, you will have to allow it the first time you run. Remove the “//” from in front if you want it to post an EMAIL
- //MailApp.sendEmail(useremail, "Survey results ", message , advancedArgs);
- // Sent the results message back to the webpage ....
- return message;
- }
- function debugit() {
- var sheet = SpreadsheetApp.getActiveSheet(); // Identifies the sheet
- var lastRow = sheet.getLastRow(); // reads The last row of data
- var numRows = 1; // Number of rows to process. Set to 1, will only look at the last submission.
- // Fetch the data
- var dataRange = sheet.getRange(lastRow, 1, numRows, 20) // Selects the data for the last row and columns 1 to 10
- var data = dataRange.getValues();
- // Makes the row of data
- var row = data[0];
- var params = []
- p=0;
- while ((row[2+p].length>0)||(row[2+p]>0)) {
- params[p] = row[2+p]
- p++
- }
- // if you have 4 questions, you need 4 statements (params[0] -> params[3]) below
- // if you have more questions continues the sequence (e.g 6 Qs add ,params[4],params[5])
- postit(params[0],params[1],parmas[2],params[3])
- }
- this is the html code:
- <!DOCTYPE html>
- <html>
- <head>
- <script language="JavaScript">
- function doPost() {
- // copy this line for every set of radio buttons ... change the text (prefer) to that in your buttons after name=”
- var answer1 = radiobuttonanswer("prefer");
- // e.g. this is copied and changed to answer2 and linked to the lyric set of radio buttons
- var answer2 = radiobuttonanswer("advice");
- var answer3 = radiobuttonanswer("describe");
- var answer4 = radiobuttonanswer("power");
- var answer5 = radiobuttonanswer("villain");
- var answer6 = radiobuttonanswer("spare");
- var answer7 = radiobuttonanswer("color");
- var answer8 = radiobuttonanswer("animal");
- var answer9 = radiobuttonanswer("weather");
- // THIS SENDS THE FORM RESULTS BACK TO GOOGLE
- // the items in the brackets after ‘postit’ need to match the code above
- google.script.run.withSuccessHandler(onPost)
- .postit(answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9);
- }
- function radiobuttonanswer(bname) { // This checks your selected choice
- var answer = document.getElementsByName(bname);
- theans ="";
- for (var i = 0, length = answer.length; i < length; i++) {
- if (answer[i].checked) {
- theans = answer[i].value;
- break;
- }
- }
- // THIS WILL RETURN A MESSAGE IF NOTHING HAS BEEN SELECTED
- if (theans.length == 0) {
- alert("Answer: nothing selected for button " + bname);
- return -1;
- }
- return theans;
- }
- function onPost(result) {
- // below is the bit that will eventually write the result
- alert("Submitted Successfully");
- document.getElementById('theresult').innerHTML = "<center><h3>" + result + "</h3></center>";
- }
- </script>
- <base target="_top">
- </head>
- <body style="background-color: black; color: white;">
- <center>
- <h2>Which Spider-man are you?</h2>
- <img src="https://1.bp.blogspot.com/-ej0p9rE5pDE/XJQUkGq7qRI/AAAAAAAAEO4/fCpjd6AFAtcseSRCXoEVm9CmomkIqGGRACKgBGAs/w0/spiderman-into-the-spider-verse-characters-uhdpaper.com-4K-6.jpg" width="512" height="216" class="center" />
- <table>
- <tr>
- <td>
- <h3>Which do you prefer?</h3>
- <label><input type="radio" name="prefer" value="Peter Parker"/> Tom Holland</label><br/>
- <label><input type="radio" name="prefer" value="Peter Porker"/> Tobey Maguire</label><br/>
- <label><input type="radio" name="prefer" value="Spider Noir"/> Andrew Garfield</label><br/>
- <label><input type="radio" name="prefer" value="Miles Morales"/> Shameek Moore (the actor for Miles Morales) </label><br/>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <h3>If you need advice, who do you go to first?</h3>
- <label><input type="radio" name="advice" value="Peter Parker" /> Your Mother</label><br/>
- <label><input type="radio" name="advice" value="Peter Porker" /> God</label><br/>
- <label><input type="radio" name="advice" value="Spider Noir" /> Your Friend</label><br/>
- <label><input type="radio" name="advice" value="Miles Morales" /> Your Father</label><br/>
- <label><input type="radio" name="advice" value="Gwen Stacy" /> Your Teacher</label><br/>
- <label><input type="radio" name="advice" value="Peni Prker" /> Your Mother</label><br/>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <h3>Which one of these describes you the most?</h3>
- <label><input type="radio" name="describe" value="Peter Parker" /> Unmatched Strategist</label><br/>
- <label><input type="radio" name="describe" value="Peter Porker" /> Ultimate Protector</label><br/>
- <label><input type="radio" name="describe" value="Spider Noir" /> Glorius Knight</label><br/>
- <label><input type="radio" name="describe" value="Miles Morales" /> Skilled Assasin</label><br/>
- <label><input type="radio" name="describe" value="Gwen Stacy" /> Helpful Medic</label><br/>
- <label><input type="radio" name="describe" value="Peni Prker" /> Genius Inventor</label><br/>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <h3>Choose one of these spider powers?</h3>
- <label><input type="radio" name="power" value="Peter Parker" /> Spidey Sense</label><br/>
- <label><input type="radio" name="power" value="Peter Porker" /> Wall-Crawling</label><br/>
- <label><input type="radio" name="power" value="Spider Noir" /> Healing Factor</label><br/>
- <label><input type="radio" name="power" value="Miles Morales" /> Spider Webs</label><br/>
- <label><input type="radio" name="power" value="Gwen Stacy" /> Superhuman Agility</label><br/>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <h3>Which one of these villains would you fight?</h3>
- <label><input type="radio" name="villain" value="Peter Parker" /> Doctor Octopus</label><br/>
- <label><input type="radio" name="villain" value="Peter Porker" /> Green Goblin</label><br/>
- <label><input type="radio" name="villain" value="Spider Noir" /> Mysterio</label><br/>
- <label><input type="radio" name="villain" value="Miles Morales" /> Sandman</label><br/>
- <label><input type="radio" name="villain" value="Gwen Stacy" /> Venom</label><br/>
- <label><input type="radio" name="villain" value="Peni Prker" /> Electro</label><br/>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <h3>How do you spend your spare time?</h3>
- <label><input type="radio" name="spare" value="Peter Parker" /> Studying</label><br/>
- <label><input type="radio" name="spare" value="Peter Porker" /> Sports</label><br/>
- <label><input type="radio" name="spare" value="Spider Noir" /> Gaming</label><br/>
- <label><input type="radio" name="spare" value="Miles Morales" /> Sleeping</label><br/>
- <label><input type="radio" name="spare" value="Gwen Stacy" /> Listening to music</label><br/>
- <label><input type="radio" name="spare" value="Peni Prker" /> Hanging out</label><br/>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <h3>Choose one of these colours</h3>
- <label><input type="radio" name="color" value="Peter Parker" /> Brown</label><br/>
- <label><input type="radio" name="color" value="Peter Porker" /> Purple</label><br/>
- <label><input type="radio" name="color" value="Spider Noir" /> Blue</label><br/>
- <label><input type="radio" name="color" value="Miles Morales" /> Green</label><br/>
- <label><input type="radio" name="color" value="Gwen Stacy" /> Yellow</label><br/>
- <label><input type="radio" name="color" value="Peni Prker" /> Red</label><br/>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <h3>Which animal do you prefer?</h3>
- <label><input type="radio" name="animal" value="Peter Parker" /> Rabbit</label><br/>
- <label><input type="radio" name="animal" value="Peter Porker" /> Penguin</label><br/>
- <label><input type="radio" name="animal" value="Spider Noir" /> Snake</label><br/>
- <label><input type="radio" name="animal" value="Miles Morales" /> Owl</label><br/>
- <label><input type="radio" name="animal" value="Gwen Stacy" /> Frog</label><br/>
- <label><input type="radio" name="animal" value="Peni Prker" /> Crocodile</label><br/>
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td>
- <h3>Which weather do you enjoy the most?</h3>
- <label><input type="radio" name="weather" value="Peter Parker" /> Windy</label><br/>
- <label><input type="radio" name="weather" value="Peter Porker" /> Sunny</label><br/>
- <label><input type="radio" name="weather" value="Spider Noir" /> Foggy</label><br/>
- <label><input type="radio" name="weather" value="Miles Morales" /> Snow</label><br/>
- <label><input type="radio" name="weather" value="Gwen Stacy" /> Cloudy</label><br/>
- <label><input type="radio" name="weather" value="Peni Prker" /> Rain</label><br/>
- </td>
- </tr>
- </table>
- <input name="name1" type="button" value="SUBMIT" onClick="doPost();" style="font-size:24px"><br/>
- </center>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement