Created
October 21, 2016 11:23
-
-
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
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
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