Office Scripts in Excel have workbook scope. That means a script works within the workbook it runs on. It cannot simply reach into another workbook and move data across.
However, we can pass data from an Office Script into Power Automate, then pass that data down into a second workbook. With two reusable scripts and one short flow, we can keep a central workbook up to date without opening files or copying and pasting. Fantastic!
What You'll Learn
- Power Automate passes data between Office Scripts running on separate Excel workbooks.
- Remove incoming header and footer rows before combining records with the central dataset.
- Use the trigger's File identifier to process each newly created workbook automatically.
- Reuse the scripts across flows by changing their parameters and destination workbook.
The Example: Append New Data to a Central Workbook
Our example uses two workbooks:
- Complete Data: The central workbook containing the existing data in four columns.
- New Data: An incoming workbook containing additional rows we want to add beneath the existing records.
Both workbooks use a worksheet called Data, with the data range starting at A1. The incoming data has one header row and no footer rows.
Whenever a new workbook arrives in a designated OneDrive for Business folder, we want Power Automate to extract its data and append it to Complete Data. The central workbook already has column headings, so we must leave the incoming header row behind.
The process has three parts:
- Run an Office Script on the incoming workbook to extract the required rows.
- Pass the extracted values through Power Automate.
- Run a second Office Script on Complete Data to combine the existing and incoming records.
We will use the two scripts available in the supporting Office Scripts code and instructions. In Excel, select Automate > New Script to add each script. If you have already saved them, select the relevant script from the gallery and click Edit.
Office Script 1: Get Data from Excel
The first script, PA Get Data from Excel, reads the incoming workbook and returns the data to Power Automate.
The script parameters
The script uses the workbook supplied by Excel, plus parameters that tell it where the data is and which rows to exclude:
- workbook: The workbook object the script runs against. In the flow, we choose the file separately.
- wsName: The name of the worksheet containing the incoming data.
- startCell: A cell identifying the start of the data range, such as A1.
- Header rows: The number of rows to remove from the top of the extracted data.
- Footer rows: The number of rows to remove from the bottom.
For this example, we use Data, A1, 1 header row, and 0 footer rows.
Why make the header and footer counts parameters? Because the rows we need are not always the whole range. We do not want repeated headings appearing between batches of records. Similarly, if an incoming workbook contains a total row at the bottom, we would normally exclude that too.
How the extraction works
The script first creates a worksheet variable called ws. It then creates a range variable, wsRng, using the start cell and its surrounding region. This picks up the connected block of cells around that starting point.
Next, it reads the range into an array of values and counts the rows. The number of rows we want to keep is the total row count minus the header and footer rows.
Finally, the script uses the slice method to select only the required rows. That resulting array is returned to Power Automate as wsRngArr.
At this point, we have extracted the incoming records, but we have not changed the central workbook. Power Automate provides the connection to the next step.
Office Script 2: Append Data to Excel
The second script, PA Paste Append Data to Excel, runs against Complete Data. Its job is to combine the existing data with the rows returned by the first script.
It has four parameters:
- workbook: The destination workbook object.
- wsName: The destination worksheet name.
- startCell: The starting cell of the existing data range.
- stringArr: The incoming data passed through Power Automate as a JSON-formatted string.
The script parses the incoming JSON string back into an array of values. It then gets the destination worksheet and reads its existing data into another array. In our example, the existing range is A1:D18.
From there, the script:
- Concatenates the existing array with the incoming array.
- Calculates the worksheet range needed to hold the combined data.
- Uses setValues to write the complete array back to the worksheet.
The result is an append operation, but the script writes both the old and new data back together. It is not performing a clipboard paste into the first empty row.
The destination headings remain in the existing array. Because the first script removed the incoming headings, the combined data contains only one header row.
Create the Power Automate Flow
With both scripts saved, we can build the flow that connects them. We need one trigger and two Run script actions.
1. Trigger the flow when a new file is created
- In Power Automate, create an Automated cloud flow.
- Give it a name, such as Auto Move Data.
- Search for the When a file is created trigger from OneDrive for Business.
- Click Create.
- Set the trigger folder to the folder where incoming workbooks will be saved. In this example, that folder is called New Data.
The folder is what matters to this trigger. Although our example workbook is called New Data, the flow starts when a new file is created in the selected folder, not because a workbook has that particular name.
2. Run the extraction script on the incoming workbook
Add an Excel Online (Business) Run script action. Configure the workbook connection as follows:
- Location: OneDrive for Business.
- Document Library: OneDrive.
- File: Select File identifier from the trigger's dynamic content.
- Script: PA Get Data from Excel.
Use the dynamic File identifier rather than selecting a fixed source workbook. The trigger already knows which file has just arrived. Passing that identifier into the action makes the script run on that incoming file each time.
Once you select the script, its parameter fields appear. Enter:
- wsName: Data
- startCell: A1
- Header rows: 1
- Footer rows: 0
This action now extracts the data rows from whichever workbook caused the flow to start.
3. Run the append script on Complete Data
Add another Excel Online (Business) Run script action. This time, choose the fixed destination workbook:
- Location: OneDrive for Business.
- Document Library: OneDrive.
- File: Browse to and select Complete Data.
- Script: PA Paste Append Data to Excel.
Set wsName to Data and startCell to A1.
For stringArr, select the output returned by the first script from the dynamic content panel. Search for ws to find wsRngArr, then insert that value.
This is the key connection: the output of the extraction script becomes the input of the append script.
Click Save. The flow is ready to test.
Test the Automatic Workbook Transfer
Open Complete Data so you can check its existing records. Then upload the New Data workbook into the OneDrive folder selected in the trigger.
That upload starts the process:
- OneDrive detects the newly created file.
- The first script reads its Data worksheet and removes the header row.
- Power Automate passes the returned records to the second script.
- The second script combines those records with the data in Complete Data.
The update may not appear immediately. The trigger can take a little while to start, so allow time for the flow to run.
Once it completes, the incoming records appear beneath the original data, without an extra header row.
Opening the destination workbook is useful for checking the result, but it is not required for the automation itself. The scripts and flow perform the transfer in the background.
Reuse the Same Scripts in Other Flows
The useful part is that the incoming file does not have to be uploaded manually. Another Power Automate flow could save an email attachment into the New Data folder, or another process could create the file there. Each new file creation can start the same transfer process.
The scripts are reusable too. Instead of rewriting them for every workbook, add them to another flow and change the worksheet names, starting cells, header counts, footer counts, and destination file as required.
Office Scripts still operate within individual workbooks. Power Automate connects those separate operations. Together, they give us a practical way to keep a central Excel data source up to date without the repeated copying and pasting.


