Skip to main content

How to Create and Run an Office Script in Excel

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

An Office Script is a short program, written in TypeScript, that does things in an Excel workbook for you: fill cells, add rows, format a table. You write it once in Excel's Code Editor, give it a name, and run it whenever you need it.

This guide creates the smallest useful script: Say Hello writes Hello from Office Scripts! in cell A1. Everything happens in Excel for the web, in a blank workbook.

Prerequisites​

  • A Microsoft 365 work or school licence that includes Office Scripts, such as Office 365 E3 or E5. Microsoft lists them in Platform limits and requirements. Without one, Excel has no Automate tab.

Step 1: Open the Code Editor​

Open the workbook in Excel for the web and select the Automate tab. Its first group, Office Scripts, has New Script:

The Automate tab of Excel for the web, with New Script outlined in red, then View Scripts, the Office Scripts Gallery of sample scripts and Automation Templates

Select New Script, then Create in Code Editor. The other choice, Create from Recording, records what you click instead.

The New Script menu open with two choices, Create in Code Editor outlined in red and Create from Recording

The first time, the Office Scripts pane says Turn actions into scripts and offers Record actions or Write a script. Select Write a script:

The Office Scripts pane with the message Turn actions into scripts, a green Record actions button and Write a script outlined in red

Step 2: Write the Script​

The editor opens with a new script called Script and some starting code that colours the selected cell yellow:

The Office Scripts pane with a script named Script and its starting code: function main(workbook: ExcelScript.Workbook) that gets the active cell and worksheet and sets the selected cell's fill colour to yellow, ending with a TODO comment

Select all the code and replace it with this:

Say Hello
function main(workbook: ExcelScript.Workbook) {
// Write a greeting in cell A1 of the open sheet.
const sheet = workbook.getActiveWorksheet();
sheet.getRange("A1").setValue("Hello from Office Scripts!");
}
  • function main(workbook: ExcelScript.Workbook) is where every Office Script starts. Excel runs main and hands it the workbook the script runs on.
  • workbook.getActiveWorksheet() is the sheet that's open.
  • getRange("A1") is one cell, and setValue(...) writes into it.
  • The line that starts with // is a comment, a note for people that the script ignores.

Step 3: Name the Script​

Select the name Script at the top of the pane, type Say Hello and press Enter. Renaming saves the script:

The script's name box with Say Hello typed in it, outlined in red, beside the Format, Undo, Save and Run buttons

When it saved, the editor also tidied the code, putting four spaces at the start of each line inside main.

Step 4: Run It​

Select Run, the triangle to the right of the name:

The script's header with the name Say Hello and the Run button outlined in red

A message under the name says The script ran successfully.:

The script's header with the message The script ran successfully. in a green box under it

And cell A1 holds the text:

The grid with A1 selected and Hello from Office Scripts! in it, running into B1, and the same text in the formula bar

Step 5: Run It Again Later​

Close the pane, and next time open it with Automate, View Scripts. Back on All scripts, the Recent tab lists the script, with Edit, Rename and Run:

The Office Scripts pane with the tabs Recent (2), This workbook (0) and Samples (17), and a card for Say Hello, created Nov 20, 2026 by Admin User, with Edit, Rename and Run, Run outlined in red

A script isn't part of the workbook: Excel saves it as a file of its own, Say Hello.osts, in the Documents/Office Scripts folder of your OneDrive, and you can run it on any workbook you open. Microsoft explains this in Office Scripts file storage and ownership. To keep a script with a team instead, see How to Save an Office Script to SharePoint.

Common Mistakes​

MistakeWhat happensDo this instead
Renaming mainExcel can't find where the script startsKeep function main(workbook: ExcelScript.Workbook)
Pasting new code under the starting codeTwo main functions, and the editor shows errorsSelect all the starting code first, then paste
Expecting the script to be inside the workbookSending the workbook to someone doesn't send the scriptIt's a file in your OneDrive; share it or save it to SharePoint
Running it with the wrong sheet opengetActiveWorksheet() writes on whichever sheet is openOpen the right sheet first, or get a sheet by its name

Troubleshooting​

SymptomLikely causeFix
No Automate tabThe licence doesn't include Office Scripts, or an admin turned them offMicrosoft's Automate tab not appearing steps
Run shows an errorA typing mistake in the codeRead the message: it names the line
The text is in the wrong cellAnother cell address in getRangeCheck the address in quotes

FAQ​

Do I need to know TypeScript?​

Not for small scripts like this one. TypeScript is JavaScript with types; a script is a few lines that call Excel's own commands, such as getRange and setValue.

Can I record a script instead of writing it?​

Yes: Create from Recording turns what you click into code, which you can then edit in the same Code Editor.

Can other people run my script?​

Not straight away: it's in your OneDrive. Microsoft's sharing guide shows how to share one with a workbook, and a script saved in SharePoint is shared with the people who can open that library.

Conclusion​

Automate, New Script, Create in Code Editor opens a new script. Its code goes inside function main(workbook: ExcelScript.Workbook), renaming it saves it, and Run runs it on the open workbook. From here, a script can write many cells at once, add table rows, or be run by Power Automate.

Additional Resources​