jeudi 26 mars 2015

Error in query: Invalid JSON string when Creating a Chart from a Spreadsheet? GAS


Why am I getting the error Error in query: Invalid JSON string when attempting to create a chart from a spreadsheet using the Google example script from their documentation here?


I can't figure it out. The only thing that I changed is the spreadsheet URL. The rest is directly from the documentation example. I provided all of my code below. Any help you guys could give would be greatly appreciated. Thanks, Brandon





function doGet() {
return HtmlService.createTemplateFromFile('index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
};

function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
};






<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="http://ift.tt/JuZcy0"></script>
<script type="text/javascript">

// Load the Visualization API and the controls package.
google.load("visualization", '1', {packages:['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var query = new google.visualization.Query(
'http://ift.tt/1M0dNkF');
query.send(handleQueryResponse);
}

function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}

var data = response.getDataTable();
var chart = new google.visualization.ColumnChart(document.getElementById('columnchart'));
chart.draw(data, { legend: { position: 'none' } });
}
</script>

<title>Data from a Spreadsheet</title>
</head>

<body>
<span id='columnchart'></span>
</body>
</html>






<div id="EntireSite">

<div>
<?!= include('Chart'); ?>
</div>



enter image description here



ng-click to call a function in services for logging ratings onto firebase


I am creating a website which logs user ratings to firebase, but I cannot accomplish the calling of a services.js function which is meant to log the user's rating on click of one of stars 1-5. Is there something wrong with my code, or something I am missing? Here is a snippet of the services.js, controllers.js, as well as the ratings div from the html. Any help is much appreciated!


From the services.js



angular.module('dev.services', [])
.factory('ratingsService', function() {
return {
//removed return envelope
//var obj = {};
/**
* @todo Add a check to determine whether or not the specified user has already rated the specified item.
*
* @param {String} userId
* @param {String} itemType
* @param {String} itemId
* @param {Number} rating
*/
rate: function(userId, itemType, itemId, rating) {
// @todo Construct a new promise that resolves when both Firebase operations have been performed.
$firebase( new Firebase(dvUrl + '/users/' + userId + '/ratings/' + itemType + '/' + itemId) ).$set(rating);
$firebase( new Firebase(dvUrl + '/' + itemType + '/' + itemId + '/ratings/' + userId) ).$set(rating);
console.log("SUCCESS!");
},
/**
* @param {Object} ratings
* @returns {Number} avgRating
*/
getAvgRating: function(ratings) {
var ratingsTotal = 0;
var ratingsCount = this.getRatingsCount(ratings);

if(ratingsCount === 0) {
return 0;
} else {
for(var rating in ratings) {
if( ratings.hasOwnProperty(rating) ) {
ratingsTotal += parseFloat(ratings[rating]);
}
}

return (ratingsTotal / ratingsCount).toFixed(1);
}
},
/**
* @param {Object} ratings
* @returns {Number} ratingsCount
*/
getRatingsCount: function(ratings) {
return ratings == null
? 0
: Object.keys(ratings).length;
}
//removed return envelope
//return obj;
}
})


From the controllers.js



angular.module('DeViine.controllers', [])
.controller('rateCtrl', ['$scope', 'ratingsService', 'dvUrl', function($scope, ratingsService) {
$scope.rate = function (item, rating) {
ratingsService.rate(userId, item.type, item.id, rating);
}
}]);


From the HTML



<div class="ratings">
<div ng-click="rate(itemId, 1)" class="rate rate1" title="1"></div>
<div ng-click="rate(itemId, 2)" class="rate rate2" title="2"></div>
<div ng-click="rate(itemId, 3)" class="rate rate3" title="3"></div>
<div ng-click="rate(itemId, 4)" class="rate rate4" title="4"></div>
<div ng-click="rate(itemId, 5)" class="rate rate5" title="5"></div>
</div>




Knockoutjs - Template with dynamic variables


I'm learning javascript and ko by trying my hand at a character sheet for my favorite tabletop rpg. Since this is day 3, I'm not even sure I'm approaching this correctly, but below is my attempt at producing a form for skills. It produces no errors, but also has zero functionality.


The evals are my attempt to produce a reference to other observables/computed observables, but no luck. The $data part of the eval was a later addition of an interpretation of advice on how to correct the problem.



<script type="text/html" id="skills-template">
<div data-bind="template: { name: 'skills-template', foreach: skills }"></div>
<div class="row">
<div class="col-md-1" data-bind="text: skill"></div>
<div class="col-md-1"><strong data-bind="text: eval('$data.' + skill + 'Total')"></strong></div>
<div class="col-md-1"><input size="2" data-bind="textInput: eval('$data.' + skill + 'Ranks')" /></div>
<div class="col-md-1"><strong data-bind="text: eval('$data.' + skillMod)"></strong></div>
<div class="col-md-1"><input size="2" data-bind="textInput: eval('$data.' + skill + 'Mod')" /></div>
<div class="col-md-1"><strong data-bind="text: untrained"></strong></div>
<div class="col-md-1"><strong>&nbsp;</strong></div>
<div class="col-md-1"><strong>&nbsp;</strong></div>
<div class="col-md-4">&nbsp;</div>
</div>

</script>


For reference, here is the skills obj I created.



