Regular Expressions in addon Script
Posted: Sun Apr 14, 2024 7:15 pm
All - I have become quite interested in writing small addon scripts to QCAD. Huge thanks to the moderators and regular contributors on here helping us new guys out. Hoping someone can help me figure out why the following script fails on this line:
const trimmedValues = values.map(value => value.trim().replace(/^"|"$/g, ''));
The debugger warning is "Uncaught exception at C:/Program Files/QCAD/scripts/Misc/drawTable\readTextFile.js:40: SyntaxError: Parse error" I have bascially individually tested all parts and they seem to run ok. Full disclosure that I am an engineer pretending to be a coder!
const trimmedValues = values.map(value => value.trim().replace(/^"|"$/g, ''));
The debugger warning is "Uncaught exception at C:/Program Files/QCAD/scripts/Misc/drawTable\readTextFile.js:40: SyntaxError: Parse error" I have bascially individually tested all parts and they seem to run ok. Full disclosure that I am an engineer pretending to be a coder!
Code: Select all
include("scripts/EAction.js");
include("scripts/library.js");
var appWin = EAction.getMainWindow();
// A full path and complete filename with suffix.
// IMPORTANT: script engine processes backslash "\" as an escape character so each "\" must be doubled as shown below
// var fileName = "C:\\Users\\Michael\\Documents\\QCAD_Working\\drawTable.txt";
function readTextFile(fileName) {
var fi = new QFileInfo(fileName);
// When the file doesn't exist then issue a warning and return 'undefined':
if (!fi.exists()) {
appWin.handleUserWarning("File does not exist");
return undefined;
}
var file = new QFile(fileName);
var flags = makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text);
if (file.open(flags)) {
var textStream = new QTextStream(file);
setUtf8Codec(textStream);
var inputArray = []
while (!textStream.atEnd())
{
var line = textStream.readLine();
qDebug(line)
function parseLine(colString) {
// Split the string by "|"
const values = colString.split('|');
// Trim whitespace from each value and remove surrounding quotes if present
const trimmedValues = values.map(value => value.trim().replace(/^"|"$/g, ''));
return trimmedValues;
}
var lineArray = parseLine(line)
inputArray.push(lineArray)
}
file.close();
return inputArray;
}
return undefined;
}