Import elements/properties from XLSX-file

Started by Franky, August 31, 2023, 07:19:39 AM

Previous topic - Next topic

Franky

Hello everyone,

I am looking for a good (simple) example of a script that allows to read the contents of an XLSX file. The intention is to update the repository and diagrams based on that content.

Thanks in advance for your thoughts.

Important extra info: I am using GraalVM as the JavaScript Engine within JArchi.

Kind regards

Franky

Jean-Baptiste Sarrodie

Hi,

You can have a look at this discussion which also provides a piece of code: https://forum.archimatetool.com/index.php?topic=1110.msg5975#msg5975

Note that this is almost two years old, so it would make sense to also test with the latest version of SheetJS (just in case it now works out of the box).

Regards,

JB
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Franky

Thanks for the quick response Jean-Baptiste.

I will test it out and share the result with you via this way.

Greetings

Franky

Franky

Thanks Jean-Baptiste!

I updated the code you referred to. I'm now using SheetJS (version 0.20.0), the 'full' variant.

SheetJS is available at this link: https://cdn.sheetjs.com/xlsx-0.20.0/package/dist/xlsx.full.min.js

I have tested the code and found it working both in Nashorn ES6 and GraalVM (JavaScript Engine).

Below you can see the used code:

console.clear();
console.show();

// SheetJS doesn't work with Nashorn in ES5 mode
// download link: https://cdn.sheetjs.com/xlsx-0.20.0/package/dist/xlsx.full.min.js
// SheetJS v0.20.0 are known to work with Nashorn in ES6 mode and with GraalVM
load(__DIR__ + 'lib\\SheetJS\\v0.20.0\\xlsx.full.min.js');

let filePath = __DIR__ + 'Test.xlsx';
let workbook = XLSX.read(base64encode(readFileSync(filePath)), { type: 'base64'});

// Dump file content in JSON
workbook.SheetNames.forEach(function(sheetName) {
  var XL_row_object = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
  var json_object = JSON.stringify(XL_row_object);
  console.log(json_object);
})

// ============================================================================

function readFileSync(path) {
  let Files = Java.type("java.nio.file.Files");
  let Paths = Java.type("java.nio.file.Paths");
  return Files.readAllBytes(Paths.get(path));
}

function base64encode(byteArray) {
  let Base64 = Java.type("java.util.Base64");
  return Base64.getEncoder().encodeToString(byteArray);
}

Kind regards

Franky