1 Reply Latest reply on Sep 24, 2013 3:06 AM by try67

    How to get a field from an attached excel/csv?

    nepcoproducts

      I currently manage some price lists for the company I work for. They are graphical, and usually created in inDesign and then exported to pdf.

       

      I'm looking for a way to attach our company's prices in an excel sheet, (or delimited file) and be able to pull a price into the pdf.

       

      For example, attaching an excel file with 2 columns, SKU & Price. Then saying, if text field SKU= "APPLE" text field PRICE = "0.50". Only I don't want the user to have any inpurt options, I want to do this myself and then save a flat version to distribute in the company.

       

       

      I've been working on this for a few weeks. My javascript skills are really rough.

       

       

      So far, I have some code in my pdf that looks like this:

       

       

      (Price.csv is an attachment in the PDF)

       

      var oFile = this.getDataObjectContents("Price.csv");

      var sData = util.stringFromStream(oFile, "utf-8");

       

      function ParseData(sData){

       

      var oData = {};

       

      sData.replace(

      new RegExp( "\\[(\\w+)=([^\\]]*)\\]", "gi" ),

      function( $0, $1, $2 ){

      oData[ $1 ] = $2;

      }

      );

       

      return( oData );

      }

       

      // Parse the data into an object

      var oData = ParseData(sData);

       

      // ---------------------------------------- //

       

       

      // Custom Calculate script for price text field

      (function () {

       

          // Get the value of the SKU field

          var sku = getField("SKU").valueAsString;

       

          // Look up the price

          var price = oData[sku];

       

          // Set the value of this field to the price if it's present,

          // otherwise blank this field

          event.value = price || "";

          console.println(price);

       

       

      })();

       

      I have two text fields on the form. One that I have filled in a valid SKU, and a blank one named event.

       

      I seem to be getting an undefined error on prices. I am completely lost as to where to go from here.

       

      Does anyone have an idea of where I may be going wrong? I get that undefined error and nothing appears on my form.

       

      Thank you in advance.

       

      (I have acrobat X pro)

        • 1. Re: How to get a field from an attached excel/csv?
          try67 MVP & Adobe Community Professional

          First of all, "undefined" is not an error. It just says that the script ended without returning any values.

          I would suggest starting by debugging the code that reads the input file (you have to use a plain-text file, by the way, not an Excel spreadsheet). Make sure that oData actually contains the data you expect it to contain. If that works OK, start debugging your calculation script and see what's going on there...