Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import from CSV #jarchi
/*
* Import from CSV
*
* Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
* Requires PapaParse - https://www.papaparse.com/
* Works with Export to CSV Script - https://gist.github.com/smileham/15c445b17a92bd6f5dc1508e573bcd8a
*
* Version 1: Import from CSV
*
* (c) 2018 Steven Mileham
*
*/
var debug = true;
console.show();
console.clear();
console.log("> Import CSV");
load(__DIR__ + "lib/papaparse.min.js");
console.log("> Loaded Papa Parse");
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);
theData = theDataFile.data;
theDataHeaders = theData[0];
var commonProperties = ["UID","Name", "Documentation","Type"];
for (var i=1; i<theData.length-1; i++) {
var theConcept = null;
var theObject = [];
for (var j=0; j<theDataHeaders.length; j++) {
theObject[theDataHeaders[j]]=theData[i][j];
}
theConcept = $("#"+theObject.UID).first();
if (!theConcept) {
debug? console.log("> Missing UID, checking Name"):true;
theConcept = $("."+theObject.Name).first();
if (!theConcept || theConcept.length>1) {
debug? console.log("> Creating Concept"):true;
theConcept = model.createElement(theObject.Type,theObject.Name);
}
}
debug? console.log(theConcept):true;
theConcept.name=theObject.Name;
theConcept.documentation=theObject.Documentation;
theConcept.type=theObject.Type;
for (var j=0; j<theDataHeaders.length; j++) {
switch (theDataHeaders[j]) {
case "UID":
case "Name":
case "Documentation":
case "Type":
break;
default:
if (theObject[theDataHeaders[j]]) {
theConcept.prop(theDataHeaders[j],theObject[theDataHeaders[j]]);
}
else {
theConcept.removeProp(theDataHeaders[j]);
}
}
}
}
console.log("> Parsing Complete")
}
else {
console.log("> Cancelled");
}
@rich-biker

This comment has been minimized.

Copy link

@rich-biker rich-biker commented Mar 28, 2019

Hi Smileham. Thanks for this excellent script. I'm just wondering why you declared the commonProperties list as you don't reference it anywhere? Am I missing something?

@smileham

This comment has been minimized.

Copy link
Owner Author

@smileham smileham commented Jan 29, 2020

(Better late than never)
You're right, I think at some point I was looping through these to "ignore" them when writing the properties out, and then ended up moving to a case statement.

@rmurray68

This comment has been minimized.

Copy link

@rmurray68 rmurray68 commented Jun 5, 2020

When doing an import from a CSV file, I keep getting Missing UID message. It then creates a random one in Archi. I am looking for a way to create pre-defined UID during initial import for consistent update and sync from other sources. Can you share a sample CSV that is formatted correctly?

@smileham

This comment has been minimized.

Copy link
Owner Author

@smileham smileham commented Jun 6, 2020

Check out https://gist.github.com/smileham/1932ae02fc481e339629dff4e3fbd7f0 for example CSVs, also you can run the Export to CSV script on a view to generate your own https://gist.github.com/smileham/15c445b17a92bd6f5dc1508e573bcd8a

@rmurray68

This comment has been minimized.

Copy link

@rmurray68 rmurray68 commented Jun 6, 2020

Thank you. I think I figured it out. The "Missing UID" is because it does not already exist in Archi. And from reading on the scripting language, I do not see any arguments on the Model Create Element to allow for specifying a UID. It appears you would need to do an initial load of the elements using the import feature in Archi first and the CSV format it needs. That does pre-create them. Then use them with your script to populate the properties. Thanks for your script. I think it will help me to structure things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.