Steps To Be Followed
Steps To Be Followed
Steps To Be Followed
9.2 Parameters 77
Declaring Parameters 77
Default Parameter Values 77
Optional Parameters 78
This chapter introduces you to Office Scripts, beginning with the answers to a few simple questions.
Office Scripts are small programs used to automate tasks in Microsoft Excel. You write code in the
TypeScript language and you can either record the steps or write your program from scratch.
According to Microsoft, these are the three requirements for using Office Scripts:
3) Any commercial or educational Microsoft 365 license with access to the Microsoft 365 Office
desktop apps.
If you’re thinking that Office Scripts sound a lot like VBA macros, you’re right! In fact, there are three
main ways to automate Excel, summarised in the table below:
Office Scripts allow you to do some things you can’t do with VBA macros, as described in the table
below:
Feature Description
Excel online You can use Office Scripts to automate workbooks in the web version of Excel.
Power Automate You can run Office Scripts from a Power Automate flow.
External APIs Office Scripts support calls to external APIs which can provide data to your files.
Better security An Office Script only has access to the workbook in which it is running, unlike VBA
which has access to your entire computer.
Office Scripts have several limitations compared to VBA and Office Add-Ins, as described in the
table below:
Feature Description
Excel only Currently, Office Scripts can only be used in Excel. Office Add-Ins can also work with
Word, PowerPoint, Outlook, OneNote and Project. VBA can use OLE and COM
libraries to control a variety of other applications, including the Microsoft Office apps.
No events VBA and Office Add-Ins can respond to events to make code run automatically. Office
Scripts must be run explicitly by the user.
Single workbook An Office Script only has access to the workbook in which it is running. Even something
as simple as copying a value from one workbook to another requires the use of a Power
Automate flow.
User interface Office Scripts don’t have access to any of the user interface elements of Excel. This
means that you can’t display dialog boxes or similar UI elements. If you need to create
a user-interface it would be better to use VBA or to create a full Office Add-In.
This section shows you the basic things you need to do before you can write your first script.
The Automate tab of the ribbon contains the tools you need to write Office Scripts.
Select Automate in
the ribbon to see
tools for working
with Office Scripts.
If you’re using the desktop version of Excel, the Automate tab may not appear automatically. You
can see how to display it in the diagram below:
You edit Office Scripts in the Code Editor window in Excel. You can open the Code Editor in several
ways; a simple option is to choose Automate | All Scripts from the Excel ribbon.
In the desktop version of Excel you can move and resize the Code Editor window by clicking and
dragging its title bar.
This section shows you how to write a simple program to add text to a cell and apply some formatting
to get used to the basics of writing code.
Start by creating a new workbook in either the desktop or online version of Excel and then choose
to create a new Office Script.
You can see the basic structure of your new script in the diagram below:
The script contains a function called main The function has a parameter called workbook which gives your
which acts as a container for your code. code access to the Excel file that you currently have open.
The function contains a single line of code consisting of a comment. You can use comments to annotate your code.
Adding a Comment
You can add your own comments to your code to provide helpful reminders of what it is meant to do.
Click at the end of the line 2 then press Enter twice to Type two forward slashes to begin the comment and
provide a new line on which to write your comment. follow these with anything you feel is appropriate.
Our first instruction will write the name Wise Owl into the currently selected cell in the workbook. To
do this:
1) On a new line in the script, begin typing the word workbook (take care when doing this –
TypeScript is a case-sensitive language!).
2) After the word workbook, enter a full stop and begin typing getActiveCell.
4) Enter another full stop followed by setValue then an open round bracket and double-quotes.
5) Between the double quotes, enter the name you want to write into the cell.
You can continue adding as many instructions as you need to complete your program. The diagram
below shows the complete script with two extra instructions and relevant comments:
You can see how to run your finished script in the diagram below:
Click Run at the top of the Code Editor to run the script that you
currently have open.
Wise Experienced VBA programmers should be careful not to press F5 to run your code.
Owl’s
Hint
In the online version of Excel this will refresh the page and close the Code Editor!
To change Code Editor settings you first need to open a script file.
Click ... at the top right of the Code Editor and choose Editor Settings
from the menu.
The Folding option allows you to collapse and expand sections of your
code contained within different types of brackets.
With the Folding option enabled you can This option allows you to edit
click arrows like this one to collapse and a script in the online version
expand sections of your script. of Visual Studio Code.
If you’re working in the online version of Excel, you can enable the Visual Studio Code connection
option. You can then open a script in the Visual Studio Code web application, as shown below:
You can edit the script You can close the Visual Studio tab or click
in the new tab. Close to return to the Code Editor in Excel.
This section explains the basics of working with Office Script files.
You can see your recently used and sample scripts in the Automate tab in the Excel ribbon.
To see all your scripts, choose Automate | All Scripts from the Excel ribbon.
Click View more scripts to open a dialog box which lets you browse to
other folders in OneDrive and SharePoint.
You can use a script’s More options menu to perform several useful tasks on it.
The simplest way to create a new script is to choose Automate | New Script from the Excel ribbon.
You can open an existing script to edit its code as shown in the diagram below:
You can rename a script file, even if you’re currently editing it.
Click the script name or choose Rename from the More options menu.
Type the new name for the script file and then press Enter to rename it.
Deleting a Script
You can delete a script from the More options menu, even if you are currently editing it.
By default, Office Script files are saved in the Documents/Office Scripts folder in OneDrive.
You can save the script that you’re currently working on in the default folder as shown below:
You can choose to save your script in a different location as shown below:
Scripts saved to your OneDrive are accessible only to you. If you’d like others to use your scripts,
you’ll need to share them in some way.
Sharing a Script
The first step in sharing a script is to share it in a workbook. To do this, first open the workbook you
want to share the script in.
You’ll also need to give other people access to the workbook, as shown in the diagram below:
The diagram below shows how you can reverse the process of sharing an Office Script:
Microsoft
Excel ✓ ✓ ✓ ✓
VBA
✓ ✓ ✓ ✓
OFFICE
macros
365
Office
Scripts ✓ ✓
Microsoft
Access ✓
Power BI
and DAX ✓ ✓ ✓ ✓
PLATFORM
POWER
Power
Apps ✓ ✓
Power
Automate ✓ ✓ ✓ ✓
Reporting
Services ✓ ✓ ✓ ✓
Report
✓ ✓ ✓
SERVER
Builder
SQL
Integration
Services ✓ ✓ ✓ ✓
Analysis
Services ✓ ✓
SQL ✓ ✓ ✓ ✓
LANGUAGES
CODING
Visual C# ✓ ✓ ✓ ✓
Python ✓ ✓ ✓ ✓