I have a program, that generates a repport in a CSV file. Problem is, that the timestamps is in Unix Timestamp format. So i need to access the specific time stamp element in a forEach loop and modify that. I'm quite new to JavaScript. If it was Python, it would have been no problem..
The code looks like this:
for (let electronicRecord of result.Values) {
fields.forEach((f, i) => csvData += electronicRecord[f] + (i == fields.length -1 ? "\n" : delimiter));
}
I know that the value i want to change is "ElectronicRecord["TimeStamp"], but how do i access that in a forEach loop?
for (let i in fields) {
if (i == "TimeStamp") {
csvData += electronicRecord[i]
} else if (fields.indexOf(i) == fields.length - 1) {
csvData += electronicRecord[i] + "\n";
} else {
csvData += electronicRecord[i] + delimiter;
}
}
This was my idea, but it doesn't seem to be working. fields is an array with the "headlines" for the CSV file.
The original function in my system is:
// Please note, the format of a file path is device and operating system specific
// You need to adapt the given path to your environment
// Example for a Unified Comfort device (Linux OS): /home/user1/data.dat
// Example for a Unified PC-based device (MS Windows OS): C:\\Users\\Public\\data.dat
const startDate = "2020-09-01 08:00:00.000";
const endDate = "2020-10-01 08:00:00.000";
let moreFollows = false;
let page = 0;
let fields = ["TimeStamp", "AuditProviderType", "AuditProvider", "ObjectReference","ObjectName", "OperationType", "OperatorStation", "User", "OldValue", "NewValue", "Reason", "Language", "Signature", "Integrity"];
let delimiter = ",";
let csvData = "";
fields.forEach((f, i) => csvData += f + (i == fields.length - 1 ? "\n" : delimiter));
do {
try {
const result = await HMIRuntime.Audit.SysFct.ReadElectronicRecord(startDate, endDate, page);
moreFollows = result.More;
for (let electronicRecord of result.Values) {
fields.forEach((f, i) => csvData += electronicRecord[f] + (i == fields.length -1 ? "\n" : delimiter));
}
page++;
} catch(ex) {
HMIRuntime.Trace("Error reading electronic record. Error: " + ex.message);
return;
}
} while (moreFollows);
try {
let fileName = "C:\\Users\\Public\\AuditFile.csv";
await HMIRuntime.FileSystem.WriteFile(fileName, csvData, "utf8");
HMIRuntime.Trace("Write file finished successfully");
} catch(ex) {
HMIRuntime.Trace("Error writing file. Error: " + ex.message);
}