Example of using JScript-CSV in EAScripLib

556 views
Skip to first unread message

Dung

unread,
Mar 31, 2014, 9:02:00 PM3/31/14
to sparx-enterprise-archite...@googlegroups.com
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

[original message]

B@localhost Aaron B

unread,
Mar 31, 2014, 10:19:00 PM3/31/14
to sparx-enterprise-archite...@googlegroups.com
Here is some JScript code I wrote a while ago which exports a basic data dictionary to CSV.


Code:
!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();

[original message]

Dung

unread,
Apr 2, 2014, 3:22:00 AM4/2/14
to sparx-enterprise-archite...@googlegroups.com
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

[original message]

B@localhost Aaron B

unread,
Apr 3, 2014, 8:24:00 AM4/3/14
to sparx-enterprise-archite...@googlegroups.com
To get child elements, you need to recurse down the Element.Elements collection.
[original message]

B@localhost Aaron B

unread,
Apr 10, 2014, 12:23:00 AM4/10/14
to sparx-enterprise-archite...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages