Forum Discussion

Jorik's avatar
Jorik
Copper Contributor
Jan 17, 2025

Excel Javascript API: Question about Bindings

Hello,

I've got a question about using Bindings in the Excel/Office Javascript API, especially regarding listening to binding updates.

There are ways to listen to updates to specific bindings using https://learn.microsoft.com/en-us/javascript/api/excel/excel.binding?view=excel-js-preview#excel-excel-binding-ondatachanged-member

But what I'm looking for is listening for updates on newly created bindings. This is especially relevant in the Excel Online / OneDrive sync. If one instance of Excel creates a binding, I want all other instances to receive an update about it.

Something like this:

    await Excel.run(async context => {
       context.workbook.bindings.onAdded.add((...) => {...});
    });

1 Reply

  • May consider a workaround as a combination of periodic checks and event listeners. 

     

    Periodically Check for New Bindings: Use a timer to periodically check for new bindings in the workbook.

    Add Event Listeners: Once a new binding is detected, add the necessary event listeners to it.

     

    Code look like

    async function checkForNewBindings() {
        await Excel.run(async (context) => {
            const bindings = context.workbook.bindings;
            bindings.load("items");
            await context.sync();
            
            bindings.items.forEach(binding => {
                if (!binding.onDataChanged) {
                    binding.onDataChanged.add((...) => {
                        // Handle the data changed event
                    });
                }
            });
        });
    }
    
    // Run the check periodically (e.g., every 5 seconds)
    setInterval(checkForNewBindings, 5000);
    

     

    This script will periodically check for new bindings and add the onDataChanged event listener to each new binding it finds.

Resources