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 read table script
Use this pattern as a starting point for read excel table rows and columns in office scripts workflows.
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.