So I created a node.js script to convert the object of an array to CSV format, then I used objects-to-csv from NPM libraries. First, is want to iterate the array by adding an ID. The script was successfully created the CSV file. but when I open it to the WPS and Microsoft Office, there was a warning that either a file has an error or it's not an SYLYK File. please see the code below.
const ObjectsToCsv = require("objects-to-csv");
// Sample data - two columns, three rows:
const data = [
{ code: "CA", name: "California" },
{ code: "TX", name: "Texas" },
{ code: "NY", name: "New York" },
];
// If you use "await", code must be inside an asynchronous function:
(async () => {
const iterateWithID = data.map((x, i) => {
return {
ID: i + 1,
code: x.code,
name: x.name,
};
});
const csv = new ObjectsToCsv(iterateWithID);
// Save to file:
await csv.toDisk("./output.csv");
// Return the CSV file as string:
console.log(await csv.toString());
})();
here is the screenshot of the warning when I open it to the WPS
I tried to remove the iteration of the array, and I didn't encounter any problem when opening the CSV file. What I really want to fix this warning when I open the CSV file.
If you make the first letters
ID
of a text file, Excel incorrectly assumes you are trying to open an SYLK file. I updated your code to haveId
instead ofID
.