Splitting a string in Google Spreadsheet

Here is how to split a string into tokens, then grab each token for each column in Google Spreadsheet.

So we start with the string.

splitting_in_trix_1

Then we do =index(split(A2, ” “), 1, 1). Basically, split will give us an array (or range). Then we select the first row (there is just one resulting row), and we select the first column (there are 3 resulting columns in our example).

splitting_in_trix_2

Similarly we do =index(split(A2, ” “), 1, 2) to pick the second token.

splitting_in_trix_3

Lastly do =index(split(A2, ” “), 1, 3) to pick the 3rd token.

splitting_in_trix_4

This is similar to explode in PHP, and string split in Python. =)

Get url parameters with javascript

There are times you will need to grab url parameters, then update some of your form components accordingly. In my case, it was a search form that passes search terms using GET.

I largely got the javascript function from here, but I added a few lines to decode/unescape values.

Here it is.

function getUrlParameter(sParam) {
  var sPageURL = window.location.search.substring(1);
  var sURLVariables = sPageURL.split('&');
  for (var i = 0; i < sURLVariables.length; i++) {
    var sParameterName = sURLVariables[i].split('=');
    if (sParameterName[0] == sParam) {
      var val = sParameterName[1];
      //console.log("before: " + val)
      val = decodeURIComponent(val);
      val = val.replace(/+/g, " ");
      //console.log("after: " + val)
      return val;
    }
  }
};

$(function() {
  // pre-set our search control components based on the URL params
  $("#search_text").val(getUrlParameter("search_text"));
  // more fields...
});

Install MySQLdb on osx

Here is the error message I got.

ImportError: No module named MySQLdb

Below is how you install such module for your python script.

brew install mysql
brew install python
sudo easy_install pip
sudo pip install MySQL-python

Depending on your file permissions at various directories, you can determine the use of sudo.

PHP functions in Python?

Coming from the PHP background, very often I wonder if there are equivalent function calls in Python. Say for example, how do you do file_get_contents in Python? I find myself doing a lot of these searches lately.

Here is this website php2python that tells you just that, with code examples.

deobfuscate js

Just found this nice tool online to deobfuscate js code. It works kinda like PrettyPrint. Give it a try! =)

http://jsnice.org/

And here is a command line version of it: https://github.com/brettlangdon/jsnice

Commit all deleted files in git

I had like 100+ files deleted from upgrading to a newer version of wordpress. Below is how to batch commit those deleted files in git.

git add -u

That will stage all those deleted files (that have been tracked before).

Ref: http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git

vis.js

Here is a fun graphing library to play with! =)

Vis.js is a dynamic, browser based visualization library. The library is designed to be easy to use, to handle large amounts of dynamic data, and to enable manipulation of and interaction with the data. The library consists of the components DataSet, Timeline, and Graph.

#mygraph {
width: 400px;
height: 400px;
border: 1px solid lightgray;
}

/js/vis.js

// create an array with nodes
var nodes = [
{id: 1, label: ‘Node 1’},
{id: 2, label: ‘Node 2’},
{id: 3, label: ‘Node 3’},
{id: 4, label: ‘Node 4’},
{id: 5, label: ‘Node 5’}
];
// create an array with edges
var edges = [
{from: 1, to: 2},
{from: 1, to: 3},
{from: 2, to: 4},
{from: 2, to: 5}
];
// create a graph
var container = document.getElementById(‘mygraph’);
var data = {
nodes: nodes,
edges: edges
};
var options = {};
var graph = new vis.Graph(container, data, options);

/js/vis.js // create an array with nodes var nodes = [ {id: 1, label: 'Node 1'}, {id: 2, label: 'Node 2'}, {id: 3, label: 'Node 3'}, {id: 4, label: 'Node 4'}, {id: 5, label: 'Node 5'} ]; // create an array with edges var edges = [ {from: 1, to: 2}, {from: 1, to: 3}, {from: 2, to: 4}, {from: 2, to: 5} ]; // create a graph var container = document.getElementById('mygraph'); var data = { nodes: nodes, edges: edges }; var options = {}; var graph = new vis.Graph(container, data, options);