I’m working on a Grails project and I want to write some data from different domain objects to a csv. I thought I would just be able to use the CSV Plugin available. I downloaded and installed the Grails CSV plugin v.3 and to get it working I just thought I would try the example. Unfortunately, the example isn’t working and I’m not sure why. I’ve posted the example below.
def sw = new StringWriter()
def b = new CSVWriter(sw) {
col1 { it.val1 }
col2 { it.val2 }
}
b << [val1: 'a', val2: 'b']
b << [val1: 'c', val2: 'd']
assert b.writer.toString() == '''"col1","col2"
"a","b"
"c","d"'''
It is throwing a compiler error saying unexpected token at col1. Am I doing something wrong here?
I was looking at the export plugin as well, but it doesn’t seem as though I’m able to use data from multiple domain classes with that.
Try changing your code to:
Generally, when the last argument is a closure, you can specify it outside parentheses, but it works only in top-level expressions — in this example the top-level expression is an assignment.