Skip to content

Instantly share code, notes, and snippets.

@siggibjarna
Created October 21, 2016 11:23
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siggibjarna/ca15688a319771a309078fdd4577deee to your computer and use it in GitHub Desktop.
Save siggibjarna/ca15688a319771a309078fdd4577deee to your computer and use it in GitHub Desktop.
DataGrip extractor to copy tab separated query results to paste into Excel with comma decimal separator
SEPARATOR = "\t"
QUOTE = "\""
NEWLINE = System.getProperty("line.separator")
def record(values, valueToString) {
values.eachWithIndex { value, idx ->
def str = valueToString(value)
def q = str.contains(SEPARATOR) || str.contains(QUOTE) || str.contains(NEWLINE)
def n = str.isNumber()
OUT.append(q ? QUOTE : "")
// Replace decimal separator for numbers
.append(n ? str.replace(".", ",") : str.replace(QUOTE, QUOTE + QUOTE))
.append(q ? QUOTE : "")
.append(idx != values.size() - 1 ? SEPARATOR : NEWLINE)
}
}
if (TRANSPOSED) {
def values = new ArrayList<String>[COLUMNS.size()]
COLUMNS.eachWithIndex { col, i -> values[i] = new ArrayList<String>() }
ROWS.each { row -> COLUMNS.eachWithIndex { col, i -> values[i].add(FORMATTER.format(row, col)) } }
COLUMNS.eachWithIndex { _, i -> record(values[i], Closure.IDENTITY) }
}
else {
// Column Headers
record(COLUMNS, { it.name() })
// Row Values
ROWS.each { row -> record(COLUMNS, { FORMATTER.format(row, it) }) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment