Skip to main content

Automatically Refresh All Data Connections in Excel Using Office Scripts

 

Automate Excel Data Refresh with Office Scripts

If you work with Excel files that contain Power Query, external databases, or connected data sources, manually refreshing the data every time can be repetitive. Office Scripts provide a simple way to automate this process.

In this article, we'll explain the following Office Script:

function main(workbook: ExcelScript.Workbook) {

  // Refresh all data connections
  workbook.refreshAllDataConnections();

}

What is Office Scripts?

Office Scripts is a feature in Excel for Microsoft 365 that allows you to automate repetitive tasks using TypeScript, a language based on JavaScript.

Office Scripts can:

  • Refresh data connections

  • Format worksheets

  • Create tables

  • Generate reports

  • Automate daily Excel tasks

  • Work with Power Automate


Understanding the Script

Let's break the code into smaller parts.

Function Declaration

function main(workbook: ExcelScript.Workbook) {

Every Office Script starts with a main() function.

What does it do?

  • main() is the entry point of the script.

  • workbook represents the currently opened Excel workbook.

  • ExcelScript.Workbook provides access to worksheets, tables, charts, pivot tables, and data connections.

Think of workbook as your entire Excel file.


Refresh All Data Connections

workbook.refreshAllDataConnections();

This is the main line of the script.

It tells Excel to refresh every data connection in the workbook.

This includes:

  • Power Query

  • SQL Server connections

  • Oracle database connections

  • OData feeds

  • CSV connections

  • SharePoint lists

  • Other external data sources

Instead of refreshing each connection manually, this single command updates them all.


How It Works

When the script runs:

  1. Opens the current workbook.

  2. Finds all configured data connections.

  3. Refreshes each connection.

  4. Updates tables, queries, PivotTables, and reports linked to those sources.


Complete Script

function main(workbook: ExcelScript.Workbook){

    // Refresh all data connections

    workbook.refreshAllDataConnections();

}

This is one of the shortest yet most useful Office Scripts for report automation.


Example Scenario

Imagine your Excel report is connected to:

  • Sales Database

  • Employee Database

  • Power Query

  • Inventory CSV

Every morning, instead of clicking:

Data → Refresh All

you simply run this Office Script, and every connected source is updated automatically.


Benefits

Using refreshAllDataConnections() offers several advantages:

  • Saves time

  • Eliminates manual refresh steps

  • Ensures reports always use the latest data

  • Perfect for scheduled automation

  • Integrates with Power Automate for unattended workflows


Common Use Cases

This script is useful for:

  • Daily sales reports

  • Financial dashboards

  • Inventory tracking

  • HR reporting

  • Business intelligence dashboards

  • Power Query automation

  • Scheduled Excel workflows


Things to Remember

  • The workbook must already contain one or more data connections.

  • If there are no data connections, the script completes without making changes.

  • Some external sources may require authentication or appropriate permissions.

  • Refresh duration depends on the size and speed of the connected data source.


Conclusion

The workbook.refreshAllDataConnections() method is a simple but powerful way to automate Excel data refreshes. With just one line of code, you can update every external data connection in your workbook, making it ideal for dashboards, reports, and automated workflows.

If you're building Excel automation with Office Scripts, this method is an essential tool for keeping your data current with minimal effort.

Comments

Popular posts from this blog

Key Limitations of Microsoft Power Automate (as of August 2025)

Microsoft Power Automate is a powerful tool for automating business processes, but like any platform, it comes with a set of limitations. Understanding these constraints is essential to designing efficient, scalable, and compliant workflows—especially as your automation strategy grows in complexity.  Here are the most important limits you need to know:  1. Switch Cases Each Switch action supports a maximum of 25 cases. If you need more, consider using nested Switches or alternate logic like parallel branches or conditionals.  2. Actions per Workflow A single flow can contain up to 500 actions. For complex workflows, you may need to split logic into separate flows or use child flows to stay within this limit.  3. Nesting Depth You can nest actions (e.g., conditionals or loops) up to 8 levels deep. Going beyond this will result in a design error.  4. Variables per Flow Each flow can define up to 250 variables. This includes all variable types (string, inte...

Bulk Import Excel Data to SharePoint List Using PowerShell and PnP

  Managing large datasets in SharePoint can be tricky, especially when you're dealing with Excel files and need to avoid list view threshold issues. In this guide, I’ll walk you through a PowerShell script that efficiently imports data from Excel into a SharePoint Online list using PnP PowerShell — with batching support for performance. Prerequisites Make sure you have the following before running the script: SharePoint Online site URL Excel file with data properly formatted PnP PowerShell module installed ( Install-Module PnP.PowerShell ) Appropriate SharePoint permissions What the Script Does Connects to your SharePoint site Loads and reads an Excel file Converts Excel date values Batches records in groups (to avoid the 5000 item threshold) Adds the items to your SharePoint list or library Logs execution time PowerShell Script $siteUrl = "[Site Collection URL]" Connect-PnPOnline -Url $siteUrl -UseWebLogin # Capture the start time $startTime...

How to Split a Large Excel File into Smaller Chunks Using PowerShell

Working with massive Excel files can be cumbersome—slow to open, hard to process, and error-prone in automation. If you’re dealing with a large dataset and need to split it into smaller, manageable files, PowerShell offers a powerful and efficient way to do it—especially with the help of the ImportExcel module. In this guide, I’ll walk you through a simple script that takes a large Excel file and splits it into multiple smaller Excel files, each containing a defined number of records. Requirements PowerShell ImportExcel module You can install it via PowerShell with:              Install-Module -Name ImportExcel # Import the ImportExcel module Import-Module ImportExcel # Path to the large Excel file $excelFilePath = "[LocalFilePathwithFileExtention]" # Define the chunk size (e.g., 10,000 records per chunk) $chunkSize = 10000 # Read the Excel file $excelData = Import-Excel -Path $excelFilePath # Calculate how many chunks are needed $totalRo...