Ravindra BagaleCourses & study guides मराठी Track your progress

12. Macros and VBA

12.17 Office Scripts: What They Are and Who Can Use Them

Example first. Every morning Shraddha opens the shared Orders workbook in her browser, makes the header bold, colours the late orders and writes a count in K1. Her VBA macros from this chapter cannot help, because VBA does not run in Excel on the web. Office Scripts are the web-friendly answer: small programs, saved in OneDrive, that you record or write from the Automate tab and can even run from Power Automate while nobody has the file open.

Office Scripts — in 30 seconds

What: Office Scripts are small programs written in TypeScript (a typed form of JavaScript) that automate steps in an Excel workbook. You create them from the Automate tab with the Action Recorder or the Code Editor.

Why: They work in Excel on the web, they are stored in the cloud with your account, colleagues can run them with a button, and Power Automate can run them on a schedule.

Tiny example: Every script starts with function main(workbook: ExcelScript.Workbook). Excel passes the open workbook in as workbook, and your code works on it.

First look at the data (before → after):

Office Scripts: before and after tables, fictional FreshGo Maharashtra data

Expected result — With an Office Script

Step What happens
1 Click Run (or a button on the sheet)
2 The script does steps 2 to 4 in about a second
3 Same result every day, for every colleague

About this data: Teaching sample for a fictional shop “HomeNest Supplies”. Figures and names (e.g. Vikram Rao) are made up for this lesson on office scripts what they are and who can use.

Before table and formula as text

Before — Every morning by hand

Step Manual action
1 Open the workbook in the browser
2 Bold and colour the header
3 Find orders over 15 minutes and colour them
4 Count them and type the count in K1

About this data: Demo table only — not real customer or company data. Built to practise office scripts what they are and who can use.

Formula / click path

Automate › New Script › Create in Code Editor
function main(workbook: ExcelScript.Workbook) { … }
Run

Do it step by step:

  1. Save the Orders workbook in OneDrive for Business or SharePoint and open it in Excel on the web.
  2. Select the Automate tab. The Office Scripts group shows New Script and View Scripts.
  3. Select New Script › Create in Code Editor. The Code Editor pane opens with an empty main function.
  4. Replace the code with the small script below and select Run.
  5. Check that K1 on the Orders sheet now says Checked by script and that the Output area shows the message.
  6. Select the script name at the top of the pane to rename it, for example Hello Orders.

Version note

Office Scripts need a work or school Microsoft 365 licence (Microsoft lists Office 365 Business, Business Premium, ProPlus, ProPlus for Devices, A3, A5, E1, E3, E5 and F3), the workbook stored in OneDrive for Business or SharePoint, and connected experiences turned on by your admin. They run in Excel on the web, Excel for Windows (Version 2210 or later) and Excel for Mac. Personal and Family plans can try them only as a preview through the Microsoft 365 Insider programme. If you do not see the Automate tab, your licence or your admin settings do not allow it yet.

Your first script, line by line

function main(workbook: ExcelScript.Workbook) {
  // Get the Orders sheet by its name
  const sheet = workbook.getWorksheet("Orders");
  // Write a note in K1
  sheet.getRange("K1").setValue("Checked by script");
  // Show a message in the Output pane of the Code Editor
  console.log("Hello from Office Scripts");
}
Line Meaning in plain English
function main(workbook: ExcelScript.Workbook) Every script has one main function. Excel calls it and hands over the workbook.
workbook.getWorksheet("Orders") Get the sheet named Orders. Using names (not "the active sheet") makes scripts safe to run from Power Automate later.
sheet.getRange("K1").setValue(…) Write a value into K1.
console.log(…) Print a message in the Output area of the Code Editor. Useful for checking your logic.
// … A comment. Excel ignores it; people read it.

About this data: Made-up rows so you can copy the pattern for office scripts what they are and who can use. Currencies may use ₹, £, € or $.

