Skip to main content
Participant
September 1, 2017
Answered

Exporting to CSV, field names and field values in separate columns

  • September 1, 2017
  • 1 reply
  • 2032 views

Hi!

Sorry for this noob-question, but that is what I am.
Regarding Acrobat Pro DC and field data export.

I want to export field data to a CSV, with the field names in one vertical column and the field values in the next vertical column. Like this:

Column A | Column B

Name 1    | Value 1

Name 2    | Value 2

I've been playing with the code below, switching places with ; and /n and /r but nothing gives the output I want.
I can get all data in one vertical column or, with the code below, names and values in two separate vertical rows.

Any tips?

var fieldValues = []; 
var fieldValuesNames = []; 

for (var i = 0; i < this.numFields; i++) 
{
  fieldValues.push(this.getField(this.getNthFieldName(i)).value); 
  var f = this.getField(this.getNthFieldName(i)); 
  fieldValuesNames.push(f.name);   
}
this.createDataObject("output.csv",fieldValuesNames.join(";") + "\r\n" + fieldValues.join(";"));
this.exportDataObject({ cName:"output.csv", nLaunch: "2"});

This topic has been closed for replies.
Correct answer try67

You're composing the same structure as the built-in command, ie the field names in one row and the field values in another, which is not what you want.

What you need to do at the end is iterate over on of the arrays and add the field name and corresponding value to a string, and then write that string to your data object. Actually, an array is not needed at all.

Try this:

var outputString = "";

for (var i = 0; i < this.numFields; i++) {

  var f = this.getField(this.getNthFieldName(i));

  if (f==null || f.type=="button" || f.type=="signature") continue;

  outputString+=f.name + ";" + f.valueAsString + "\r\n";

}

this.createDataObject("output.csv", outputString);

this.exportDataObject({ cName:"output.csv", nLaunch: "2"});

PS. You used a semi-colon as a delimiter. I recommend using a tab ("\t") instead, and saving a tab-delimited text file. Otherwise you can have issues if the field value contains your delimiter and you need to start escaping the text, etc. It's a whole mess...

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
September 1, 2017

You're composing the same structure as the built-in command, ie the field names in one row and the field values in another, which is not what you want.

What you need to do at the end is iterate over on of the arrays and add the field name and corresponding value to a string, and then write that string to your data object. Actually, an array is not needed at all.

Try this:

var outputString = "";

for (var i = 0; i < this.numFields; i++) {

  var f = this.getField(this.getNthFieldName(i));

  if (f==null || f.type=="button" || f.type=="signature") continue;

  outputString+=f.name + ";" + f.valueAsString + "\r\n";

}

this.createDataObject("output.csv", outputString);

this.exportDataObject({ cName:"output.csv", nLaunch: "2"});

PS. You used a semi-colon as a delimiter. I recommend using a tab ("\t") instead, and saving a tab-delimited text file. Otherwise you can have issues if the field value contains your delimiter and you need to start escaping the text, etc. It's a whole mess...

Participant
September 1, 2017

Sooooo many thanks for this!
The tab delimiter does not seem to work for my part. Probably an office issue. Does not separate the fields.
However the ;-delimiter should not be an issue, if it's used all that happens is that a third column appears.

I'm happy!

try67
Community Expert
Community Expert
September 1, 2017

If you change the file format from csv to txt and use tabs, Excel will pick

it up automatically and will separate the values to cells correctly.

On Fri, Sep 1, 2017 at 11:51 AM, rickards14102929 <forums_noreply@adobe.com>