Skip to content

Instantly share code, notes, and snippets.

@tkazimie
Last active June 14, 2022 14:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkazimie/f5443ac70113b77363ad4a41985ddd98 to your computer and use it in GitHub Desktop.
Save tkazimie/f5443ac70113b77363ad4a41985ddd98 to your computer and use it in GitHub Desktop.
#jArchi This scripts exports selected elements to the CSV in fixed structure and import elements to the model and the current view
/*
* Import Elements from CSV
*
* Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
* Requires PapaParse - https://www.papaparse.com/
*
* This script imports CSV containg followin structure:
* * Name
* * Documentation
* * Type (element type)
* * Properties
* as a result new elements are created in the model and put to the current view stackig from position 10,10.
* Before running the script you must choose the view.
* Script does not check whenever elements existed in the model.
*
* Input file could be created manually or generated by coresponding scrip toCsv.
*
* Version 1: Import elements from CSV list
* Version 2: Add properties support
*
* 2020 by tkazimie@me.com
*
* This script was creaed based on "Import from CSV" by Steven Mileham
*/
var debug = false;//true;//false;
console.clear();
var theConcept = null;
load(__DIR__ + "lib/papaparse.min.js");
debug? console.log("> Loaded Papa Parse"):true;
var theCurView = $(selection).filter("archimate-diagram-model").first();
if (theCurView != null) {
var filePath = window.promptOpenFile({ title: "Open CSV", filterExtensions: ["*.CSV"], fileName: "default.archimate" });
if (filePath) {
var FileReader = Java.type("java.io.FileReader");
var theCSVFile = new FileReader(filePath);
var theCSV ="";
var data = theCSVFile.read();
console.log("> Please Wait...");
while(data != -1) {
var theCharacter = String.fromCharCode(data);
theCSV+=theCharacter;
data = theCSVFile.read();
}
theCSVFile.close();
console.log("> File Loaded");
theDataFile = Papa.parse(theCSV);
console.log("> File Parced");
theData = theDataFile.data;
theDataHeaders = theData[0];
debug? console.log("> header:"+theDataHeaders):true;
var commonProperties = ["Name", "Documentation","Type"];
var k=1;
for (var i=1; i<theData.length; i++) {
debug? console.log("> data row:"+theData[i]):true;
var theObject = [];
for (var j=0; j<theDataHeaders.length; j++) {
theObject[theDataHeaders[j]]=theData[i][j];
debug? console.log("> data:"+theData[i][j]):true;
}
if (theObject.Name || theObject.Name.length > 1 ) {
debug? console.log("> Creating Concept["+i+"]:"+theObject.Name):true;
try {
var theElem = model.createElement(theObject.Type,theObject.Name);
}
catch(err) {
theElem=null;
}
if (theElem) {
theElem.documentation=theObject.Documentation;
for (var j=0; j<theDataHeaders.length; j++) {
switch (theDataHeaders[j]) {
case "Name":
case "Documentation":
case "Type":
break;
default:
if (theObject[theDataHeaders[j]]) {
theElem.prop(theDataHeaders[j],theObject[theDataHeaders[j]]);
}
}
}
debug? console.log("> Element created:["+i+"]"+theObject.Name):true;
var theBox=theCurView.add(theElem, 10+k*10, 10+k*10, 150, 75);
if (!theBox) {
console.log("> Cannot create box for:"+i+theObject.Name);
}
else
{
debug? console.log("> Box created: "+theBox):true;
k++;
}
}
else
{
console.log("Skipping line - Cannot create:"+theObject.Name+":"+theObject.Type);
}
}
else {
console.log("Skipped nameless row.");
}
}
}
else
{
window.alert("File open canceled.");
}
}
else {
window.alert("Please Select a the view for elements creation before running the script.");
}
console.log("End fromCsv Script");
/*
* Export selected elements to CSV
*
* Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
* Requires PapaParse - https://www.papaparse.com/
*
* This script exports selected elements to the CSV in fixed structure:
* * Name
* * Documentation
* * Type (element type)
* * <existing properties (if defined)>
* to the selectd file.
*
* Before running the script you must select some elements in the tree or the view.
*
* Output file could used by coresponding scrip fromCsv as well the file coule be used as a
* template for manual file creation
*
*
* Version 1: Export selected elements to CSV
* Version 2: Add properties support
*
* 2020 by tkazimie@me.com
*
* This script was creaed based on "Export from CSV" by Steven Mileham
*/
var debug = false;//true;//false;
load(__DIR__ + "lib/papaparse.min.js");
debug? console.log("> Loaded Papa Parse"):true;
console.clear();
console.log("New toCsv Script");
// Set up Header
var conceptHeaders = [
"Name",
"Documentation",
"Type"
];
var theData = new Array();
var propertiesList = [];
$(selection).each(function(theConcept) {
debug? console.log("Elem name:"+theConcept.name+" type:"+theConcept.type):true;
if (theConcept.type != "archimate-diagram-model") {
var theProperties = theConcept.prop();
for (var i=0; i<theProperties.length; i++){
var found = false;
for (var j=0; j<propertiesList.length; j++) {
if (propertiesList[j]==theProperties[i]) {
found=true;
}
}
if (!found) {
propertiesList.push(theProperties[i]);
conceptHeaders.push(theProperties[i]);
}
}
var theObject = new Object;
theObject["Name"]=theConcept.name;
theObject["Documentation"]=theConcept.documentation;
theObject["UID"]=theConcept.id;
theObject["Type"]=theConcept.type;
for (var i=0; i<propertiesList.length; i++){
if (theConcept.prop(propertiesList[i])) {
theObject[propertiesList[i]]=""+theConcept.prop(propertiesList[i]);
}
}
theData.push(theObject);
}
});
// Open a dialog to let the user choose where to save the generated file
var defaultFileName = model.name ? model.name + ".csv" : "Exported Model.csv"; // Default file name
var exportFile = window.promptSaveFile({ title: "Export to CSV", filterExtensions: [ "*.csv" ], fileName: defaultFileName } );
var theCSV = Papa.unparse({fields:conceptHeaders, data:theData});
if(exportFile != null) {
debug? console.log("> TheCSV"):true;
debug? console.log(theCSV):true;
$.fs.writeFile(exportFile, theCSV);
console.log("> Export done");
}
else {
window.alert("File open canceled.");
}
console.log("End toCsv Script");
@tkazimie
Copy link
Author

