How to Add a new field to a CSV file (preprocessor)

hi,

in the “How-Tos” “Using Preprocessors in DataMapper” you describe that there is a possibility to add a new field to a csv via preprocessor. Do you have a code for that?

regards!

Something like this would do the trick:

var tmpFile = createTmpFile();
var originalFile = openTextReader(data.filename);
var modifiedFile = openTextWriter(tmpFile.getPath());

fieldSeparator = ",";

while((line = originalFile.readLine())!= null){
 newFieldValue = "some value to add";
 modifiedFile.write(line + fieldSeparator + newFieldValue + "\n");
}
modifiedFile.close();
originalFile.close();
deleteFile(data.filename);
tmpFile.move(data.filename);

I suspect you’d want to convert the CSV into JSON so you can natively add/remove data. Then when you’re done, convert it back out to CSV.

Here are some examples of pre-existing code that you might draw inspiration from:

https://jsfiddle.net/sturtevant/AZFvQ/