self.skills = [
{ skill: 'Acrobatics', skillMod: 'dexMod', untrained: 'Yes', acPenalty: 'Yes' },
{ skill: 'Appraise', skillMod: 'Dex', untrained: 'Yes', acPenalty: 'No' },
{ skill: 'Bluff', skillMod: 'Dex', untrained: 'Yes', acPenalty: 'No' },
{ skill: 'Climb', skillMod: 'Dex', untrained: 'Yes', acPenalty: 'Yes' },
{ skill: 'Craft', skillMod: 'Dex', untrained: 'Yes', acPenalty: 'No' },
{ skill: 'Diplomacy', skillMod: 'Dex', untrained: 'Yes', acPenalty: 'No' }
]


And this is an example of a computed observable I tried.



self.AcrobaticsTotal = ko.computed(function() {
return Number(self.AcrobaticsRanks()) + Number(self.dexMod()) + Number(self.AcrobaticsMod()) + Number(self.AcrobaticsTrained());
}, this);


I can get this all working if I hand code it, but I thought I would do it smartly and avoid typing all 40 skills and all of the associated observables. Thanks for any advice.


edit: I should have been more clear, I'm trying to get the data-bind to be the equivalent of other observables that I've declared. I want eval($data.' + skill + 'Total') to be acrobaticsTotal and have ko act on the acrobaticsTotal observable.





Will this code ever return false?


I'm reviewing some code where the logic looks flawed. I'm not sure if the following code will ever return false because of the if else return flow. My question is, will the following code ever return false, or even throw an error?



function performSearch(e) {
if(e.keyCode === RETURN_KEY_KEYCODE) {
var select = document.getElementById("selectmenusearch");
var selected = select.options[select.selectedIndex].value;
if(selected === 'organisation') {
submitSearchForm('<%= doOrganisationSearchURL %>');
} else {
submitSearchForm('<%= doIndividualSearchURL %>');
}
} else {
return false;
}
return true;
}


So the flow to me looks like



if (this condition is true) {
//execute some code
} else {
return false
}
else return true


NB: I know it would be better to refactor to have only one return statement but it looks to me like there are two else statements.





Backbone save() respond with success


I have a properly configured Backbone Model, but when I run the save method like so:



backbone_model.save(null, {

success: function () { console.log('ok'); },

error : function () { console.log('not ok'); }

});


I have a successful POST request sent (and Backbone request is triggered) to my Node instance which responds with nothing except 200 OK like so:



app.post('/backbone', function (req, res) {
res.send(); // Automatically sends 200 OK
}


But Backbone does not trigger the sync event, and the error callback is executed. I've also tried returning 201 Created, with no success.





Angular: When does app.config get called?


I am creating a chrome extension UI using angular. I want to make it so when the user clicks the icon in the upper right the correct screen appears. If the user is not logged in they should go to the login page. If the user is logged in and in drawing mode then they should go to the drawing screen, if they are logged in and not drawing then they should go to the main menu.


My main problem is checking whether or not they are already in drawing mode. I am sending a message to my content scripts to check whether or not I am in drawing mode, but for some reason this callback is never getting called! Very disappointing. I'm not sure when code in app.config gets called; when does it?


app.js



app.config(function($stateProvider, $urlRouterProvider) {
var rootRef = new Firebase(mysterious_url);
var user = rootRef.getAuth();
chrome.tabs.sendMessage('isInDrawingMode', {action: 'isInDrawingMode'}, function(response) {
if (!user) {
$urlRouterProvider.otherwise('/login');
} else if (response.inDrawingMode) {
$urlRouterProvider.otherwise('/draw');
} else {
$urlRouterProvider.otherwise('/main');
}
});


contentscripts.js



chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse){
// Toggle User Canvas Messages
if ( request.toggle === 'off' ){
// toggleUserCanvasOff();
disableDrawingMode();
sendResponse({confirm:'canvas turned off'});
} else if ( request.toggle === 'on' ){
enableDrawingMode();
// toggleUserCanvasOn();
sendResponse({confirm:'canvas turned on'});

// Initialize toggle status for popup button
} else if ( request.getStatus === true ){
sendResponse({status:toggle});
} else if (request.canvasData) { // new Canvas data
onCanvasData(request.site, request.user, request.data);
} else if (request.erase){
eraseUserCanvas();
} else if (request.changeColor){
lineColor = request.changeColor;
} else if (request.image){
getCurrentUser(function(user){
var userCanvas = $('.'+ user);
addOneTimeClickEvent(userCanvas, addImage, request.image);
});
} else if (request.action) {
sendResponse({inDrawingMode: "true"});
}
}
);




How to change css styling for different lines within a paragraph in javascript


So I have this code that defined a variable called circle, and appended it a title. The title has four lines, first is the country, second is the price, 3rd is the value, and 4th is the gdppc. Now I want to make the gdppc a larger font size than the previous 3 variables. This would require me to style gdppc separately from the others (currently they are all styled by this css called tipsy). However, I don't know how exactly would I do that.



circles.append("title")
.text(function(d) { return d.country + "<br/> " + "Price=" + d.trust
+ ", Value=" + d.business + "<br/> " + d.gdppc; });

$(".circles").tipsy({ gravity:'nw', html: true,});


The idea is probably to make gdppc a separate variable, and style it separately.