Office Scripts: Automate Excel with TypeScript

Office Scripts in Excel tutorial showing TypeScript automation for worksheets data formatting formulas and repetitive tasks
Automate repetitive Excel tasks with Office Scripts and TypeScript. This practical tutorial introduces you to Excel’s modern scripting capabilities, showing how to record and write scripts, manipulate worksheets and ranges, format data, create formulas, and automate common spreadsheet workflows. Learn how TypeScript-based Office Scripts can replace repetitive manual steps, standardize processes, and make Excel automation more scalable and consistent. Ideal for Excel power users, analysts, finance teams, developers, and professionals who want to automate Excel tasks without relying on traditional VBA macros.

VBA has automated Excel for decades. Yet it only runs on the Windows desktop app. Open the same workbook in a browser, and your macros go quiet. More teams now live in Excel for the web, so that gap hurts. Office Scripts close it. They are Microsoft's modern, cloud-first way to automate Excel, written in TypeScript and run straight from the Automate tab.

This guide gets you started with Office Scripts from scratch. First, it explains the Automate tab and the main function. Then it records a script and reads real code. Worked examples and a full troubleshooting section follow. By the end, you will write and run your first script with confidence.

What Office Scripts Are

Office Scripts automate Excel with a modern language. You write them in TypeScript, a typed form of JavaScript. Crucially, they run in the cloud, not on your desktop. So they work on Excel for the web and on Windows alike. The infographic below shows the flow.

How an Office Script runs, end to end Automate tab Record or code main(workbook) TypeScript entry ExcelScript API getRange, setValues Run it button or flow Cloud-first · cross-platform · runs on Excel for the web Chain into Power Automate for scheduled, hands-free runs Modern VBA alternative

Every script starts from a single main function. It runs, touches the workbook, and finishes. Notably, you never manage files or windows yourself. Instead, Excel hands your code a workbook object to work with.

Office Scripts Versus VBA

Both tools automate Excel, but they suit different worlds. VBA rules the Windows desktop and reaches deep into the app. Office Scripts run in the cloud and chain into Power Automate. Therefore your choice depends on where the work lives.

Pick the right tool: OFFICE SCRIPTS: Cloud, cross-platform, TypeScript, Power Automate. Great for shared, browser-based, scheduled work. VBA: Desktop only, deep app access, UserForms, COM. Great for rich local tools and legacy macros. Rule of thumb: cloud and sharing favour Office Scripts.
Office Scripts need a business plan. The Automate tab appears on Microsoft 365 business and enterprise plans. A personal or home plan will not show it. So check your plan before you begin.

Example 1: Record Your First Script

The fastest start is to record, not type. The Action Recorder watches what you do. Then it writes the matching TypeScript for you. So you learn the code by seeing your own steps.

Record and replay: 1. Open the Automate tab in Excel. 2. Click Record Actions. 3. Format a range or type a few values. 4. Click Stop, then give the script a name. 5. Press Run to replay it any time. The recorder writes clean, editable code. So it doubles as a gentle way to learn.

Example 2: Read the main Function

Every script shares the same entry point. Excel calls a function named main for you. It passes in the workbook as an argument. Therefore your code always starts from that object.

The required signature: function main(workbook: ExcelScript.Workbook) { let sheet = workbook.getActiveWorksheet(); sheet.getRange("A1").setValue("Hello"); } The workbook argument is your gateway to the file. From it, you reach sheets, ranges, tables, and charts.

Example 3: Read and Write a Range

Most scripts read some cells and write others. The getRange method points at a block of cells. Then getValues and setValues move the data. As a result, you process a range in a few lines.

Move values in and out: function main(workbook: ExcelScript.Workbook) { let sheet = workbook.getActiveWorksheet(); let data = sheet.getRange("A1:A10").getValues(); sheet.getRange("B1").setValue(data.length + " rows"); } getValues returns a grid of the cell contents. Then setValue writes a single result back.

Example 4: Loop Over Rows

Real work often repeats across many rows. A simple loop walks through each one. You read a value, change it, and write it back. So a script can clean a whole column at once.

Process each row: function main(workbook: ExcelScript.Workbook) { let sheet = workbook.getActiveWorksheet(); let range = sheet.getRange("A1:A100"); let values = range.getValues(); for (let i = 0; i < values.length; i++) { values[i][0] = String(values[i][0]).trim(); } range.setValues(values); } Read once, edit in memory, then write once. Consequently, the script stays fast even on big ranges.

Example 5: Return a Value to a Flow

A script can hand data back to Power Automate. You simply return a value from main. Then a flow reads that result and acts on it. This is how scripts join a larger automation.

Send a result onward: function main(workbook: ExcelScript.Workbook): number { let sheet = workbook.getActiveWorksheet(); let total = sheet.getRange("C1").getValue() as number; return total; } The return type sits after the argument list. So Power Automate receives the total and continues.

Example 6: Run a Script on a Schedule

Scripts really shine inside Power Automate. There, a flow can run your script on a timer. It can also trigger on a new email or file. Therefore your report updates with no one at the keyboard.

Automate the run: 1. In Power Automate, create a scheduled flow. 2. Add the action Run script. 3. Pick the workbook and your saved script. 4. Map any inputs the script expects. The flow now runs the script for you. So a nightly refresh happens while you sleep.

Troubleshooting Office Scripts

All three problems below are the most common. Each has a clear cause and a quick fix.

The Automate tab is missing

This almost always comes down to your plan. Office Scripts need a Microsoft 365 business or enterprise licence. A personal or family plan simply will not show the tab. First, confirm your licence with your admin. Next, ask whether Office Scripts are enabled in the admin centre. Some organisations switch the feature off by policy. Once the plan and policy allow it, the Automate tab appears.

The script cannot reach another app

Office Scripts run inside a safe sandbox by design. So a script cannot open files or control other programs. This differs sharply from VBA, which can reach the whole PC. To connect to email, files, or other services, use Power Automate instead. A flow calls your script and handles the outside steps. In short, keep the script on the workbook and let the flow do the rest.

TypeScript flags a type error

TypeScript checks types before the script runs. So a value read from a cell may need a clear type. A cell value can be text, a number, or a boolean. Because of this, the editor may warn until you say which one. To fix it, cast the value, such as reading it as a number. The editor underlines the problem and suggests a fix. Once the types line up, the warning disappears.

Frequently Asked Questions

  • Are Office Scripts a replacement for VBA?+
    Not exactly, since each suits a different world. Office Scripts run in the cloud and across platforms. VBA runs on the desktop with deeper app access. So choose scripts for shared, browser-based work.
  • What language do Office Scripts use?+
    They use TypeScript, a typed form of JavaScript. Every script starts from a main function. That function receives the workbook as an argument. From there, the ExcelScript API does the work.
  • Do I need to code to get started?+
    No, you can begin with the Action Recorder. It writes the TypeScript from your own clicks. Then you can read and tweak that code. So recording is a gentle first step.
  • Why is the Automate tab not showing?+
    Usually your plan does not include the feature. Office Scripts need a Microsoft 365 business plan. Your admin may also need to enable them. Check both the licence and the policy.