Hi Team ,
I need to read .csv file which present in my local machine and create ticket based on that
can you please give idea how can i implement this through script runner or any other way
Thanks in advance .
When I need to read a csv for anything, I do it like this:
import com.opencsv.CSVReader@Grapes(@Grab(group='com.opencsv', module='opencsv', version='4.2'))def filePath = '/path/to/file/test.csv'def reader = new CSVReader(new FileReader(new File(filePath)))def data= reader.collect { it }.with { rows -> def header = rows.head()def dataRows = rows.tail() dataRows.collect { row -> [header, row].transpose().collectEntries()}}data.each{row-> // access any data from the row by the name of the header // for example, row['colHeader'] or row.colHeader}
Hi @PD Sheehan ,
Thanks for getting back .
can i put this csv file in my local machine because i d not have server access
The file has to be available at the file system level.
So if you don't have access to that, an option is to put your csv in a google sheet.
Then in sheets, go to file/publish to the web. Follow the instructions and get the URL for accessing the sheet as a csv.
Then in scriptrunner you can access it with
import groovyx.net.http.ContentTypeimport groovyx.net.http.HTTPBuilderimport groovyx.net.http.Methodimport com.opencsv.CSVReader@Grapes( @Grab(group='com.opencsv', module='opencsv', version='4.2'))def url = 'url for your google sheet csv'def httpBuilder = new HTTPBuilder(url)def csvReaderdef datahttpBuilder.request(Method.GET, ContentType.TEXT) { response.success = {resp, text -> csvReader = new CSVReader(text).readAll() } response.failure = {resp, text -> log.error "Unable to read csv from \n url: $url \nError: $text" }}if(csvReader) { data = csvReader.collect { it }.with { rows -> def header = rows.head() def dataRows = rows.tail() dataRows.collect { row -> [header, row].transpose().collectEntries() } }}
Thanks again for your reply but unfortunately getting below error while running this code in script runner
The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: General error during conversion: Error grabbing Grapes -- [unresolved dependency: com.opencsv#opencsv;4.2: not found] java.lang.RuntimeException: Error grabbing Grapes -- [unresolved
@PD Sheehan : Thanks, works for me!
It looks like you're new here. Sign in or register to get started.