I was hoping to get help using Google Spreadsheet data with a script I have paste below. The script is a calculator/comparison tool, that compares movie rating with one another. Essentially, the scrip displays two drop down lists (both lists are identical), and you choose one movie in one drop down and another movie in other drop down, then you hit calculate. Inside the script, which you will see below, I have assigned values to each movie and as you can imagine, this becomes tedious. So what I would like to do is use a Google Drive Spreadsheet to almost power the data inside this script, so when the google spreadsheet is saved, the values change inside the script.
... as you can see, the data is pretty straight forward, and matches what's inside the script, however, given I'm forced to surround the data by '<option value=xx.xx> and '</option>, I am unsure how to allow the data on the spreadsheet to power this calc.
2. Script:
HTML Code:
<html><head><script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-30589636-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script><body><select id="player1"><option value=00.00>SELECT MOVIE</option><option value=80.50>X-Files 1 (Sci-Fi)</option><option value=78.50>Star Trek (Sci-Fi)</option><option value=100.00>Die Hard 1 (Action)</option><option value=88.50>Die Hard 2 (Action)</option><option value=58.00>Die Hard 3 (Action)</option></select><p>Vs.</p><select id="player2"><option value=00.00>SELECT MOVIE</option><option value=80.50>X-Files 1 (Sci-Fi)</option><option value=78.50>Star Trek (Sci-Fi)</option><option value=100.00>Die Hard 1 (Action)</option><option value=88.50>Die Hard 2 (Action)</option><option value=58.00>Die Hard 3 (Action)</option></select><input type="button" value="Calc" onclick="calcVals()"/><p>=</p><div id="results"></div><script type="text/javascript">
function calcVals() {
var score1=document.getElementById("player1").value
var score2=document.getElementById("player2").value
var pick1=document.getElementById("player1").options[document.getElementById("player1").selectedIndex].text
var pick2=document.getElementById("player2").options[document.getElementById("player2").selectedIndex].text
var grandscore=score1-score2;
if (grandscore==0){
document.getElementById("results").innerHTML=pick1+" <p><strong>is as good as</strong></p> "+pick2
} else {
document.getElementById("results").innerHTML=grandscore>0?pick1+" <p><strong>is better than</strong></p> "+pick2+" <em>by</em> "+grandscore+" <strong>percent</strong>":pick2+" <p><strong>is better than</strong></p> "+pick1+" <em>by</em> "+Math.abs(grandscore)+" <strong>percent</strong>"
}
}
</script>
So, again, in Summary:
I. I would love to have the above script adjusted so that automatic saves on the Google Spreadsheet will just show up on the calc.
II. I essentially do not want to even have to open up my javascript file any longer, I'd like to just have to update my google spreadsheet
Thanks in advance if anyone can provide a solution to this!!!!
this seems to work - it goes a bit funny on the last option because you haven't given Die Hard 3 a rating. I put an alert in the onchange so you can see the values coming through. It's a bit of a low-rent way of looping through the columns, but I couldn't see another...
Code:
<body>
<select id="res" onchange="alert(this.value)"></select>
<script>
function getData(o){
var idx=0;
var obj=o.feed.entry;
for (var i = 0; i < obj.length; i++) {
if(i%3==0){
document.getElementById("res").options[idx]=new Option(obj[i+1].content.$t,obj[i].content.$t)
console.log(document.getElementById("res").options[idx])
idx++ }
}
}
</script>
<script src="https://spreadsheets.google.com/feeds/cells/0AjdJSwsrKzjqdHppVmlQSktrNmxnazRNakFVVHExYkE/od6/public/basic?alt=json-in-script&callback=getData"></script>
</body>
Last edited by xelawho; 12-27-2012 at 02:58 PM.
Reason: slightly lower-rent method
I may have tested your code wrong, but my goal (and my javascript that i pasted above) has two different drop down lists, as my goal is allow the user to select one movie in one drop down list and another in the other drop down list (Even if the movies are the same)... if the same, the calculator will say that they are equal. It's a comparison calculator.
Any chance you could help me create this from what you made, or help me manipulate my javascript above to read the google spreadsheet? Thanks a ton!!!!!!!
So what I'm after:
1. Two drop down lists
2. Same identical lists in each drop down
3. The same movie being selected in each would call for an equal rating
4. Somehow have the calculator say what movie is better, or just repeat the values would be sufficient enough, as the user would know which is rated higher if it gave both values
5. I am wanting to just adjust the values inside the spreadsheet and again I only need one spreadsheet, as the drop down lists will again use the same data/list
= This would suggest that the first two are better than the second two by, i think my math is right, 14 or so %... I know this seems overboard, but I'd use it for more than just 2 for 2 movie comparison calculations, so 2 for 2 would be the most ideal for me.
so you make another 2 selects, fill the options the same way, add the selected values of 1 & 2 and 3 & 4 and compare them in the same way. Which bit are you having trouble with?
Bookmarks