Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.