Skip to content

Instantly share code, notes, and snippets.

@Kattoor
Created August 9, 2021 09:20
Show Gist options
  • Save Kattoor/50155a209fae4b19281f219def622b27 to your computer and use it in GitHub Desktop.
Save Kattoor/50155a209fae4b19281f219def622b27 to your computer and use it in GitHub Desktop.
datasheetformat.js
const fs = require('fs');
const data = fs.readFileSync('javelindata_areadefinitions.datasheet');
const amountOfColumnsOffset = 0x44;
const amountOfRowsOffset = 0x48;
const headersOffset = 0x5c;
const amountOfBytesInHeader = 12;
const amountOfBytesInCell = 8;
const amountOfColumns = data.readInt32LE(amountOfColumnsOffset);
const amountOfRows = data.readInt32LE(amountOfRowsOffset);
const cellsOffset = headersOffset + amountOfColumns * amountOfBytesInHeader;
const amountOfBytesInRow = amountOfBytesInCell * amountOfColumns;
const stringsOffset = cellsOffset + amountOfRows * amountOfColumns * amountOfBytesInCell;
const headers = [];
for (let i = 0; i < amountOfColumns; i++) {
const headerOffset = headersOffset + i * amountOfBytesInHeader;
const stringValue = readStringValue(headerOffset);
const type = data.readInt32LE(headerOffset + 8);
headers.push({stringValue, type});
}
const rows = [];
for (let i = 0; i < amountOfRows; i++) {
const cells = [];
for (let j = 0; j < amountOfColumns; j++) {
const cellOffset = cellsOffset + i * amountOfBytesInRow + j * amountOfBytesInCell;
const cellValue = readCell(cellOffset);
const columnType = headers[j].type;
cells.push(parseCellValueToType(cellValue, columnType));
}
rows.push(cells);
}
console.log(rows);
function parseCellValueToType(cellValue, type) {
switch (type) {
case 1:
const offset = stringsOffset + cellValue.readInt32LE(0);
let lengthUntilNullTermination = 0;
let nextByte;
do {
nextByte = data.readInt8(offset + lengthUntilNullTermination++);
} while (nextByte !== 0)
return data.slice(offset, offset + lengthUntilNullTermination - 1).toString();
case 2:
return cellValue.readFloatLE(0);
case 3:
return !!cellValue.readInt32LE(0);
}
}
function readCell(offset) {
const stringOffset = data.readInt32LE(offset);
const cellValue = data.slice(offset + 4, offset + 8);
return cellValue;
}
function readStringValue(offset) {
const hash = data.slice(offset, offset + 4);
const stringOffset = data.slice(offset + 4, offset + 8);
return {hash, stringOffset};
}
@twitdude
Copy link

How do you view the larger datasheets? They appear to get truncated and show

],
... 4614 more items
]

@Kattoor
Copy link
Author

Kattoor commented Aug 28, 2021

Sorry for the late response!
Replace console.log(rows); with fs.writeFileSync('./output.json', JSON.stringify(rows)).
Now you'll get a file 'output.json' with all rows.

@Kattoor
Copy link
Author

Kattoor commented Aug 28, 2021

If you'd like it in CSV, replace console.log(rows); with fs.writeFileSync('./output.csv', rows.map(cells => cells.join(';')).join('\n'));

@Kattoor
Copy link
Author

Kattoor commented Sep 12, 2021

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