Patrik W Patrik W - 6 months ago 11
JSON Question

Get data from Google sheet using json

I have created a Google sheet where I´ve put a lot of logic and functions. Now I just want to import data from the sheet into my webiste (using MVC in my html project).

I read about it on this site:
https://ctrlq.org/code/20004-google-spreadsheets-json

It all seem easy and that it should work fine just using his code, but perhaps I am missing something. Below is my code, only added the $(document).ready part and my spreadsheet ID.

<div class="results"></div>

<script>

// ID of the Google Spreadsheet
var spreadsheetID = "1-JfSulb2FIh0C-hyy-ihYV0ieFi3ephC75oYB1mrKr8";

// Make sure it is public or set to Anyone with link can view
var url = "https://spreadsheets.google.com/feeds/list/" + spreadsheetID + "/od6/public/values?alt=json";
$(document).ready(function () {
$.getJSON(url, function(data) {

var entry = data.feed.entry;

$(entry).each(function(){
// Column names are name, age, etc.
$('.results').prepend('<h2>'+this.gsx$name.$t+'</h2><p>'+this.gsx$age.$t+'</p>');
});
});
});

</script>


So the problem is that I don´t get any data from the sheet into my html div. However I try it´s just empty. Is there something else I need to do to get it to work? I´ve named the cells in the first row to name and age but get nothing.

My Google sheet:

https://docs.google.com/spreadsheets/d/1-JfSulb2FIh0C-hyy-ihYV0ieFi3ephC75oYB1mrKr8/edit?usp=sharing

Is there something I need to do in the sheet other than making it public and copy the code into my project? I guess this is a noob question but I just can't get it to work. Any help would be greatly appreciated!

Answer

There was no error in the code.

I had for some reason deleted the jQuery part in my _Layout page

<script src="~/lib/jquery/dist/jquery.js"></script>

Stupid mistake which I didn't think about looking for. Anyways now it works just fine :)