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;
}