Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Import from CSV #jarchi
* Import from CSV
* Requires jArchi -
* Requires PapaParse -
* Works with Export to CSV Script -
* Version 1: Import from CSV
* Version 1.1: Force character encoding to use UTF-8
* Version 2: Support for Specialization and creates "CSVImport-timestamp" view
* Version 2.1: Fix for "empty" rows
* (c) 2022 Steven Mileham
var debug = false;;
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.csv" });
if (filePath) {
var FileReader = Java.type("");
var Types = Java.type("java.nio.charset.StandardCharsets");
var theCSVFile = new FileReader(filePath,Types.UTF_8);
var theCSV ="";
var data =;
console.log("> Please Wait...");
while(data != -1) {
var theCharacter = String.fromCharCode(data);
data =;
console.log("> File Loaded");
theDataFile = Papa.parse(theCSV);
theData =;
theDataHeaders = theData[0];
var newGroupStart = 48;
var nameGroupStart = 180;
var uidGroupStart = 312;
var maxPerRow = 10;
var newGroupCount=0;
var nameGroupCount=0;
var uidGroupCount=0;
var newGroupRow=1;
var nameGroupRow=1;
var uidGroupRow=1;
var theDate = new Date();
var theView = model.createArchimateView("CSVImport-"+theDate.toString());
var templateObject = theView.createObject("note", 0, 0, -1, -1);
var templateHeight = templateObject.bounds.height;
var templateWidth = templateObject.bounds.width
var titleNote = theView.createObject("note", 12, 12, maxPerRow*templateWidth, 24);
titleNote.text = "Import from:"+filePath+" on "+theDate.toString();
var newGroup = theView.createObject("diagram-model-group", 12, newGroupStart, maxPerRow*templateWidth, templateWidth); = "New Elements";
var nameGroup = theView.createObject("diagram-model-group", 12, nameGroupStart, maxPerRow*templateWidth, templateWidth); = "Matched by Name";
var uidGroup = theView.createObject("diagram-model-group", 12, uidGroupStart, maxPerRow*templateWidth, templateWidth); = "Matched by UID";
var commonProperties = ["UID","Name", "Documentation","Type","Specialization"];
for (var i=1; i<theData.length; i++) {
var matchType = 3;
var theConcept = null;
var theObject = [];
for (var j=0; j<theDataHeaders.length; j++) {
debug? console.log(theObject):true;
if (theObject.Name!=null && theObject.Name.trim()!="" && theObject.Name.trim()!="null") {
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;;
if (theObject.Specialization!="")
theSpecializations = model.specializations;
for (var j=0; j<theSpecializations.length; j++) {
debug? console.log("> Checking Spec " + theSpecializations[j]):true;
if (theSpecializations[j].name==theObject.Specialization && theSpecializations[j].type == theObject.Type)
debug? console.log("> Setting Spec " + theConcept.specialization):true;
if (theConcept.specialization==null)
debug? console.log("> Creating Spec " + theObject.Specialization):true;
model.createSpecialization(theObject.Specialization, theObject.Type);
for (var j=0; j<theDataHeaders.length; j++) {
switch (theDataHeaders[j]) {
case "UID":
case "Name":
case "Documentation":
case "Type":
case "Specialization":
if (theObject[theDataHeaders[j]]) {
else {
newGroup.bounds = {x:newGroup.bounds.x, y:newGroup.bounds.y, width:maxPerRow*templateWidth,height:(templateHeight+12)*newGroupRow+24};
nameGroup.bounds = {x:nameGroup.bounds.x, y:newGroup.bounds.y+newGroup.bounds.height+12, width:maxPerRow*templateWidth,height:(templateHeight+12)*nameGroupRow+24};
uidGroup.bounds = {x:uidGroup.bounds.x, y:nameGroup.bounds.y+nameGroup.bounds.height+12, width:maxPerRow*templateWidth,height:(templateHeight+12)*uidGroupRow+24};
switch (matchType) {
case 3:
if (uidGroupCount>=maxPerRow)
theGroupStart = uidGroup.bounds.y;
theGroupCount = uidGroupCount;
theGroupRow = uidGroupRow;
case 2:
debug? console.log("> Name Group Width " +nameGroup.bounds.width):true;
if (nameGroupCount>=maxPerRow)
theGroupStart = nameGroup.bounds.y;
theGroupCount = nameGroupCount;
theGroupRow = nameGroupRow;
debug? console.log("> NEW Name Group Width " +nameGroup.bounds.width):true;
case 1:
if (newGroupCount>=maxPerRow)
theGroupStart = newGroup.bounds.y;
theGroupCount = newGroupCount;
theGroupRow = newGroupRow;
var theViewObject = theView.add(theConcept, (theGroupCount*templateWidth)-(templateWidth-12)+(theGroupCount*12), theGroupStart+12+((theGroupRow-1)*templateHeight)+(theGroupRow*12), -1, -1, true);
console.log("> Parsing Complete")
else {
console.log("> Cancelled");
Copy link

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?

Copy link

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.

Copy link

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?

Copy link

smileham commented Jun 6, 2020

Check out for example CSVs, also you can run the Export to CSV script on a view to generate your own

Copy link

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.

Copy link

degjerken commented Jul 28, 2021

Hi Smileham,
Tested out your script - very nice functionality :-). Thanks!
Have however found a small issue with the importing as it turns out to not being able to handle characters such as 'æ', 'ø', 'å', etc. Do you know of any way to address this issue? The export function treats these letters just fine. Has it perhaps something to do with the papaparser?

Copy link

smileham commented Jul 28, 2021

Well spotted, I've updated it to load the file using UTF-8, try it now! @degjerken

Copy link

degjerken commented Jul 29, 2021

Thanks a lot, @smileham! This update made my day ;-)

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