You are welcome :-) Please note this is very much based on "Import from CSV" by Steven Mileham with only small modifications.

The way to debug I used is console.log as you can see above example. This prints text on console. Console you can turn on in Archimate script window using "Show console" button.

@jsandman
Copy link

Thank you so much for sharing. This is great!!!

Do you plan to update the script so that you can re-import what was exported and upsert any existing elements?

I am trying to export a lot of elements and update them in excel, then re-import the elements back into the model without creating duplicates.

@Erthengs
Copy link

Erthengs commented May 8, 2021

@jsandman A long time ago, I built in Excel vba (I am not a professional programmer) exactly this, allowing me to analyse, update and add elements and properties in bulk, and convert it into regular import csv's for Archi. This has saved me many hours over the years. I sincerely hope that this great archi javascript will be further developed so that I can throw my Excel away; if you want to have it in the mean time, let me know.

Thank you so much for sharing. This is great!!!

Do you plan to update the script so that you can re-import what was exported and upsert any existing elements?

I am trying to export a lot of elements and update them in excel, then re-import the elements back into the model without creating duplicates.

@trischbeck
Copy link

@Erthengs: I'm struggling since quite a while with exactly this problem - and have a very error-prone Archi - CSV - Excel roundtrip working. I'd be very much interested in your Excel VBA to alleviate my woes :-)

Kind regards, Thomas

@jsandman A long time ago, I built in Excel vba (I am not a professional programmer) exactly this, allowing me to analyse, update and add elements and properties in bulk, and convert it into regular import csv's for Archi. This has saved me many hours over the years. I sincerely hope that this great archi javascript will be further developed so that I can throw my Excel away; if you want to have it in the mean time, let me know.

Thank you so much for sharing. This is great!!!
Do you plan to update the script so that you can re-import what was exported and upsert any existing elements?
I am trying to export a lot of elements and update them in excel, then re-import the elements back into the model without creating duplicates.

@smileham
Copy link

Hey @tkazimie, just to let you know, someone pointed out a bug in my original "Import from CSV" script where it was breaking on special characters. I've updated it to now support UTF-8 on import. https://gist.github.com/smileham/1e57a5946235e780dee5a824f664aa3d

@smileham
Copy link

smileham commented Jun 14, 2022

Just FYI folks, I've updated the original to now import/export the Specializations and support for creating a View to highlight those Elements which have been imported. https://gist.github.com/smileham/1e57a5946235e780dee5a824f664aa3d and https://gist.github.com/smileham/15c445b17a92bd6f5dc1508e573bcd8a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment