How to Detect Document Closed With Office Js

72 Views Asked by At

I am creating a project that scan document in office after it closed.

In Vba and Outlook I was able to do that but I got stucked in Excel Online.

I am using Office js and it seems there is not any event for that and if I use "window.onbeforeunload" to detect when tab has closed but it doesn't scan.It seems there is not enough time to scan all the workbook before it clossed by tab.

Is there any way to run the code before closed and make Excel tab Open until scanning is open.

window.onbeforeunload = function() {
  Excel.run(async (context) => {

    const sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();

    let nonEmptyValues = [];
    //Pushing datas in the excel to an array
    for (let i = 0; i < sheets.items.length; i++) {
        const sheet = sheets.items[i];
        const range = sheet.getUsedRange();
        range.load("text");
        await context.sync();

        range.text.forEach(row => {
            row.forEach(cell => {
                if (cell !== "") {
                    nonEmptyValues.push(cell);
                }
            });
        });
    }
    //Scanning Excel datas in the server
    ScanValuesInTheServer(nonEmptyValues);
}).catch(error => {
    console.log(error);
});
1

There are 1 best solutions below

2
Eugene Astafiev On

The JavaScript API for Office (OfficeJS) doesn't provide anything for that out of the box. You can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team go through the planning process, see https://aka.ms/M365dev-suggestions .