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

New Here ,
Sep 01, 2017

Copy link to clipboard

Copied

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"});

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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...

TOPICS
Acrobat SDK and JavaScript, Windows

Views

789

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

New Here ,
Sep 01, 2017

Copy link to clipboard

Copied

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"});

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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...

TOPICS
Acrobat SDK and JavaScript, Windows

Views

790

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 01, 2017 0
Most Valuable Participant ,
Sep 01, 2017

Copy link to clipboard

Copied

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...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2017 0
New Here ,
Sep 01, 2017

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2017 0
try67 LATEST
Most Valuable Participant ,
Sep 01, 2017

Copy link to clipboard

Copied

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>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2017 0