Skip to Content
0
Jan 08, 2023 at 06:12 AM

Picking a particular column values in CSV using groovy

369 Views

Hi,

The interface which has a CSV file coming from the SFTP has Multiple rows and columns where I'm in need of to pick the particular columns values(for eg: effective start date, termination date etc.) with the date formats to change their formats.

So, I thought of using a groovy instead of converting them into xml and message mapping. And I have a query here that in a CSV file how to pick up all rows with the the column values which has dates/particular columns using groovy script. I have drafted a script which I'm facing some issues while simulating it.

The code script is:

import com.sap.it.api.mapping.*;
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
def Message processData(Message message)
{
def body = message.getBody(String)
def rows = message.readLines().tail()*.split('\n')
def arg1= message.getBody(body.rows*.getAt(11))

Date inputDate_parsed=new SimpleDateFormat("dd/MM/yyyy").parse(arg1);
DateFormat dateFormat_required = new SimpleDateFormat("yyyy-MM-dd");
def converted_datetime=dateFormat_required.format(inputDate_parsed);
message.setBody(converted_datetime)

message.setBody(body.join('\n'))
return message
}

I have attached an dummy CSV file with the columns names which has date columns named as Effective start date, start date, continuous start date and termination date.

File 1st row:

"Employee Number","Employee First name","Employee Last name","Employee Name","Primary Business Email Address","Contact Email Address","Employing Legal Entity","Business Unit","Retail Shop","Person Type","Cost Centre","Continuous Start Date","Start Date","Effective Start Date","Termination Date","Manager ID"

image.pngimage.png

So im in need to pick those columns values from this csv file.

Thanks,

Yogesh Kumar

Attachments

image.png (10.4 kB)