Forum Discussion
Pierre73
Apr 18, 2023Copper Contributor
Script to convert CSV to table
Hello, I am trying to create a script that convert csv to an excel table to use in power automate to send emails from that script. The emails need to be clickebale My csv example [Sales Name],...
Pierre73
Apr 18, 2023Copper Contributor
Hi NikolinoDE,
Thank you for your reply. In the script you provided the sales name, first name and email are always the same. The names and emails are changing all the time. So in the script it should be interactive, depending what is in the csv file.
Is that possible to have it in a script?
Thank you for your reply. In the script you provided the sales name, first name and email are always the same. The names and emails are changing all the time. So in the script it should be interactive, depending what is in the csv file.
Is that possible to have it in a script?
NikolinoDE
Apr 18, 2023Gold Contributor
Example how to change the code to format the cells in email column as hyperlinks:
async function main(context: Excel.RequestContext) {
// Get the file from OneDrive
let response = await fetch("<YOUR_FILE_URL>");
let csvData = await response.text();
// Get the current worksheet
let sheet = context.workbook.worksheets.getActiveWorksheet();
// Split the CSV data into rows
let rows = csvData.split("\n");
// Loop through each row
for (let i = 0; i < rows.length; i++) {
// Split the row into columns
let columns = rows[i].split(",");
// Loop through each column
for (let j = 0; j < columns.length; j++) {
// Set the value of the cell
sheet.getCell(i, j).values = [[columns[j]]];
// Format the email column as a hyperlink
if (j === 2 && i > 0) {
sheet.getCell(i, j).hyperlink = `mailto:${columns[j]}`;
sheet.getCell(i, j).format.font.color = "blue";
sheet.getCell(i, j).format.font.underline = "Single";
}
}
}
// Create a table from the data
let table = sheet.tables.add(sheet.getUsedRange().getAddress(), true);
table.getHeaderRowRange().format.fill.color = "#4472C4";
table.getHeaderRowRange().format.font.color = "white";
await context.sync();
}
Just hope the file isn't in OneDrive.
- Pierre73Apr 18, 2023Copper Contributor
NikolinoDE
I copy past your script and I have some errors. I see I have to modify <YOUR FILE URL>, but I have errors on line 1, 7 and 32.- NikolinoDEApr 18, 2023Gold ContributorIt retrieves a CSV file from a specified URL, parses the data, and inserts it into a table in the active worksheet.
To use this script, you need to replace <YOUR_FILE_URL> with the URL of the CSV file you want to import.
Make sure that the URL is enclosed in quotation marks and that you have permission to access the file.- Pierre73Apr 18, 2023Copper Contributor
I added the URL (file based on OneDrive), but I still have the errors