Book a Demo

Author Topic: Example of using JScript-CSV in EAScripLib  (Read 16098 times)

Dung

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Example of using JScript-CSV in EAScripLib
« on: April 01, 2014, 11:02:35 am »
I have a need to write custom CSV Export using JScript or VBScript. The JScript-CSV library for CSV EXPORT has:

1. function CSVEExportInitialize( fileName /* : String */, columns /* : Array */, exportColumnHeadings /* : boolean */ )
    How is the columns variable declared and assigned?

2. function CSVEExportRow( valueMap /* : Scripting.Dictionary */ ) /* : void */

   How is the valueMap declared in the calling function?

Has anyone used the above functions for export I can use for reference?

Your help is greatly appreciated.

Thanks,
Dung
« Last Edit: April 01, 2014, 11:04:12 am by dungvandang »
Dung Dang

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Example of using JScript-CSV in EAScripLib
« Reply #1 on: April 01, 2014, 12:19:25 pm »
Here is some JScript code I wrote a while ago which exports a basic data dictionary to CSV.

Code: [Select]
!INC Local Scripts.EAConstants-JScript
!INC EAScriptLib.JScript-CSV
!INC EAScriptLib.JScript-Dialog

/*
 * Script Name: Export Tables to CSV
 * Author: Aaron Bell
 * Purpose: For use as a Project Browser script type.  Exports all tables
 *   in the currently selected package to CSV format.
 * Date:
 */

function main()
{
      var fileName = SaveCSVFileDialog();
      
      //abort if no file was specified
      if ( fileName == null || fileName == "" )
            return;
      
      //the currently selected package in the Project Browser will be used to contain the new elements that are imported
      targetPackage = Repository.GetTreeSelectedPackage();
      
      Session.Output("Exporting to file: " + fileName);
      
      var exportColumnHeadings = true;
      var columns, rowCount;
      
      //columns are comma-delimited
      CSV_DELIMITER = ",";

      columns = Array(6);
      columns[0] = "TableName";
      columns[1] = "Column";
      columns[2] = "Type";
      columns[3] = "Length";
      columns[4] = "PrimaryKey";
      columns[5] = "ForeignKey";
      
      CSVEExportInitialize( fileName, columns, exportColumnHeadings )
      rowCount = doExport(targetPackage);
      CSVEExportFinalize()

      Session.Output("Done.  " + rowCount + " rows exported.");
      
}

function doExport( targetPackage ) {
      var element as EA.Element;
      var attribute as EA.Attribute;
      var x, y;
      var rowCount;

      rowCount = 0;
      
      for ( x = 0; x < targetPackage.Elements.Count; x++ )
      {
            element = targetPackage.Elements.GetAt(x);
            if ( element.Type == "Class" && element.Stereotype == "table" && element.Attributes.Count > 0 )
            {
                  for ( y = 0; y < element.Attributes.Count; y++ )
                  {
                        attribute = element.Attributes.GetAt(y);

                        var valueMap = new ActiveXObject( "Scripting.Dictionary" );
                        valueMap.add( "TableName", element.Name );
                        valueMap.add( "Column", attribute.Name );
                        valueMap.add( "Type", attribute.Type );
                        valueMap.add( "Length", attribute.Length );
                        valueMap.add( "PrimaryKey", attribute.IsOrdered );
                        valueMap.add( "ForeignKey", attribute.IsCollection );
                        CSVEExportRow( valueMap );
                        rowCount++;
                  }
            }
      }
      
      return rowCount;
}

function SaveCSVFileDialog()
{
      var Project;
      var Filename, FilterString, Filterindex, Flags, InitialDirectory, OpenorSave, filepath;

      Filename = "";
      FilterString = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||";
      Filterindex = 1;
      Flags = 0x2; //OFN_OVERWRITEPROMPT
      InitialDirectory = "";
      OpenorSave = 1;
      
      Project = Repository.GetProjectInterface();
      filepath = Project.GetFileNameDialog(Filename, FilterString, Filterindex, Flags, InitialDirectory, OpenorSave);
      
      return filepath;
}

main();

Dung

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Example of using JScript-CSV in EAScripLib
« Reply #2 on: April 02, 2014, 05:22:45 pm »
This works well. Thanks Aaron.

However, I am running into another wall in which there are elements inside element of the same level of the package. I can't seem to drill down to the elements within element of the package. I have also looked at the JScript Recursive Element Counrt Example, but that example does not drill down to another level of element containing elements. I would like to see another example of recursive element count of the elements.

Thanks in advance!

Dung
Dung Dang

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Example of using JScript-CSV in EAScripLib
« Reply #3 on: April 03, 2014, 10:24:08 am »
To get child elements, you need to recurse down the Element.Elements collection.

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Example of using JScript-CSV in EAScripLib
« Reply #4 on: April 10, 2014, 01:23:25 pm »

rjheward

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Example of using JScript-CSV in EAScripLib
« Reply #5 on: December 06, 2018, 10:30:44 pm »
This is a dead link thesedays.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Example of using JScript-CSV in EAScripLib
« Reply #6 on: December 07, 2018, 02:07:15 am »
Well, after 4 years... I guess it would not be very helpful anyway.

q.

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Example of using JScript-CSV in EAScripLib
« Reply #7 on: December 07, 2018, 08:39:54 am »
Pretty sure that link was referring to one of these posts... (One is an Import example, the other is an Export example)
http://sparxsystems.com/forums/smf/index.php/topic,25388.msg201695.html#msg201695
http://sparxsystems.com/forums/smf/index.php/topic,25389.msg201697.html#msg201697

(The link is different now due to our forum system changing sometime over the last few years.)