Skip to content

What are Office Scripts?

Office Scripts are a scripting feature for automating tasks in Excel.

They were originally available only in Excel on the web, but you can now also use them in Excel for Windows (version 2210 or later) and Excel for Mac.

They play a role similar to VBA macros in desktop Excel.

You write scripts in TypeScript. The syntax is very different from VBA, but if you’re familiar with Google Apps Script, which is also based on JavaScript, you’ll find them easy to work with.

This chapter explains how to use Office Scripts with sample code, and points out how they differ from VBA along the way.

This site provides samples like the following.

Sample format
function main(workbook: ExcelScript.Workbook) {
const fruits: string[] = [];
fruits.push('Apple', 'Orange', 'Banana');
const labels = fruits.map<string>((fruit, i) => {
return `Fruit #${i + 1} is ${fruit}`;
});
}

All articles

26 articles

Topics

  • Differences from VBA

    Explains the differences between Office Scripts, which you can use to automate Excel, and VBA in the desktop version of Excel.

  • Working with arrays

    Many people struggle with arrays in VBA, but Office Scripts let you work with arrays much more flexibly. This article gives a systematic overview of how to work with arrays in Office Scripts.

  • Implement asynchronous processing

    This article gives an overview of asynchronous processing with async/await and with Promise, and explains how to implement it in Office Scripts, with sample code.

  • Working with dates

    Explains how to get the current date, add to and subtract from dates, and find the difference between two dates. This article gives a systematic overview of working with dates, with sample code.

  • 月末日・月初日の取得

    オフィススクリプトを使って、月末日や月初日、月の日数を Date オブジェクトで求める方法と、0 日を指定すると前月の末日になる仕組みを、サンプルコードと併せて解説します。

  • n か月後の日付

    オフィススクリプトを使って、1 か月後や n か月後の日付を求める方法と、月末の日付に月を加算したときに翌々月にずれてしまう問題への対処法を、サンプルコードと併せて解説します。

  • Regular expressions

    This article gives a systematic overview of using regular expressions in Office Scripts, including checking whether strings match, and replacing and splitting strings.

  • Pausing execution

    When you write a program that depends on external processes, you may sometimes want to pause processing for a certain amount of time. This article explains how to implement a wait for a specified number of seconds in Office Scripts, as an alternative to Application.Wait or Sleep (Windows API) in VBA.

Basic syntax

  • Conditional statements

    Shows how to write conditional branches (if and switch statements) in Office Scripts for both single and multiple conditions, with sample code.

  • Loops (iteration)

    Explains how to implement loops in Office Scripts using for statements, while statements, and forEach, with sample code.

Controll excel books

  • Add a worksheet

    Explains how to add a worksheet from Office Scripts, with sample code.

  • Get a worksheet

    Explains how to get a specific worksheet in Office Scripts, with sample code.

Controll Excel sheets

  • Activate a worksheet

    Explains how to make a specific worksheet active in Office Scripts, with sample code.

  • Copy a worksheet

    Explains how to copy a specific worksheet in Office Scripts.

  • Get the last row

    Explains how to get the last row of a specific worksheet in Office Scripts, with sample code.

  • 最終列の取得

    オフィススクリプトを使って、特定のワークシートでデータが入力されている最終列を取得する方法を、サンプルコードと併せて解説します。

Control ranges

  • セルの値の取得と書き込み

    オフィススクリプトを使って、特定のセルの値を取得する方法と、セルに値を書き込む方法を、サンプルコードと併せて解説します。

  • 範囲の値の一括取得と書き込み

    オフィススクリプトを使って、セル範囲の値を 2 次元配列としてまとめて取得・書き込みする方法を、サンプルコードと併せて解説します。

  • 行番号・列番号でセルを指定する

    オフィススクリプトを使って、A1 形式の番地ではなく、行番号・列番号でセルや範囲を指定する方法を、VBA の Cells との違いと併せて解説します。

Controll string

  • 文字列の検索

    オフィススクリプトを使って、文字列に特定の文字が含まれているか調べる方法と、見つかった位置を使って区切り文字の前後を切り出す方法を、サンプルコードと併せて解説します。

  • Replace strings

    Explains how to replace a specific string with another string, and what to watch out for when the replacement doesn't work as expected, with sample code.

  • 文字列の分割

    オフィススクリプトを使って、カンマや空白などの区切り文字で文字列を分割する方法と、複数の区切り文字を正規表現で指定する方法、分割した値をセルに書き込む方法を、サンプルコードと併せて解説します。

  • Extract part of a string

    Explains how to extract part of a string in Office Scripts, the equivalent of Left, Mid, and Right in VBA, with sample code.

  • Convert between strings and numbers

    Explains how to convert strings to numbers and numbers to strings in Office Scripts, with sample code.

  • 空白の削除

    オフィススクリプトを使って、文字列の前後の空白や、文字列に含まれるすべての空白を削除する方法、連続する空白を 1 つにまとめる方法を、サンプルコードと併せて解説します。

  • 入力値の形式チェック

    オフィススクリプトを使って、郵便番号や電話番号、半角数字のみといった入力値の形式を正規表現でチェックする方法と、形式が正しくないセルに色を付ける方法を、サンプルコードと併せて解説します。