Skip to main content
Power Automate
    • Connectors
    • Templates
    • Take a guided tour
    • Digital process automation
    • Robotic process automation
    • Business process automation
    • Process advisor
    • AI Builder
  • Pricing
  • Partners
    • Blog
    • Documentation
    • Roadmap
    • Self-paced learning
    • Webinar
    • Business process and workflow automation topics
    • Overview
    • Issues
    • Give feedback
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • User groups
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Microsoft Power Automate Community
    • Welcome to the Community!
    • News & Announcements
    • Get Help with Power Automate
    • General Power Automate Discussion
    • Using Connectors
    • Building Flows
    • Using Flows
    • Power Automate Desktop
    • Process Advisor
    • AI Builder
    • Power Automate Mobile App
    • Translation Quality Feedback
    • Connector Development
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Galleries
    • Community Connections & How-To Videos
    • Webinars and Video Gallery
    • Power Automate Cookbook
    • Events
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Community Engagement
    • Community AMA
    • Community Blog
    • Power Automate Community Blog
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power Automate Community
    • Galleries
    • Power Automate Cookbook
    • Re: import csv 2.0 with office script

    Re: import csv 2.0 with office script

    02-09-2022 05:41 AM

    juresti
    Continued Contributor
    2519 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    juresti
    juresti Continued Contributor
    Continued Contributor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    import csv 2.0 with office script

    ‎12-17-2021 12:26 PM

    Title: Import CSV File 2.0

     

    Description: This flow allows you to import csv files into a destination table and uses office script for parsing the data.

    This is my version 2.0 of csv import and is much simpler and smaller than my original csv import.

     

    You may need office 365 for business to make office scripts available.

     

    You could create a JSON output to access the columns dynamically. In this flow we simply begin to write our data columns by accessing them via column indexes (apply to each actions).

     

    You can easily access the columns in the for each loop by using the column indexes . . . [0],[1],[2], etc...

    Column 1 will be index [0], column 2 will be index [1], and so on.

     

     

    Detailed Instructions: 

    The only thing to modify is the get file content and trigger and the return data rows loop (apply to each).

    Import the package attached below into your own environment.

    You need to change the action where I get the file content from, such as sharepoint get file content, onedrive get file content.

    When you change the get file content action it may remove any references to it, this is where it belongs though.

    juresti_0-1639772518361.png

     

    This var file content is important as it converts your file content into readable text data as it is assigned to the variable.

     

    You will also need to create the office script within a blank excel file on a sharepoint site.

     

    Questions: If you have any issues running it, most likely I can figure it out for you.

     

    Anything else we should know: You can easily change the trigger type to be scheduled, manual, or when a certain event occurs.

     You may end up with quotes or other characters within columns after the parsing. These can be replaced while processing each column for writing.

    Use the replace formula while writing a column.

    Replace(searchedString, “replace this”, “with this”)

     

    Creating the office script:

    Open a blank file in excel online

     

    Go to Automate

    juresti_1-1639772518363.png

     

     

    All scripts

    juresti_2-1639772518364.png

     

     

    New script

    juresti_3-1639772518365.png

     

     

    In the script window paste this code making sure to replace all existing code.

     

    //Code begin----------------------------

    function main(workbook: ExcelScript.Workbook, csvdata: string[][]) {

     

      // initialize the 2D array

      let results: string[][] = new Array(); // rows array

     

      // loop each row from the input array

      for (let i = 0; i < csvdata[0].length; i++) { 

        // new row array

        let columnarray: string[][] = new Array();

        // split into column array

        columnarray.push(csvdata[0][i].split(RegExp("(?:^|,)((?:[^\",]|\"[^\"]*\")*)")));

        // add split columns array to rows array

        results.push(columnarray[0].toString().replace("\r","").slice(1, columnarray[0].lastIndexOf(",")).split(",,"));

      }

     

      // removes rows 

      results.shift(); // headers row

     

      console.log(results);

      // omit blank lines on return data

      return results.filter((value,index,array) => value[0] != "");

     

    }

    //Code end------------------------------------------

     

    Then save the script.

    juresti_4-1639772518366.png

     

     

    The flow is now ready to run. Change the trigger to your preference.

    The file must be a CSV file format.

     

    importcsvwithofficescript_20211217195529.zip
    Labels:
    • Labels:
    • Scheduled flows
    • Solutions
    Message 1 of 6
    2,815 Views
    2 Kudos
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    andrehamon
    andrehamon
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-09-2022 02:17 AM

    Thank you so much for this very handy flow @juresti! One question, I'm trying to only return data from the script where a certain column contains data.

     

    E.g.

    A     B     C

    1             cat

    2     X     dog

    3     X     mouse

    4            hamster

     

    Where the script would only pass rows back to Power automate where column B contains 'X'. I think that I may need to alter the script line:

    return results.filter((value,index,array) => value[0] != "");

    But am totally lost 😅

    Message 2 of 6
    2,528 Views
    1 Kudo
    Reply
    juresti
    juresti Continued Contributor
    Continued Contributor
    In response to andrehamon
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-09-2022 05:41 AM

    Hello @andrehamon 

     

    It is good to know you found this useful.

     

    You are correct, that line needs to be modified for your return criteria.

     

    This is how:

     

    Value[0] means the value in column 1 or column A.

    So Value[1] would be column 2 or column B and so on.

    Simply set Value[?] of your column and equal to the value you are looking for.

     

    Column B contains an X

     

    Value[1] = "X"

     

    Note that this is an equals evaluation, so it must be an exact match, no spaces or anything else in the column.

     

    Message 3 of 6
    2,519 Views
    1 Kudo
    Reply
    andrehamon
    andrehamon
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-09-2022 04:23 PM

    Amazing, was testing with data that used spaces instead of blanks so when I tried Value[n] != "" it wasn't working. Value[n] != " " has solved the issue!

    Message 4 of 6
    2,503 Views
    1 Kudo
    Reply
    Development
    Development
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎05-20-2022 04:07 AM

    how to get column size of csv file  in office script??

    Message 5 of 6
    1,903 Views
    0 Kudos
    Reply
    juresti
    juresti Continued Contributor
    Continued Contributor
    In response to Development
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎05-20-2022 09:24 AM

    Hi @Development 

     

    To get the length of a column specify the row then the column and get the length.

     

    The line would look like this using my example:

     

    let column_size = results[0][0].length;

     

    The array can be confusing but it reads like so where 0 is actually row 1 or column 1:

     

    results[row][column]

    Message 6 of 6
    1,890 Views
    1 Kudo
    Reply

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    • Sign up free
    • Sign in

    Browse

    • Templates
    • Connectors
    • Partners

    Downloads

    • Mobile
    • Gateway

    Learn

    • Documentation
    • Learn
    • Support
    • Community
    • Give feedback
    • Blog
    • Pricing

    • © 2023 Microsoft
    • Contact us
    • Trademarks
    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Terms & conditions
    California Consumer Privacy Act (CCPA) Opt-Out Icon Your California Privacy Choices