Skip to main content

Notifications

Community site session details

Community site session details

Session Id : K8/MzoCiJ+NOL5Ff8sx0Gm
Power Automate - Using Flows
Unanswered

Copy Data from one Excel spreadsheet to another with a flow

Like (0) ShareShare
ReportReport
Posted on 13 Nov 2023 11:19:33 by

Hello, 

 

As the title above suggests, I am trying to make a flow that will copy data from one excel document to another. All the resources I look at on this subject seem to be outdated. 

I don't really mind at this stage if it's a simple "lift and shift" my only requirement would be that I want the data to be overwritten each time the flow is run (looking at scheduling it to recur once per week)

 

If anyone could help me out with this, I'd be grateful. 

 

Cheers, 

  • Suggested answer
    RBoneck Profile Picture
    409 on 23 Nov 2024 at 01:36:56
    Copy Data from one Excel spreadsheet to another with a flow
    This Office Script will collect all data from all tabs/sheets of a given workbook (use the 'Run script' action on the file with the data you want to copy from) :
     
    /**
     * This script returns the values from the used ranges on each worksheet.
     */
    function main(workbook: ExcelScript.Workbook): WorksheetData[] {
        // Create an object to return the data from each worksheet.
        let worksheetInformation: WorksheetData[] = [];
    
        // Get the data from every worksheet, one at a time.
        workbook.getWorksheets().forEach((sheet) => {
            let values = sheet.getUsedRange()?.getValues();
            worksheetInformation.push({
                name: sheet.getName(),
                data: values as string[][]
            });
        });
    
        return worksheetInformation;
    }
    
    // An interface to pass the worksheet name and cell values through a flow.
    interface WorksheetData {
        name: string;
        data: string[][];
    }
    ------------
      
    This Office Script will clear all data from a given tab/worksheet (use the 'Run script' action on the file with the data you want to override):
     
    /**
    * This script 
        locates the specified tab and clears the data on it.
    */
    
    function main(workbook: ExcelScript.Workbook,
        SheetName: string = "Sheet1"
    ) {
    
        //Check if the worksheet exists.
        let dataWorksheet = workbook.getWorksheet(SheetName);
        if (dataWorksheet) {
            //delete the worksheet.
            dataWorksheet.getRange().clear(ExcelScript.ClearApplyTo.contents);
    
              //switch to the worksheet
              workbook.getWorksheet(SheetName).activate();
        }
        else {
            console.log(`No worksheet with the specified name in this workbook.`);
        };
      
    }
    ---------------
      
    And this Office Script will add the data from the result of the first script run action to the specified file you want to copy to (use the 'Run script' action on the file with the data you want to override):

    Note that this script is not perfect since I adapted it from something slightly different. As it stands, if the workbook you are pulling from has multiple sheets/tabs, this script will paste the contents of each sheet on top of the next one into your specified workbook and worksheet, so you are not guaranteed to have the correct data if you use this and there were multiple sheets in the book you copied from.
     
    /**
     * This script adds data to a specified tab with data provided by the Return Worksheet Data script
     */
    function main(workbook: ExcelScript.Workbook, 
        worksheetName: string,
        worksheetInformation: WorksheetData[]
        ) {
        
        // Add each new worksheet.
        worksheetInformation.forEach((value) => {
            let sheet = workbook.getWorksheet(worksheetName);
    
            // If there was any data in the worksheet, add it to a new range.
            if (value.data) {
                let range = sheet.getRangeByIndexes(0, 0, value.data.length, value.data[0].length);
                range.setValues(value.data);
            }
        });
    }
    
    // An interface to pass the worksheet name and cell values through a flow.
    interface WorksheetData {
        name: string;
        data: string[][];
    }
    ---

    Note that your last 'Run script' action should have the result from the first 'Run script' action in the worksheetInformation field. It should look like the picture below, and you will have to click the little T in the top right that says 'Switch to input entire array' to make it look like this.


  • GeoffRen Profile Picture
    Microsoft Employee on 14 Nov 2023 at 17:43:50
    Re: Copy Data from one Excel spreadsheet to another with a flow

    Nowadays you can use Excel connectors to manipulate any data in a workbook in whatever way you want using Office Scripts, which is scripting in Excel integrated with Power Automate. Using this you can export data from one workbook and then write it to another. This video has a good walkthrough of what to do. (2) Move data between workbooks automatically with Office Scripts & Power Automate | Excel Off The Grid - YouTube

  • bblake8480 Profile Picture
    642 Most Valuable Professional on 13 Nov 2023 at 19:44:35
    Re: Copy Data from one Excel spreadsheet to another with a flow

    If that data is in a table and is being copied to a table in the second document, then yes, a flow can copy data from one Excel document to another. The primary limitation of the Excel connectors for Power Automate is that they can only work with data in defined tables in Excel. As far as overwriting the data in the destination, there isn't a quick or easy way to do that. If you intend to write to an existing file, you would first have to manually delete the rows in the table on that document before adding the new rows.

     

    Alternately, if you don't want to clear out the existing rows from the destination, you can create worksheets and tables in a document, so you could create a blank Excel doc, add a workbook to it, add a table to the workbook, then write the rows into it. There's a good blog post here showing how to do that create process.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,645 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,997 Most Valuable Professional

Leaderboard

Featured topics

Restore a deleted flow
Loading started