Saturday, September 29, 2012

Latex Tables from Google Spreadsheets

Basically, I'm writing my thesis now, and so I needed to make a lot of latex tables. I have R->latex and matlab->latex scripts lying about, but as I've lost the inclination to redo experiments, I find it easier to just use the existing latex tables in my documents. However, reformatting them is kind of a pain.

So I would generally use Excel for this, but I find Google spreadsheets to be easier to use because I'm using more than 2 computers more often these days. I hate having to copy things back and forth and as friendly as google-drive is, Excel is less so. My workstation in the lab, for example is the French language version, which I can barely use (the commas kill me.)

Anyways so here it is, Google docs spreadsheets have a Javascript runtime that is exposed much like VBA. You can use it to write custom functions and other things. I wrote a handy button that will take my selected range and output a latex table.

Once you have your sheet filled out, you can then hit Tools->Script Manager from the menu bar. Hit "spreadsheet."

In another browser window, the javascript editor will show up with a handy example provide by Google. Here's what I did with mine.

 function latexify() {  
  var range = SpreadsheetApp.getActiveRange();  
  var numRows = range.getNumRows();  
  var numCols = range.getNumColumns();  
  var values = range.getValues();  
  var cs = new Array(numCols);  
  var strRows = "";   
    
  for(var k = 0; k < numCols; k++){  
   cs[k] = 'c';  
  }  
  strRows = "\\begin{table}\n";  
  strRows += "\\centering\n";  
  strRows += "\\begin{tabular}";  
  strRows += "{|" + cs.join("|") + "|}\n"  
  strRows += "\\hline\n";  
    
  for (var i = 0; i <= numRows - 1; i++) {  
   var row = values[i];  
   for (var j = 0; j <= numCols - 1; j++){  
    var cell = row[j];  
    strRows = strRows + cell;  
    if(j < numCols-1)  
     strRows = strRows + " & ";  
   }  
   strRows += "\\\\ \n\\hline\n";  
   }  
  strRows += "\\hline";  
  strRows += "\\end{tabular}\n";  
  strRows += "\\label{table:table}\n";  
  strRows += "\\caption{\\small{}} \n";  
  strRows += "\\end{table}\n";  
   
  Logger.log(strRows);  
 };  

Lovely, isn't it? Choose Run from the menu in the editor, and then View->Logs to show the output. Documentation is available from the Help menu, but I found the API Reference most useful.

13 comments:

John said...

Thank you so much for this script.

Unknown said...

Thanks for this quick hack. I would only suggest moving the \label command after the \caption, as otherwise references to the table will have wrong numbers (as described at http://en.wikibooks.org/wiki/LaTeX/Labels_and_Cross-referencing#Fixing_wrong_labels) - it happened to me.

Also would you consider publishing this in the Script Gallery of Google Docs?

Pablo said...

Thanks a lot!!

Unknown said...

Thanks for this. You saved 2 hours of my life.

Alan Moraes said...

Thank you, Dave!

Pablo said...

Brilliant! Thanks.

Anonymous said...

Wow dude, this is really awesome! Quite a time-saver, I was literally copy-pasting cells from Google Docs into Latex before this LOL.

Phil said...

Brilliant ! Thanks for this, it has opened up a great line ....

Unknown said...

NICE:) Thank you.
I am not really into java, so is it possible to load the displayed number of decimals in the google sheets to latex ( I have used the 'decrease decimal places' since my results have way to many decimals, but I am getting all the decimals into my latex document).

Unknown said...

AWESOMEEEE!

George Pikoulis said...

Total respect! I'm on a few days deadline and this post saved my ass!

Unknown said...

I fucking love the internet.

Unknown said...

any chance we could get this to spit out a tex file. It would be super helpful when we have a few tens of tables...