What is Office Scripts?

Office Scripts is a feature in Excel for the web that allows users to automate tasks by writing and running scripts. These scripts are written in JavaScript and leverage the Excel JavaScript API to interact with workbook data, perform calculations, and modify the structure of spreadsheets.

Features

Steps to Create Office Script

Step 1. Open Excel for the web.

Go to the "Automate" tab in the ribbon.

Automate

Step 2. Office Scripts uses TypeScript (a superset of JavaScript), which provides a strongly typed syntax to avoid common mistakes.

Click on the "New Script".

New Script

Step 3. Add the following script to the right-side script panel.

TypeScript
function main(workbook: ExcelScript.Workbook) {

    let selectedSheet = workbook.getActiveWorksheet();

    selectedSheet.getRange("A1").setValue("Hello, Office Scripts!"); // sets the text in cell A1

    let selectedCell = workbook.getActiveCell(); // gets the active cell

    selectedSheet = workbook.getActiveWorksheet(); // gets active worksheet

    selectedCell.getFormat().getFill().setColor("yellow"); // sets fill color to yellow for the selected cell

    selectedSheet.getRange("A1").getFormat().setColumnWidth(100); // sets the column width to 100

}

Excel Script

Step 4. Save the script by clicking "Save Script" .

After writing your script, Save it, and you can run it directly from the Code Editor or assign it to a button in your worksheet for easier access.

Save Script

Step 5. Click on the "Run" to run the script and see the output in the workbook.

Run

You can see the output like this.

Output

How office script is useful?

  1. Data Cleanup: Automatically remove duplicates, correct formatting, or standardize data entries.
  2. Report Generation: Generate monthly reports by aggregating data and formatting them according to predefined templates.
  3. Data Integration: Pull data from multiple sheets or external sources and consolidate it into a single view.
  4. Custom Calculations: Perform complex calculations or data transformations that are not natively supported in Excel functions.