- Microsoft Office Excel Online Templates
- Free Office Online Excel Courses
- Office Online Excel Macros
- Windows Office Excel Online
- Excel Online Office 365
Open Excel File Online. To open Excel xlsx and xls file, you need to follow the below-mentioned process. Press the start button. Select the option “All Programs”. Look for the Microsoft Office and click on it. Find Microsoft Excel in the submenu and click on it. After clicking on it, you will find an excel window with a grid format. Office Scripts in Excel on the web is in public preview. The functionality described here is subject to change as the feature develops. You can submit feedback on Office Scripts through the Help Feedback button in Excel. You can submit feedback about the documentation to the OfficeDev/office-scripts-docs GitHub repository.
This tutorial teaches you the basics of recording, editing, and writing an Office Script for Excel on the web. You'll record a script that applies some formatting to a sales record worksheet. You'll then edit the recorded script to apply more formatting, create a table, and sort that table. This record-then-edit pattern is an important tool to see what your Excel actions look like as code.
Prerequisites
Before starting this tutorial, you'll need access to Office Scripts, which requires the following:
- Excel on the web.
- A Microsoft 365 commercial or EDU plan that includes Office Scripts.
Important
This tutorial is intended for people with beginner to intermediate-level knowledge of JavaScript or TypeScript. If you're new to JavaScript, we recommend starting with the Mozilla JavaScript tutorial. Visit Office Scripts Code Editor environment to learn more about the script environment.
Add data and record a basic script
First, we'll need some data and a small starting script.
Microsoft Office Excel Online Templates
Create a new workbook in Excel for the Web.
Copy the following fruit sales data and paste it into the worksheet, starting at cell A1.
Fruit 2018 2019 Oranges 1000 1200 Lemons 800 900 Limes 600 500 Grapefruits 900 700 Open the Automate tab. If you do not see the Automate tab, check the ribbon overflow by pressing the drop-down arrow.
Press the Record Actions button.
Select cells A2:C2 (the 'Oranges' row) and set the fill color to orange.
Stop the recording by pressing the Stop button.
Fill in the Script Name field with a memorable name.
Optional: Fill in the Description field with a meaningful description. This is used to provide context as to what the script does. For the tutorial, you can use 'Color-codes rows of a table'.
Tip
You can edit a script's description later from the Script Details pane, which is located under the Code Editor's ... menu.
Save the script by pressing the Save button.
Your worksheet should look like this (don't worry if the color is different):
Edit an existing script
The previous script colored the 'Oranges' row to be orange. Let's add a yellow row for the 'Lemons'.
From the now-open Details pane, press the Edit button.
You should see something similar to this code:
This code gets the current worksheet from the workbook. Then, it sets the fill color of the range A2:C2.
Ranges are a fundamental part of Office Scripts in Excel on the web. A range is a contiguous, rectangular block of cells that contains values, formula, and formatting. They are the basic structure of cells through which you'll perform most of your scripting tasks.
Add the following line to the end of the script (between where the
color
is set and the closing}
):Test the script by pressing Run. Your workbook should now look like this:
Create a table
Let's convert this fruit sales data into a table. We'll use our script for the entire process.
Add the following line to the end of the script (before the closing
}
):That call returns a
Table
object. Let's use that table to sort the data. We'll sort the data in ascending order based on the values in the 'Fruit' column. Add the following line after the table creation:Your script should look like this:
Tables have a
TableSort
object, accessed through theTable.getSort
method. You can apply sorting criteria to that object. Theapply
method takes in an array ofSortField
objects. In this case, we only have one sorting criteria, so we only use oneSortField
.key: 0
sets the column with the sort-defining values to '0' (which is the first column on the table, A in this case).ascending: true
sorts the data in ascending order (instead of descending order).Run the script. You should see a table like this:
Note
If you re-run the script, you'll get an error. This is because you cannot create a table on top of another table. However, you can run the script on a different worksheet or workbook.
Re-run the script
- Create a new worksheet in the current workbook.
- Copy the fruit data from the beginning of the tutorial and paste it into the new worksheet, starting at cell A1.
- Run the script.
Next steps
Complete the Read workbook data with Office Scripts in Excel on the web tutorial. It teaches you how to read data from a workbook with an Office Script.
-->This article will introduce you to the technical aspects of Office Scripts. You'll learn how the Excel objects work together and how the Code Editor synchronizes with a workbook.
Note
Office Scripts in Excel on the web is in public preview. The functionality described here is subject to change as the feature develops. You can submit feedback on Office Scripts through the Help > Feedback button in Excel. You can submit feedback about the documentation to the OfficeDev/office-scripts-docs GitHub repository.
main
function
Each Office Script must contain a main
function with the ExcelScript.Workbook
type as its first parameter. When the function is executed, the Excel application invokes this main
function by providing the workbook as its first parameter. Hence, it is important to not modify the basic signature of the main
function once you have either recorded the script or created a new script from the code editor.
The code inside the main
function runs when the script is run. main
can call other functions in your script, but code that's not contained in a function will not run.
Caution
If your main
function looks like async function main(context: Excel.RequestContext)
, your script is using the older async API model. For more information (including how to convert your script to the current API model), refer to Support older Office Scripts that use the Async APIs.
Object model
To write a script, you need to understand how the Office Script APIs fit together. The components of a workbook have specific relations to one another. In many ways, these relations match those of the Excel UI.
- A Workbook contains one or more Worksheets.
- A Worksheet gives access to cells through Range objects.
- A Range represents a group of contiguous cells.
- Ranges are used to create and place Tables, Charts, Shapes, and other data visualization or organization objects.
- A Worksheet contains collections of those data objects that are present in the individual sheet.
- Workbooks contain collections of some of those data objects (such as Tables) for the entire Workbook.
Workbook
Every script is provided a workbook
object of type Workbook
by the main
function. This represents the top level object through which your script interacts with the Excel workbook.
The following script gets the active worksheet from the workbook and logs its name.
Ranges
A range is a group of contiguous cells in the workbook. Scripts typically use A1-style notation (e.g., B3 for the single cell in column B and row 3 or C2:F4 for the cells from columns C through F and rows 2 through 4) to define ranges.
Ranges have three core properties: values, formulas, and format. These properties get or set the cell values, formulas to be evaluated, and the visual formatting of the cells. They are accessed through getValues
, getFormulas
, and getFormat
. Values and formulas can be changed with setValues
and setFormulas
, while the format is a RangeFormat
object comprised of several smaller objects that are individually set.
Ranges use two-dimensional arrays to manage information. Read the Working with ranges section of Using built-in JavaScript objects in Office Scripts for more information on handling those arrays in the Office Scripts framework.
Free Office Online Excel Courses
Range sample
The following sample shows how to create sales records. This script uses Range
objects to set the values, formulas, and parts of the format.
Running this script creates the following data in the current worksheet:
Charts, tables, and other data objects
Office Online Excel Macros
Scripts can create and manipulate the data structures and visualizations within Excel. Tables and charts are two of the more commonly used objects, but the APIs support PivotTables, shapes, images, and more. These are stored in collections, which will be discussed later in this article.
Creating a table
Create tables by using.
Adding Excel objects with a script
You can programmatically add document objects, such as tables or charts, by calling the corresponding add
method available on the parent object.
Note
Do not manually add objects to collection arrays. Use the add
methods on the parent objects For example, add a Table
to a Worksheet
with the Worksheet.addTable
method.
The following script creates a table in Excel on the first worksheet in the workbook. Note that the created table is returned by the addTable
method.
Removing Excel objects with a script
To delete an object, call the object's delete
method.
Note
As with adding objects, do not manually remove objects from collection arrays. Use the delete
methods on the collection-type objects. For example, remove a Table
from a Worksheet
using Table.delete
.
The following script removes the first worksheet in the workbook.
Windows Office Excel Online
Further reading on the object model
The Office Scripts API reference documentation is a comprehensive listing of the objects used in Office Scripts. There, you can use the table of contents to navigate to any class you'd like to learn more about. The following are several commonly viewed pages.