Advertisement
chickenstalker3

I need help with fixing my code

Jan 11th, 2024
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 16.79 KB | Fixit | 0 0
  1. Im trying to do a kind of personality quiz for school using google script and html but it keeps showing me an error message. :
  2.  
  3. This is the google script code (code.gs):
  4.  
  5. function doGet() {
  6.   return HtmlService.createHtmlOutputFromFile('index.html')
  7.       .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  8. }
  9.  
  10. // Use this code for Google Docs, Forms, or new Sheets.
  11. function onOpen() {
  12.   SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
  13.       .createMenu('Dialog')
  14.       .addItem('Open', 'openDialog')
  15.       .addToUi();
  16. }
  17.  
  18. function openDialog() {
  19.   var html = HtmlService.createHtmlOutputFromFile('index.html')
  20.       .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  21.   SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
  22.       .showModalDialog(html, 'Dialog title');
  23. }
  24.  
  25. function postit(answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9) {
  26.   var dataS = SpreadsheetApp.getActiveSpreadsheet();
  27.   var sheet = dataS.getSheetByName("Sheet1");
  28.   var lastRow = sheet.getLastRow() + 1;  
  29.   // The row below the last row of data
  30.  
  31.   sheet.getRange(lastRow, 1).setValue(new Date()) // adds the time
  32.   var useremail = Session.getActiveUser().getEmail();
  33.   sheet.getRange(lastRow, 2).setValue(useremail) // adds the EMAIL
  34.   // Writes in the form entries
  35.   // repeat for each answer sent
  36.   //  add one to the number each time for a new column
  37.   sheet.getRange(lastRow, 3).setValue(answer1)
  38.   sheet.getRange(lastRow, 4).setValue(answer2)
  39.   sheet.getRange(lastRow, 5).setValue(answer3)
  40.   sheet.getRange(lastRow, 6).setValue(answer4)
  41.   sheet.getRange(lastRow, 7).setValue(answer5)
  42.   sheet.getRange(lastRow, 8).setValue(answer6)
  43.   sheet.getRange(lastRow, 9).setValue(answer7)
  44.   sheet.getRange(lastRow, 10).setValue(answer8)
  45.   sheet.getRange(lastRow, 11).setValue(answer9)
  46.  
  47.   // PASTE THE BIG BLOCK OF CODE IN STEP 7.5 BELOW THIS ....
  48.      // This is the bit to customise, it write the message depending on the survey.
  49.    
  50.     //    LIST OF POSSIBILITIES
  51.     //                  0              1                2              3            4           5
  52.     var people = ["Peter Parker","Miles Morales","Peter Porker","Spider Noir","Gwen Stacy","Peni Parker"]
  53.     // You need as many zeros as things in list
  54.     var score = [0,0,0,0,0,0]   // 4 zeros, because 4 people in my list, you may need to add more
  55.    
  56.     // You will score point for the answer that matches the person
  57.     // score[..]++ means add one
  58.    
  59.     if(answer1 == "Tobey Maguire") {
  60.       score[2]++ // means add one to the score of people[2] in the list = Ringo
  61.     }
  62.     if(answer1 == "Tom Holland") {
  63.       score[0]++
  64.     }
  65.     if(answer1 == "Shameek Moore (the actor for Miles Morales)") {
  66.       score[1]+=2 // means add 2
  67.     }
  68.     if(answer1 == "Andrew Garfield") {
  69.       score[3]+=3 // means add 3
  70.     }
  71.    
  72.    
  73.     if(answer2 == "God") {
  74.       score[2]++
  75.     }
  76.     if(answer2 == "Your father") {
  77.       score[1]++
  78.     }
  79.     if(answer2 == "Your mother") {
  80.       score[0]+=2
  81.     }
  82.     if(answer2 == "Your teacher") {
  83.       score[3]+=2
  84.     }
  85.     if(answer2 == "Social Media") {
  86.       score[4]+=4 // means add 3
  87.     }
  88.     if(answer2 == "Your friend") {
  89.       score[5]+=5 // means add 3
  90.     }
  91.    
  92.     if(answer3 == "Skilled Assasin") {
  93.       score[1]+=2
  94.     }
  95.     if(answer3 == "Glorius Knight") {
  96.       score[3]+=3
  97.     }
  98.     if(answer3 == "Ultimate Protector") {
  99.       score[2]++
  100.     }
  101.     if(answer3 == "Unmatched Strategist") {
  102.       score[0]+=2
  103.     }
  104.     if(answer3 == "Helpful Medic") {
  105.       score[4]++ // means add 3
  106.     }
  107.     if(answer3 == "Genius Inventor") {
  108.       score[5]++ // means add 3
  109.     }
  110.    
  111.     if(answer4 == "Spidey Sense") {
  112.       score[1]++
  113.     }
  114.     if(answer4 == "Spider Webs") {
  115.       score[3]++
  116.     }
  117.     if(answer4 == "Wall-Crawling") {
  118.       score[0]++
  119.     }
  120.     if(answer4 == "Enhanced Strength") {
  121.       score[2]++
  122.     }
  123.     if(answer4 == "Healing Factor") {
  124.       score[4]++ // means add 3
  125.     }
  126.     if(answer4 == "Superhuman Agility") {
  127.       score[5]++ // means add 3
  128.     }
  129.  
  130.     if(answer5 == "Doctor Octopus") {
  131.       score[1]++
  132.     }
  133.     if(answer5 == "Green Goblin") {
  134.       score[3]++
  135.     }
  136.     if(answer5 == "Mysterio") {
  137.       score[0]++
  138.     }
  139.     if(answer5 == "Sandman") {
  140.       score[2]++
  141.     }
  142.     if(answer5 == "Venom") {
  143.       score[4]++ // means add 3
  144.     }
  145.     if(answer5 == "Electro") {
  146.       score[5]++ // means add 3
  147.     }
  148.  
  149.     if(answer6 == "Sports") {
  150.       score[1]++
  151.     }
  152.     if(answer6 == "Listening to music") {
  153.       score[3]++
  154.     }
  155.     if(answer6 == "Hanging out") {
  156.       score[0]++
  157.     }
  158.     if(answer6 == "Sleeping") {
  159.       score[2]++
  160.     }
  161.     if(answer6 == "Studying") {
  162.       score[4]++ // means add 3
  163.     }
  164.     if(answer6 == "Gaming") {
  165.       score[5]++ // means add 3
  166.     }
  167.  
  168.     if(answer7 == "Red") {
  169.       score[1]++
  170.     }
  171.     if(answer7 == "Brown") {
  172.       score[3]++
  173.     }
  174.     if(answer7 == "Blue") {
  175.       score[0]++
  176.     }
  177.     if(answer7 == "Green") {
  178.       score[2]++
  179.     }
  180.     if(answer7 == "Purple") {
  181.       score[4]++ // means add 3
  182.     }
  183.     if(answer7 == "Yellow") {
  184.       score[5]++ // means add 3
  185.     }
  186.  
  187.    if(answer8 == "Rabbit") {
  188.       score[1]++
  189.     }
  190.     if(answer8 == "Snake") {
  191.       score[3]++
  192.     }
  193.     if(answer8 == "Frog") {
  194.       score[0]++
  195.     }
  196.     if(answer8 == "Owl") {
  197.       score[2]++
  198.     }
  199.     if(answer8 == "Penguin") {
  200.       score[4]++ // means add 3
  201.     }
  202.     if(answer8 == "Crocodile") {
  203.       score[5]++ // means add 3
  204.     }
  205.  
  206.     if(answer9 == "Snow") {
  207.       score[1]++
  208.     }
  209.     if(answer9 == "Rain") {
  210.       score[3]++
  211.     }
  212.     if(answer9 == "Sunny") {
  213.       score[0]++
  214.     }
  215.     if(answer9 == "Windy") {
  216.       score[2]++
  217.     }
  218.     if(answer9 == "Cloudy") {
  219.       score[4]++ // means add 3
  220.     }
  221.     if(answer9 == "Foggy") {
  222.       score[5]++ // means add 3
  223.     }
  224.  
  225.     var message = "Thanks for filling in my form! <br/><br/>"
  226.    
  227.    // Summarise the answers and work out who had the most
  228.     biggest = -1 // will score the best score
  229.     var best_id // will be the ID of the best one
  230.     for(i=0;i<6;i++) {   // 4, because there are 4 things in my list - you will need to change this number
  231.      message = message + people[i] + " likeness score is " + score[i] + "<br/>"
  232.       if (score[i]>biggest) {
  233.         best_id = i;
  234.         biggest = score[i]
  235.       }
  236.     }
  237.  
  238.     //  This bit writes the result in the 14th column of your sheet
  239.     sheet.getRange(lastRow, 14).setValue(people[best_id])
  240.  
  241.      // Let's announce the winner
  242.     message = message + "<br/>It looks like you are " + people[best_id]
  243.  
  244.  
  245.     // This is the bit that sends EMAILs (optional)
  246.     //  this sets the message to HTML (don't change)
  247.     var advancedArgs = {htmlBody:message};  
  248.     //  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
  249.     //MailApp.sendEmail(useremail, "Survey results ", message , advancedArgs);
  250.  
  251.     // Sent the results message back to the webpage ....
  252.     return message;
  253.  
  254.  }
  255. function debugit() {
  256.  var sheet = SpreadsheetApp.getActiveSheet(); // Identifies the sheet
  257.   var lastRow = sheet.getLastRow();  // reads The last row of data
  258.   var numRows = 1;   // Number of rows to process. Set to 1, will only look at the last submission.
  259.   // Fetch the data
  260.   var dataRange = sheet.getRange(lastRow, 1, numRows, 20) // Selects the data for the last row and columns 1 to 10
  261.   var data = dataRange.getValues();
  262.   // Makes the row of data
  263.   var row = data[0];
  264.   var params = []
  265.   p=0;
  266.   while ((row[2+p].length>0)||(row[2+p]>0)) {
  267.     params[p] = row[2+p]
  268.     p++
  269.   }
  270.   // if you have 4 questions, you need 4 statements (params[0] -> params[3]) below
  271.   // if you have more questions continues the sequence (e.g 6 Qs add ,params[4],params[5])
  272.   postit(params[0],params[1],parmas[2],params[3])
  273. }
  274.  
  275.  
  276. this is the html code:
  277.  
  278. <!DOCTYPE html>
  279. <html>
  280.   <head>
  281.     <script language="JavaScript">
  282.  
  283.      function doPost() {
  284.     // copy this line for every set of radio buttons ... change the text (prefer) to that in your buttons after name=”
  285.      var answer1 = radiobuttonanswer("prefer");
  286. // e.g. this is copied and changed to answer2 and linked to the lyric set of radio buttons
  287.      var answer2 = radiobuttonanswer("advice");
  288.      var answer3 = radiobuttonanswer("describe");
  289.      var answer4 = radiobuttonanswer("power");
  290.      var answer5 = radiobuttonanswer("villain");
  291.      var answer6 = radiobuttonanswer("spare");
  292.      var answer7 = radiobuttonanswer("color");
  293.      var answer8 = radiobuttonanswer("animal");
  294.      var answer9 = radiobuttonanswer("weather");
  295.  
  296.     //  THIS SENDS THE FORM RESULTS BACK TO GOOGLE
  297.     // the items in the brackets after ‘postit’ need to match the code above
  298.            google.script.run.withSuccessHandler(onPost)
  299.                .postit(answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9);
  300.      }
  301.  
  302.      function radiobuttonanswer(bname) { // This checks your selected choice
  303.         var answer = document.getElementsByName(bname);
  304.         theans ="";
  305.         for (var i = 0, length = answer.length; i < length; i++) {
  306.         if (answer[i].checked) {
  307.             theans = answer[i].value;
  308.             break;
  309.         }
  310.        }
  311.   // THIS WILL RETURN A MESSAGE IF NOTHING HAS BEEN SELECTED
  312.        if (theans.length == 0) {
  313.           alert("Answer: nothing selected for button " + bname);
  314.           return -1;
  315.        }
  316.  
  317.        return theans;
  318.  
  319.        }
  320.  
  321.  
  322.  
  323.     function onPost(result) {
  324. // below is the bit that will eventually write the result
  325.     alert("Submitted Successfully");
  326.     document.getElementById('theresult').innerHTML = "<center><h3>" + result + "</h3></center>";
  327.      }
  328. </script>
  329.  
  330.     <base target="_top">
  331.   </head>
  332.   <body style="background-color: black; color: white;">
  333.     <center>
  334.       <h2>Which Spider-man are you?</h2>
  335.       <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" />
  336.  
  337.       <table>
  338.         <tr>
  339.           <td>
  340.             <h3>Which do you prefer?</h3>
  341.              <label><input type="radio" name="prefer" value="Peter Parker"/> Tom Holland</label><br/>
  342.              <label><input type="radio" name="prefer" value="Peter Porker"/> Tobey Maguire</label><br/>
  343.              <label><input type="radio" name="prefer" value="Spider Noir"/> Andrew Garfield</label><br/>
  344.              <label><input type="radio" name="prefer" value="Miles Morales"/> Shameek Moore (the actor for Miles Morales) </label><br/>
  345.           </td>
  346.         </tr>
  347.       </table>
  348.  
  349.       <table>
  350.         <tr>
  351.           <td>
  352.             <h3>If you need advice, who do you go to first?</h3>
  353.              <label><input type="radio" name="advice" value="Peter Parker"  /> Your Mother</label><br/>
  354.              <label><input type="radio" name="advice" value="Peter Porker"  /> God</label><br/>
  355.              <label><input type="radio" name="advice" value="Spider Noir"  /> Your Friend</label><br/>
  356.              <label><input type="radio" name="advice" value="Miles Morales"  /> Your Father</label><br/>
  357.              <label><input type="radio" name="advice" value="Gwen Stacy"  /> Your Teacher</label><br/>
  358.              <label><input type="radio" name="advice" value="Peni Prker"  /> Your Mother</label><br/>
  359.           </td>
  360.         </tr>
  361.       </table>
  362.  
  363.       <table>
  364.         <tr>
  365.           <td>
  366.             <h3>Which one of these describes you the most?</h3>
  367.              <label><input type="radio" name="describe" value="Peter Parker"  /> Unmatched Strategist</label><br/>
  368.              <label><input type="radio" name="describe" value="Peter Porker"  /> Ultimate Protector</label><br/>
  369.              <label><input type="radio" name="describe" value="Spider Noir"  /> Glorius Knight</label><br/>
  370.              <label><input type="radio" name="describe" value="Miles Morales" /> Skilled Assasin</label><br/>
  371.              <label><input type="radio" name="describe" value="Gwen Stacy"  /> Helpful Medic</label><br/>
  372.              <label><input type="radio" name="describe" value="Peni Prker"  /> Genius Inventor</label><br/>
  373.           </td>
  374.         </tr>
  375.       </table>
  376.  
  377.       <table>
  378.         <tr>
  379.           <td>
  380.             <h3>Choose one of these spider powers?</h3>
  381.              <label><input type="radio" name="power" value="Peter Parker"  /> Spidey Sense</label><br/>
  382.              <label><input type="radio" name="power" value="Peter Porker"  /> Wall-Crawling</label><br/>
  383.              <label><input type="radio" name="power" value="Spider Noir"  /> Healing Factor</label><br/>
  384.              <label><input type="radio" name="power" value="Miles Morales" /> Spider Webs</label><br/>
  385.              <label><input type="radio" name="power" value="Gwen Stacy"  /> Superhuman Agility</label><br/>
  386.           </td>
  387.         </tr>
  388.       </table>
  389.  
  390.       <table>
  391.         <tr>
  392.           <td>
  393.             <h3>Which one of these villains would you fight?</h3>
  394.              <label><input type="radio" name="villain" value="Peter Parker"  /> Doctor Octopus</label><br/>
  395.              <label><input type="radio" name="villain" value="Peter Porker"  /> Green Goblin</label><br/>
  396.              <label><input type="radio" name="villain" value="Spider Noir"  /> Mysterio</label><br/>
  397.              <label><input type="radio" name="villain" value="Miles Morales" /> Sandman</label><br/>
  398.              <label><input type="radio" name="villain" value="Gwen Stacy"  /> Venom</label><br/>
  399.              <label><input type="radio" name="villain" value="Peni Prker"  /> Electro</label><br/>
  400.           </td>
  401.         </tr>
  402.       </table>
  403.  
  404.       <table>
  405.         <tr>
  406.           <td>
  407.            <h3>How do you spend your spare time?</h3>
  408.              <label><input type="radio" name="spare" value="Peter Parker"  /> Studying</label><br/>
  409.              <label><input type="radio" name="spare" value="Peter Porker"  /> Sports</label><br/>
  410.              <label><input type="radio" name="spare" value="Spider Noir"  /> Gaming</label><br/>
  411.              <label><input type="radio" name="spare" value="Miles Morales" /> Sleeping</label><br/>
  412.              <label><input type="radio" name="spare" value="Gwen Stacy"  /> Listening to music</label><br/>
  413.              <label><input type="radio" name="spare" value="Peni Prker"  /> Hanging out</label><br/>
  414.           </td>
  415.         </tr>
  416.       </table>
  417.    
  418.       <table>
  419.         <tr>
  420.           <td>
  421.            <h3>Choose one of these colours</h3>
  422.              <label><input type="radio" name="color" value="Peter Parker"  /> Brown</label><br/>
  423.              <label><input type="radio" name="color" value="Peter Porker"  /> Purple</label><br/>
  424.              <label><input type="radio" name="color" value="Spider Noir"  /> Blue</label><br/>
  425.              <label><input type="radio" name="color" value="Miles Morales" /> Green</label><br/>
  426.              <label><input type="radio" name="color" value="Gwen Stacy"  /> Yellow</label><br/>
  427.              <label><input type="radio" name="color" value="Peni Prker"  /> Red</label><br/>
  428.  
  429.          </td>
  430.         </tr>
  431.       </table>
  432.  
  433.       <table>
  434.         <tr>
  435.           <td>
  436.            <h3>Which animal do you prefer?</h3>
  437.              <label><input type="radio" name="animal" value="Peter Parker"  /> Rabbit</label><br/>
  438.              <label><input type="radio" name="animal" value="Peter Porker"  /> Penguin</label><br/>
  439.              <label><input type="radio" name="animal" value="Spider Noir"  /> Snake</label><br/>
  440.              <label><input type="radio" name="animal" value="Miles Morales" /> Owl</label><br/>
  441.              <label><input type="radio" name="animal" value="Gwen Stacy"  /> Frog</label><br/>
  442.              <label><input type="radio" name="animal" value="Peni Prker"  /> Crocodile</label><br/>
  443.          </td>
  444.         </tr>
  445.       </table>
  446.  
  447.       <table>
  448.         <tr>
  449.           <td>
  450.            <h3>Which weather do you enjoy the most?</h3>
  451.              <label><input type="radio" name="weather" value="Peter Parker"  /> Windy</label><br/>
  452.              <label><input type="radio" name="weather" value="Peter Porker"  /> Sunny</label><br/>
  453.              <label><input type="radio" name="weather" value="Spider Noir"  /> Foggy</label><br/>
  454.              <label><input type="radio" name="weather" value="Miles Morales" /> Snow</label><br/>
  455.              <label><input type="radio" name="weather" value="Gwen Stacy"  /> Cloudy</label><br/>
  456.              <label><input type="radio" name="weather" value="Peni Prker"  /> Rain</label><br/>
  457.          </td>
  458.         </tr>
  459.       </table>
  460.      
  461.    
  462.       <input name="name1" type="button" value="SUBMIT" onClick="doPost();" style="font-size:24px"><br/>
  463.  
  464.  
  465.     </center>
  466.   </body>
  467. </html>
  468.  
  469.  
  470.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement