Skip to main content

Waiting process

When using an Office Scripts, if you create a program that is affected by external processes, you may want to wait for the process only for a specific time.

This time, I will explain how to implement the process of waiting for the specified number of seconds using an Office Scripts.

About asynchronous processing

When implementing waiting processing, use a asynchronous function using async/await.

For more information about asynchronous functions, see the following page.

Define a function to wait for the specified seconds

Define a function that returns the Promise object that will obtain results after the specified number of seconds passes.

A function to wait for the specified seconds
/**
* @param ms Waiting time (milliseconds)
*/
function wait(ms: number) {
return new Promise<void>((resolve) => setTimeout(() => resolve(), ms));
}

Sample code

Use async/await to call the waiting function mentioned above.

A sample that continues processing after a certain period of time
async function main(workbook: ExcelScript.Workbook) {
console.log('Processing has started');

await wait(1000);

console.log('1 second has passed');
}