Skip to content

Instantly share code, notes, and snippets.

@cfriedline
Created November 14, 2013 21:07
Show Gist options
  • Save cfriedline/7474326 to your computer and use it in GitHub Desktop.
Save cfriedline/7474326 to your computer and use it in GitHub Desktop.
Create LaTeX table from selected text in Google Spreadsheet
function latexify() {
var app = UiApp.createApplication();
height = "400"
width = "500"
app.setHeight(height)
app.setWidth(width)
app.setTitle("LaTeXified!")
panel = app.createVerticalPanel()
panel.setHeight("350");
panel.setWidth(width);
ta = app.createTextArea();
ta.setName("ta").setId("ta");
ta.setSize(width, "350");
panel.add(ta);
app.add(panel);
close = app.createButton("Close")
close.setId("close")
horiz = app.createHorizontalPanel()
horiz.add(close)
app.add(horiz)
var doc = SpreadsheetApp.getActive()
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}[ht]\n";
strRows += "\\centering\n";
strRows += "\\begin{tabular}";
//strRows += "{|" + cs.join("|") + "|}\n"
strRows += "{" + cs.join("") + "}\n"
strRows += "\\toprule\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 + " & ";
}
if (i == 0) {
strRows += "\\\\\n\\midrule\n";
} else {
strRows += " \\\\\n"
}
}
strRows += "\\bottomrule\n";
strRows += "\\end{tabular}\n";
strRows += "\\caption{I am the caption} \n";
strRows += "\\label{t:label}\n";
strRows += "\\end{table}\n";
ta.setText(strRows);
ta.setFocus(true);
var closeHandler = app.createServerClickHandler("close");
close.addClickHandler(closeHandler);
doc.show(app)
};
function close(e) {
var app = UiApp.getActiveApplication();
return app.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment