It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Adaptavist ScriptRunner: Export to CSV

Is there a way we could export Object to CSV in ScriptRunner?

4 answers

what do you mean, when you say object?

I need to pull issue keys and issue history which contains the statuses FROM and TO and also userkey. 

I want to create an object to stitch the properties I need, so I could export them into a CSV file.

1 vote

Steps:

  1. Export the change history to Excel (details)
  2. It should look like the spreadsheet below
  3. Save the Excel spreadsheet to CSV using the Excel feature

Note that it is using the Better Excel Exporter app. It is a paid app and I'm a developer.

jira-issues-selected-fields-with-change-history

assuming you got your "object" ready here's basic concept of how to export it:

def filePath = "/some/path/test.csv"
new File(filePath).createNewFile()
PrintWriter writer = new PrintWriter(filePath, "UTF-8")
def someInfo = [1, 2, 3]
writer.println("head1;head2;head3")
someInfo.each {
writer.println("$it;line;test")
}
writer.close()

This should do it:

// This is the CSV library Atlassian has built into Jira
// See https://wush.net/svn/mindprod/com/mindprod/csv/CSVWriter.java

import com.mindprod.csv.CSVWriter

// Convert a list of maps to a CSV string
def mapsToCSV(mapList) {

// Get keys from all maps and build a header row
def headerSet = new LinkedHashSet()
mapList.each { headerSet.addAll(it.keySet()) }
def headers = headerSet.toArray()

def sw = new StringWriter()
def csv = new CSVWriter(sw)

// Add header row
headers.each {csv.put(it.toString())}
csv.nl()

// Add rows - put in empty string if a header key doesn't exist in a row
mapList.each { row ->
headers.each {
csv.put(row.get(it)?.toString() ?: '')
}
csv.nl()
}
csv.close()
return sw.toString()
}

Example usage:

def myMapList = [[
'key': 'BUSINESS-1',
'summary': 'My first issue',
"components":[{"id":"10000","name":"First component"}]
],[
'key': 'SCRUM-1',
'summary': 'My first backlog item',
'storypoints': 10
]]
String csvString = mapsToCsv(myMapList)

Example output:

key,summary,components,storypoints
BUSINESS-1,"My first issue","[[id:10000, name:First component]]",
SCRUM-1,"My first backlog item",,10

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you