Script to convert CSV to table

Copper Contributor

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],[First Name],[Email]
John Dow, John,email address removed for privacy reasons
Roger Stone, Roger,email address removed for privacy reasons
Claire Dupont,Claire,email address removed for privacy reasons

 

Final Result to Achieve

Pierre73_0-1681805422035.png

 

 

 

9 Replies

@Pierre73 

From what I could gather from your text, you want to create an office script that converts a CSV file to an Excel spreadsheet, and then use that script in Power Automate to send emails.

Here's an example of how you can write an Office script to convert a CSV file to an Excel spreadsheet:

function main(workbook: ExcelScript.Workbook) {
  // Get the current worksheet
  let sheet = workbook.getActiveWorksheet();

  // Get the data from the CSV file
  let csvData = `Sales Name,First Name,Email
John Dow,John,email address removed for privacy reasons
Roger Stone,Roger,email address removed for privacy reasons
Claire Dupont,Claire,email address removed for privacy reasons`;

  // 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).setValue(columns[j]);
    }
  }

  // Create a table from the data
  let table = sheet.addTable(sheet.getUsedRange().getAddress(), true);
}

*Script from Internet.

 

This script gets the data from a CSV file and splits it into rows.

It then loops through each row and splits it into columns.

The script sets the value of each cell in the worksheet based on the data from the CSV file.

Finally, it creates a table from the data in the worksheet.

You can then use this script in Power Automate to convert your CSV file into an Excel table and send emails based on the data in the table.

As for making the emails clickable, you can format the cells containing email addresses as hyperlinks. This will make them clickable and allow users to easily send emails by clicking on the email addresses.

 

I hope this helps!

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?

@Pierre73 

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.

 

 

@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.

Pierre73_0-1681825215542.pngPierre73_1-1681825295184.png

 

It 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.

I added the URL (file based on OneDrive), but I still have the errors

Pierre73_0-1681825996868.png

 

The script i provided is an Office Script for Excel on the web, not a Power Automate flow. Office Scripts and Power Automate are two separate technologies, and a script written for one cannot be directly used in the other.
If you want to use this script in Excel on the web, you can do so by opening the Excel workbook in your web browser, selecting the “Automate” tab, and then selecting “Code Editor”. This will open the Office Scripts code editor, where you can paste the script and run it.
If you want to use this script in a Power Automate flow, you will need to create a new flow and use the “Run script” action to run the script in an Excel workbook. You will also need to make sure that the flow has access to the Excel file and that the file is stored in a location supported by Power Automate, such as OneDrive or SharePoint.
If you are encountering a “Power Automate incompatible error” when trying to run this script, it may be because you are trying to run it as a Power Automate flow instead of as an Office Script. Please make sure that you are using the correct technology for your needs.
This is how I have done it.
I copied the scripte in excel online. But the tests are not working

@Pierre73 

If you have copied the script into the Office Scripts code editor in Excel on the web and it is not working as expected, there could be several reasons for this. Here are some common issues and troubleshooting steps you can try:

  1. Make sure that you have replaced correctly <YOUR_FILE_URL> with the URL of the CSV file in OneDrive. The URL should be enclosed in quotation marks.
  2. Check if there are any error messages in the console of the code editor. These messages can provide more information about what might be causing the issue.
  3. Make sure that the CSV file is formatted correctly and that its contents match the expected format of the script. For example, the script expects the CSV data to be separated by commas and newlines.
  4. Verify that you have permission to access the CSV file in OneDrive and that its sharing settings allow it to be accessed by the script.

 

If you have tried these steps and are still encountering issues, If this didn't help either, I can't help, unfortunately, because I'm at my wits end.

 

Thank you for your understanding and patience