Been racking my brains for a couple days going through the forums looking for a way to do what I thought would be a simple task. That's to read a CSV file attached to a ticket then parse it down into something useable.
Finally came up with the following solution that I'm sure could be finessed but works. Thought I would share here incase anyone else ran into a similar need.
import com.mindprod.csv.CSVReader
import com.atlassian.jira.issue.attachment.Attachment
Map csvFromAttachment (Attachment attachment, Boolean hasHeaders = false, char delim = ',', char quote = '\"', Boolean allowMultiLine = true, Boolean trim = true) {
Map res = [headers: null, lines: []]
Integer cnt = 0
attachment.withInputStream { stream -> {
CSVReader reader = new CSVReader(stream.newReader(),delim, quote, allowMultiLine, trim)
try {
while (reader) {
if (cnt == 0 && hasHeaders) res.headers = reader.getAllFieldsInLine()
else (res.lines as List).add(reader.getAllFieldsInLine())
cnt++
}
} catch (EOFException e) {
reader.close()
} catch (e) {
// Handle later
}
}}
return res
}
Could you elaborate a little bit more? Do you intend to read through the CSV file and pass the value to the fields?
I am looking forward to your feedback and clarification.
Thank you and Kind regards,
Ram
For this particular instance I was looking to parse an attachment object into a map that could be utilized by other functions. The above code is functional and works as intended. Apologies if this was not the best place to post the solution but I wanted the community to have access to the solution in case a similar need arose. There was no straight forwards solution on forums and many of the links in the older posts are broken.
Simply sharing a solution that I'm sure individuals can use and / or improve upon. If this needs moved or removed that's fine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.