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.
📄️ Asynchronous process
The outline of asynchronous processing and how to implement the asynchronous processing in the Office Scripts for Async/AWAIT and Promise, respectively.
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.
/**
* @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.
async function main(workbook: ExcelScript.Workbook) {
console.log('Processing has started');
await wait(1000);
console.log('1 second has passed');
}