Excel Course
Office Scripts Introduction
Office Scripts is Excel's modern automation language — built on TypeScript, running in the browser and in Excel for Windows, and designed to work seamlessly with Power Automate for scheduled and triggered workflows. If you have used VBA before, Office Scripts will feel familiar in purpose but different in almost every detail. If you have never automated Excel before, Office Scripts is a friendlier starting point than VBA. This lesson covers what Office Scripts is, how it compares to VBA, where to find it, and how to write and run your first script.
What Office Scripts Is — and What It Is Not
Office Scripts is a scripting environment built into Excel on the web and Excel for Microsoft 365 on Windows. Scripts are written in TypeScript — a typed superset of JavaScript — and run against the Excel object model through the ExcelScript API. Every script receives a workbook parameter, through which it can access sheets, ranges, tables, charts, and PivotTables programmatically.
| Office Scripts | VBA | |
|---|---|---|
| Language | TypeScript (JavaScript superset) | Visual Basic for Applications |
| Where it runs | Excel for Web, Excel 365 Windows | Excel Windows only (desktop) |
| Storage | OneDrive / SharePoint — travels with your account | Embedded in the workbook or Personal.xlsb |
| Power Automate | Native integration — run scripts on a schedule or trigger | No native cloud integration |
| Security | Sandboxed — cannot access local file system or OS | Full OS access — higher security risk |
| Sharing | Share scripts from OneDrive like any file | Must distribute workbook or module export |
| Recording | Action Recorder built-in — generates TypeScript code | Macro Recorder built-in — generates VBA code |
| Best for | Cloud-based, team workflows, Power Automate pipelines | Complex desktop automation, legacy systems |
Finding the Office Scripts Editor
Office Scripts lives on the Automate tab in the Excel ribbon. If you do not see the Automate tab, it means either your Microsoft 365 subscription does not include it, or your admin has not enabled it for your organisation. It is available on Microsoft 365 Business Standard and above, and on all personal Microsoft 365 plans.
New Script → opens blank code editor
All Scripts → your OneDrive script library
The Office Scripts Code Editor
Clicking New Script opens the Code Editor pane on the right side of Excel. The editor is a full TypeScript environment with syntax highlighting, autocomplete, and an error panel. Every script starts with the same function signature — the main function that Excel calls when you run the script.
Every Office Script starts with this structure:
function main(workbook: ExcelScript.Workbook) {
// Your code goes here
}
Breaking it down:
function main → the entry point Excel calls when you run the script
workbook → the parameter name (convention is always "workbook")
ExcelScript.Workbook → the TypeScript type — gives you access to the full Excel API
{ } → the function body — everything between the braces runs in order
The workbook parameter is your gateway to everything:
workbook.getActiveSheet() → the currently selected sheet
workbook.getWorksheet("Sales") → a sheet by name
workbook.getSheets() → an array of all sheets
workbook.getActiveSheet().getRange("A1") → a specific cell or range
TypeScript basics you need for Office Scripts:
let x = 5; → declare a variable (mutable)
const name = "Alice"; → declare a constant (immutable)
// comment → single-line comment
/* comment */ → multi-line comment
console.log("hello"); → write to the script output log for debugging
2 let sheet = workbook.getActiveSheet();
3 sheet.getRange("A1").setValue("Hello!");
4 }
Your First Script — Writing Values and Formatting Cells
The best way to learn Office Scripts is to write small, practical scripts from the beginning. The first script every new learner should write does two things: writes a value into a cell, and applies basic formatting. These two operations cover the majority of what most automation scripts need to do.
Script 1 — Write a value and format a cell:
function main(workbook: ExcelScript.Workbook) {
// Get the active sheet
let sheet = workbook.getActiveSheet();
// Write a value to cell A1
sheet.getRange("A1").setValue("Sales Report");
// Make it bold and set font size
sheet.getRange("A1").getFormat().getFont().setBold(true);
sheet.getRange("A1").getFormat().getFont().setSize(16);
// Set background colour (hex colour string)
sheet.getRange("A1").getFormat().getFill().setColor("#7c3aed");
// Set font colour to white
sheet.getRange("A1").getFormat().getFont().setColor("#ffffff");
// Auto-fit column A width
sheet.getRange("A:A").getFormat().autofitColumns();
console.log("Formatting complete.");
}
Key methods used:
.getValue() / .setValue() → read or write a single cell value
.getValues() / .setValues() → read or write a 2D array (range)
.getFormat().getFont() → access font properties
.getFormat().getFill() → access background fill
.getFormat().autofitColumns() → auto-fit column width
Notice the chain of method calls: sheet.getRange("A1").getFormat().getFont().setBold(true). This is the pattern you will use constantly in Office Scripts — navigate down the object hierarchy (workbook → sheet → range → format → font) and call a method at the end. Each step returns an object that has further methods and properties. Once you see this pattern, reading and writing Office Scripts becomes very natural.
Reading Values From a Sheet
Scripts are most useful when they read data from the sheet, make decisions based on it, and write results back. Reading a single cell value and reading an entire range are both straightforward, but they use different methods and return different data types.
Reading a single cell:
function main(workbook: ExcelScript.Workbook) {
let sheet = workbook.getActiveSheet();
// Read a single value — returns a string | number | boolean
let cellValue = sheet.getRange("B2").getValue();
console.log("Cell B2 contains: " + cellValue);
}
Reading a range of values — returns a 2D array:
function main(workbook: ExcelScript.Workbook) {
let sheet = workbook.getActiveSheet();
// getValues() returns a 2D array: rows × columns
// e.g. A1:C3 → [[row1col1, row1col2, row1col3], [row2col1, ...], ...]
let data = sheet.getRange("A1:C5").getValues();
// Loop through rows
for (let row of data) {
// row is a 1D array: [col1value, col2value, col3value]
console.log(row[0] + " | " + row[1] + " | " + row[2]);
}
}
Finding the last used row (dynamic ranges):
function main(workbook: ExcelScript.Workbook) {
let sheet = workbook.getActiveSheet();
// getUsedRange() returns the rectangle containing all data
let usedRange = sheet.getUsedRange();
let rowCount = usedRange.getRowCount();
let colCount = usedRange.getColumnCount();
console.log("Data runs to row " + rowCount + ", column " + colCount);
}
The getUsedRange() method is one you will use in nearly every script that processes a variable-length dataset. Rather than hardcoding a range like "A1:C500", you ask Excel for the actual used range at runtime and work within it. This makes the script work correctly regardless of whether the dataset has 50 rows or 50,000.
Running, Saving and Managing Scripts
Running a script:
Click the ▶ Run button in the Code Editor
Or: Automate tab → All Scripts → click the script name → Run
Saving a script:
The editor auto-saves to OneDrive as you type
Scripts are saved as .osts files in OneDrive/Documents/Office Scripts
They are NOT saved inside the workbook — they are separate files
Naming scripts well:
Click the script name at the top of the editor to rename
Use descriptive names: "Format Sales Report Header" not "Script 1"
Sharing a script:
Open the Scripts panel → right-click the script → Share link
Anyone with the link and access to your OneDrive can use it
Or: pin the script to a workbook so others with the workbook can see it
Workbook tab → right-click script → Add to workbook
Deleting a script:
All Scripts panel → right-click → Delete
Or delete the .osts file directly from OneDrive
Keyboard shortcuts in the editor:
Ctrl+S → save
Ctrl+Z → undo
Ctrl+/ → toggle comment
F5 → run (in some environments)
🟠 Practice
Q1. Write a short Office Script that gets the active sheet and writes the text "Q1 Report" into cell A1, then makes it bold.
Q2. What method would you use to get the full range of data on a sheet without knowing how many rows it contains?
Q3. Where are Office Scripts stored, and what does this mean for sharing them with a colleague?
🟣 Quiz
Q1. What is the key advantage of Office Scripts over VBA for cloud-based work?
Q2. What does sheet.getRange("A1:C3").getValues() return?
Q3. You write an Office Script and want a colleague to use it. They open the same shared Excel workbook on SharePoint. Can they see and run your script?
Next up — Recording Scripts, where we use the Action Recorder to capture real Excel actions as TypeScript code, examine the generated output, and learn how to clean it up into a reusable, readable script.