What you can do with Office Scripts (from Microsoft's overview)

  • Action Recorder: record your clicks and typing and turn them into a script (lesson 12.18).
  • Code Editor: edit recorded scripts or write new ones, with if/else and loops that the Excel UI does not have (lesson 12.19).
  • Copy as code: record a few actions and copy them as script code without saving a whole script.
  • Buttons: add a button to the sheet so colleagues can run a script with one click.
  • Sharing: share a script with the people who can open the workbook.
  • Power Automate: run a script from a flow, on a schedule or when something happens (lesson 12.20).

Where scripts are saved. A script belongs to you, not to the workbook: it is stored as a file in your OneDrive (Microsoft's file-storage page describes the .osts files in a Documents/Office Scripts folder). That is why sharing a script is a separate step from sharing the workbook.

Limits worth knowing now (Microsoft's platform-limits page): a single read or write between the script and the workbook can carry about 5 MB, and one range can hold at most 5 million cells. For a training sheet of 10 orders this never matters; for 2 lakh rows, read and write in blocks.

Microsoft sources: Office Scripts in Excel (overview); Platform limits, requirements and error messages for Office Scripts.

Practice task (try it before you open the answer):

  1. Change the hello script so it writes today's message "Report ready" in K2 instead of K1.
  2. Add a second console.log that prints the text "Done".
  3. Run it and say where each message appears.
Answer
  1. Change the range line to sheet.getRange("K2").setValue("Report ready");.
  2. Add console.log("Done"); before the closing brace.
  3. "Report ready" appears in cell K2 of the Orders sheet; both log messages appear in the Output area of the Code Editor, not in the sheet.

Ravindra Bagale's Tip

Ask your IT admin first whether Office Scripts are switched on for your organisation. Ten minutes of checking saves an afternoon of searching for a missing Automate tab.

Worked example — office scripts what they are and who can use

Why

Priya Deshmukh in Pune and Carlos Rivera abroad both need a clear, repeatable way to use office scripts what they are and who can use on a Maple Bakery Co sheet without breaking totals.

What

Office Scripts What They Are And Who Can Use is the Excel idea this page teaches. We will walk one tiny example end to end.

How

  1. Open a blank sheet. In A1 type a short label for office scripts what they are and who can use.
  2. Enter a small made-up value for Priya Deshmukh using £ where money appears.
  3. Add a second value for Carlos Rivera using ₹ so you see mixed currencies stay numeric.
  4. Apply the method from this lesson step by step (menu or formula as shown above).
  5. Check the result still works inside SUM or a simple check cell.

Formula / action

Follow the formula or ribbon steps shown in this lesson (keep values numeric).
Result: The demo cell shows the expected result for Maple Bakery Co's sample row.
Meaning: Once office scripts what they are and who can use is correct on one row, copy the same pattern down the column.

Practice tasks (10+)

Try these on a blank sheet. Open a hint only if you are stuck. No quiz — just practice.

1. Rebuild the smallest example of office scripts what they are and who can use on a blank sheet for Orbit Logistics.

Hint: Use only the steps on this page. Keep numbers numeric.

2. Change one input for Vikram Rao and confirm the result updates.

Hint: If the result is stuck, check absolute vs relative references.

3. Repeat the same steps with a £ or € value for Sophie Martin.

Hint: Formatting must not break SUM.

4. Write the result meaning in one plain sentence next to the cell.

Hint: Imagine explaining it to a new joiners.

5. Add a second row and fill the method down.

Hint: Double-click the fill handle if the pattern is consistent.

6. Break the example on purpose, then fix it using this lesson.

Hint: Common break: text instead of a number.

7. Apply office scripts what they are and who can use to a 5-row made-up Orbit Logistics table.

Hint: Keep column headers clear.

8. Check your answer with a simple total or COUNT.

Hint: A wrong total usually means a format or key mismatch.

9. Swap ₹ and $ on one column and re-check totals.

Hint: Stored values stay the same; only the display changes.

10. List two mistakes beginners make with office scripts what they are and who can use.

Hint: Look back at the tip box on this page.

11. Teach the steps out loud in under one minute.

Hint: Why → What → How.

12. Save the sheet as OrbitLogist_practice.xlsx.

Hint: Use a clear file name for class.

Loading practice tasks...

  1. Last used row (Easy): Which row number is the last filled row? The headings are in row 1.
  2. Late orders (Easy): How many rows have Status "Late"?
  3. Total amount (Easy): Add up the Amount column.
  4. Worst delay (Easy): What is the largest number in the Days late column?
  5. Late orders in Pune (Medium): How many orders are Late and in City "Pune"?
  6. Customer for an order (Medium): Find the Customer for Order ID "DL-704".
  7. Money stuck in late orders (Medium): Add up the Amount column for rows where Status is "Late".
  8. Big orders (Medium): How many rows have an Amount of 2000 or more?
  9. Message text (Medium): Build the text "N late orders", where N is the count of Late rows.
  10. Average delay of late orders (Hard): What is the average Days late for rows with Status "Late"? Round to 1 decimal.
  11. Tag for the first row (Hard): For the first row, return "Chase" if Days late is 3 or more, else "OK".
  12. Warning or all clear (Hard): Return "Warning" if any order is 3 or more days late, else "All clear".

Quick revision cards

Three standalone cards. Each one explains the idea with its own example. Scan the QR to open this lesson.

Revision card 1: What is Office Scripts What They Are And Who Can Use?
Card 1: What is Office Scripts What They Are And Who Can Use? · Download PNG
Revision card 2: Tiny example — Office Scripts What They Are And Who Can Use
Card 2: Tiny example — Office Scripts What They Are And Who Can Use · Download PNG
Revision card 3: Checklist — Office Scripts What They Are And Who Can Use
Card 3: Checklist — Office Scripts What They Are And Who Can Use · Download PNG