Forum Discussion
Exporting Variables into a Google Spreadsheet
Having read this post on how to export variables to be read in a google spreadsheet, I set about trying to get this working in my project, I soon ran into problems, I just could not get it to work as google have changed some of the ways they work their drive documents.
So, after a LOT of google searching and testing various methods, plus reading this article, I came up with the following method which is a combination of the two articles – and it works, hurrah!
1. Create a new google spreadsheet and change the sheet name (lower left hand corner) to DATA. Make sure your column names are the same as the variables you want to export (exactly matching case)
2. Find out your spreadsheet ‘key’ by looking in the address bar, the key is the long series of letters and numbers after /d/ and before /edit:
Eg: https://docs.google.com/spreadsheets/d/1gF0QCNA1TZCNY3qr2zNpWKQ8r43D39o-nqz56c7cQUs/edit#gid=1283040575
Key = 1gF0QCNA1TZCNY3qr2zNpWKQ8r43D39o-nqz56c7cQUs
3. Open the script editor (Tools ==> Script Editor) in your spreadsheet and paste the script from the attached file (I have copied and pasted this script and just kept in all the instructions)
4. There are two places in the script where it says “KEY” – copy and paste your key into these two places, between the “”.
5. Run the setUp script twice (Run menu). The first time it will ask for permission to run (grant it), then the second time you select to run it you won't get any popup indication it has run.
6. Go to Publish > Deploy as web app, enter Project Version name and click 'Save New Version', set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously).
7. Copy the 'Current web app URL' and paste in a notepad file to keep safe.
8. In Articulate, add a trigger to run javascript and use the following code, replacing “Current web app URL” with your URL you copied in the previous step (in””):
var player = GetPlayer();
$.ajax({
url:
"Current web app URL",
type: "POST",
data: {"Name": player.GetVar("Name")
, "Rating1": player.GetVar("Rating1")
, "Rating2": player.GetVar("Rating2")
, "Rating3": player.GetVar("Rating3")
, "Rating4": player.GetVar("Rating4")
, "postRating1": player.GetVar("postRating1")
, "postRating2": player.GetVar("postRating2")
, "postRating3": player.GetVar("postRating3")
, "Postrating4": player.GetVar("Postrating4")},
success: function(data)
{
console.log(data);
},
error: function(err) {
console.log('Error:', err);
}
});
return false;
9. Publish your articulate project – you need to host it somewhere like SCORM cloud or a LMS. When it has finished publishing click to open the files and edit the story.html and story_html5.html files – add the following line in under the line <!-- version: X.X.XXX.XXX --> or somewhere after <head>:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
10. Go back to articulate and click ‘zip’ – then publish your zip file and hopefully it will work!
This isn’t for the faint hearted but it is so worth it if you can get it working! Good luck!
- SteveFlowersCommunity Member
Heya Kate -
I've used the method you show here as well and it works pretty well to setup a Google Spreadsheet as an endpoint. This method is handy for capturing inline survey results, to grab choices for choice analysis, and for double capturing completions if your LMS happens to be temperamental. One of the cool things about the Google Spreadsheet is the notification / subscription feature. Subscribing to updates on a per-addition or digest basis is great for infrequent submissions. It's also handy to use formulas and linked sheets to create public dashboards. The original data can be locked away while the linked data (filtered and sanitized) can be exposed in a published location.
Here's an alternate method that uses a Google Form as the intermediary to submit the fields from Storyline. A little less setup using a form to process the submissions and I've had fewer issues with permissions (if your Google Apps instance is corporate or government, using GAPPS script is potentially problematic).
Add this to a trigger when the slide loads. This will write the JQuery library to your header so you won't need to do any post-publish surgery. This will load Jquery for both the HTML and HTML5 versions. This can also be used for other libraries. I've successfully used this method to load up the soundmanager.js library for persistent background music.
function add_script(scriptURL,oID) {
var scriptEl = document.createElement("script");
var head=document.getElementsByTagName('head')[0];
scriptEl.type = "text/javascript";
scriptEl.src = scriptURL;
scriptEl.id=oID;
head.appendChild(scriptEl);}//only want to add these once!
if(document.getElementById('jquery')==null){
add_script("https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js","jquery");
}After you've built your form and viewed the live form, inspect the form elements or explore the source to find the field references for your form. These will be something like entry.156531987. Write these down or copy them into another reference document. While you're at it, grab the form ID from the URL.
var player=GetPlayer();
var vRate=player.GetVar("slider1");
var vSuggestion=player.GetVar("TextEntry");var fTarget="https://docs.google.com/a/nara.gov/forms/d/YOURFORMID/formResponse";
function submit_form(){$.ajax({
url: fTarget,
type: "POST",
data: {"entry.156531987" :vRate,"entry.1547132205":vSuggestion},
success: function(data) {
//won't return anything since it's cross domain
}
});
return false;
}
setTimeout(submit_form(), 2000);Edit: Left off a little bit. I delay the submission of data for a couple of seconds for some reason. Must have fixed an issue in testing. Probably will work find without the delay.
- BonnieAnderson1Community Member
Thanks Steve!
Bonnie
- PaulMacDonell-bCommunity Member
Steve,
This is wonderful code. I've used this technique and it works for one of my columns of data. I'm sure I'll figure it why the other columns are not saved eventually.
My main questions though is "How do I load the variables I've saved to the Google Spreadsheet back into Storyline?" I want to do this in order to save the progress of videos that a user has viewed in Storyline. There are three videos and I'd like to record which ones they have watched. I mark these with a checkmark in the story. Your routine allows me to do this.
If the user leaves before watching all three videos, the variables in the Google spreadsheet will track that. When they return to the story, I'd like them to know which videos they have watched and which ones they haven't, again, by checking (using states) those that they've watched. The data from the Google spreadsheet, once loaded, will provide this capability.
Of course, I'm a script kiddie with little knowledge of JavaScript. Any help you can provide will be most appreciated! Thanks again for your contributions!!!
- LauraBrunningCommunity Member
Hi,
We're using the form solution to send variables to a Google sheet. All works OK, but only if I edit the story.html file and add in the <script type="text/javascript src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> bit in the header.
I've tried the solution above to automatically write it to the header and added it to a trigger before the rest of the javascript stuff kicks in, but this way, it doesn't send the data to the Google sheet. It'll only work if I add it manually to the output file before zipping.
Am I missing something really obvious? Any help would be most appreciated!
- SteveFlowersCommunity Member
The Google Form itself should be shared with everyone.
However, there are a few things that can stop up the works. Watching the Javascript console is helpful in tracking down where things might be wrong. Here are a list of potential gotchas:
- The JQuery dependency isn't loaded before the ajax call. This means on the same slide on the base layer with your ajax call in another layer or in another slide that's sure to be hit. Loading everything at once, it'll likely fail as there's quite a bit that happens to force the load.
- There's another script error somewhere. Invalid script, a missing variable, an LMS call tested outside of an LMS. Lots of stuff can put a stop to script execution before you reach the ajax call.
- Using the wrong form address. In my example above, the link provided was for my organization. It includes the org address. If you use this same string, it'll fail unless your part of my org:)
fTarget="https://docs.google.com/a/nara.gov/forms/d/YOURFORMID/formResponse";
- KateRobertsonCommunity Member
Meant to say, the data lines of the code corresponds to what you want put into your spreadsheet so adapt as necessary.
Thanks Kate for sharing your steps here - I know it's a common question so it's always great to be able to point users to another example. :-)
- ErichRenkenCommunity Member
Thanks, Kate, for sharing this solution. If anyone wants to see an example in action, I've posted it here:
http://erichrenken.com/SaveResultsToGoogleSpreadsheet/story.html - ErichRenkenCommunity Member
Hi, Paul. I've played around with that a bit and had success using the Google "Query Language"
https://developers.google.com/chart/interactive/docs/querylanguage#Setting_the_Query_from_JavaScript
What makes this tricky is that you'll need a unique way of identifying the person taking the course, assuming that there will be multiple rows in your spreadsheet. Otherwise, you might pull back a different person's data.In this way, Google Sheets start functioning like a database in which you can set/get/edit/delete values.
- PaulMacDonell-bCommunity Member
Erich,
Thanks for this! Good material for my subway rides. My son, a mobile
developer, is also very excited to see how this works.
- LouiseLindopCommunity Member
Hi Albert. In case it helps I've attached my test .story. I have tweaked code slightly to get rid of the [Object object] error I was getting but it all seems to work well. You'll find the javascript code on the last slide. Happy to share the spreadsheet too if you want to message me with your google details.
- LouiseLindopCommunity Member
Hi Norman.
I did a test in SL1 and it all worked as it did in SL2. I also did a test to see if it all worked OK without a results slide and it also worked as it did with results slide. I also am testing on a website (no LMS). There is no message displayed in SL when the data is sent. I'm not sure what else to suggest. If I were you I would just start again at the beginning with fresh spreadsheet and SL file, but perhaps you have tried that?
Louise
- SteveFlowersCommunity Member
Hi Norman -
Since the script can bomb on any number of things, I'll usually run the JS console in the browser to watch for errors. This will quickly tell you if the problem is in your send function or if there's something else going on.
Another method you can use in a pinch is editing the user.js file on the server. Many FTP clients allow for direct edit and save of the file (auto-upload of changes). This would make it easy to insert an alert, something like alert("here"); to see how far through the script it goes. Wherever the alert stops working, the problem is somewhere above that.
If you don't get errors and the alert works, it may be that something is going on with your field ID's or the structure of your endpoint string.
Do you have a link you can share here?
- KateRobertsonCommunity Member
Hi Norman
I am glad you have managed to get it working. Louise changed the code slightly to get rid of the popup so that section should read:
success: function(data){
console.log(data);
},
error: function(err) {
console.log('Error:', err);
}Although, your code seems to work as well!