Syntax or pattern
This is the basic pattern for this type of automation. Copy the code, then replace workbook-specific names before running it.
function main(workbook: ExcelScript.Workbook) { ... }5 practical examples
Basic add formulas script
Use this pattern as a starting point for add formulas to tables, ranges, and calculated columns with office scripts.
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getActiveWorksheet();
const usedRange = sheet.getUsedRange();
if (!usedRange) return;
console.log(`Rows: ${usedRange.getRowCount()}`);
}Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.
Target a specific worksheet
Run the script against a named worksheet instead of the active sheet.
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getWorksheet('Report');
if (!sheet) return;
sheet.getRange('A1').setValue('Updated by Office Script');
}Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.
Work with an Excel table
Read or update a structured table in the workbook.
function main(workbook: ExcelScript.Workbook) {
const table = workbook.getTable('SalesTable');
const rows = table.getRangeBetweenHeaderAndTotal().getValues();
console.log(rows.length);
}Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.
Add a status timestamp
Write a simple timestamp so users know the automation has run.
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getActiveWorksheet();
sheet.getRange('A1').setValue('Last updated');
sheet.getRange('B1').setValue(new Date().toLocaleString());
}Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.
Simple safety check
Stop the script when the expected worksheet or table is missing.
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getWorksheet('Data');
if (!sheet) {
throw new Error('Data sheet was not found.');
}
}Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.
Safety notes before you run automation
- Test the code on a copy of the workbook first.
- Save important files before running code that deletes, moves, formats or emails data.
- Check worksheet names, table names and range references before copying the example.
Common mistakes to avoid
- Running automation on the only copy of a workbook instead of a backup.
- Assuming the active worksheet is always the sheet you meant to update.
- Forgetting that macros and scripts can change many cells very quickly.
FAQ
Is this Office Script safe to copy?
Use the code as a learning pattern. Test it on a copy of your workbook before running it on important files.
Why does the code not work in my workbook?
Usually the sheet, table, range, or column name is different. Update names and test on a small sample first.
Should I use VBA or Office Scripts?
Use VBA for desktop Excel macros and legacy workbooks. Use Office Scripts for Excel on the web and cloud automation workflows.
Here are some ideas for you
Optional resources that may help if you are learning Excel automation, building reports, or automating spreadsheets often.
- Excel VBA booksSee ideas
Learn macro patterns, objects, ranges and real automation examples.
- Office Scripts resourcesSee ideas
Practice automating Excel on the web with repeatable script patterns.
- Productivity keyboardsSee ideas
Helpful for writing code, formulas and documentation for long sessions.
- External monitorsSee ideas
View the workbook, VBA editor, script editor and notes side by side.
- External backup drivesSee ideas
Useful for keeping backup copies of automation workbooks and reports.
- Workflow notebooksSee ideas
Sketch macro steps, report logic and safety checks before coding.
Some links in this section may be affiliate links. Choose only what is useful for your own work.