Hi, I do use following script to convert a csv to an excel file using power automate.
function main(workbook: ExcelScript.Workbook, csv: string) {
let sheet = workbook.getWorksheet("Sheet1");
/* Convert the CSV data into a 2D array. */
// Trim the trailing new line.
csv = csv.trim();
// Split each line into a row.
let rows = csv.split("\r\n");
rows.forEach((value, index) => {
/*
* For each row, match the comma-separated sections.
* For more information on how to use regular expressions to parse CSV files,
* see this Stack Overflow post: https://stackoverflow.com/a/48806378/9227753
*/
let row = value.match(/(?:,|\n|^)("(?:(?:"")*[^"]*)*"|[^",\n]*|(?:\n|$))/g);
// Remove the preceding comma.
row.forEach((cell, index) => {
row[index] = cell.indexOf(",") === 0 ? cell.substr(1) : cell;
});
// Create a 2D-array with one row.
let data: string[][] = [];
data.push(row);
// Put the data in the worksheet.
let range = sheet.getRangeByIndexes(index, 0, 1, data[0].length);
range.setValues(data);
});
// Add any formatting or table creation that you want.
}
However, when working with dates is incorrectly converting dates with format dd/mm/yyyy
When date is: 02/12/2021 -> It converts to 2021-07-12T00:00:00.000Z
But when date is 21/12/2021 0> It correctly converts to 21/12/2021
Do you know how can I edit the script to work correctly with dates with format dd/mm/yyyy?
Thanks