Created
July 5, 2017 18:49
-
-
Save jacksonpradolima/afb55d7bf86d1e5902e2442a5ac19478 to your computer and use it in GitHub Desktop.
Google Sheets script to allow export cells to LaTeX tables (adapted from http://alienexp.blogspot.com.br/2012/09/latex-tables-from-google-spreadsheets.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div style="font-family: sans-serif;"> | |
<? var data = generateLaTeXTable(); ?> | |
<textarea rows="20" cols="40" style="overflow:scroll;"><?= data ?></textarea> | |
<br> | |
<input type="button" value="Close" onclick="google.script.host.close()" /> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen(e) { | |
SpreadsheetApp.getUi() | |
.createMenu('Scripts') | |
.addItem('Convert to LaTeX table', 'showLaTeXTableDialog') | |
.addToUi(); | |
} | |
function showLaTeXTableDialog() { | |
var html = HtmlService.createTemplateFromFile('latex_table').evaluate(); | |
SpreadsheetApp.getUi().showSidebar(html); | |
} | |
function generateLaTeXTable(){ | |
try{ | |
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"; | |
return strRows; | |
}catch(e){ | |
return null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For general guidance on setting up a script in Google Sheets, see this quickstart guide.
To use this script:
latexfy.gs
and paste in the contents oflatexfy.gs
. Save the file, File > Save.latex_table.html
and paste in the contents ofdialog.html
. Save the file, File > Save.You can leave the script sidebar open. When you export new cells the script sidebar will be updated.
Many thanks to Dave Rim for creating the original script.