title: Remove event handler excel javascript api
body: <p>I've been trying to execute the event handler function just when the format function is called and then remove the event handler function. In this way the period of listening to calculated functions is limited.</p>
  <p>This sample <a href="https://docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-events#remove-an-event-handler" rel="nofollow noreferrer">Remove an event handler</a> declares the function but I'm a little bit confused with the place to call the &quot;remove()&quot;.</p>
  <pre><code> async function format() {
      var eventResult;
      await Excel.run(async (context) =&gt; {   
        const sheet = context.workbook.worksheets.getActiveWorksheet();    
        eventResult = sheet.onCalculated.add(function(event){
          return Excel.run(function (context){
            //Do some things 
            return context.sync().then(function () {
              //Do some things 
            })
            .then(()=&gt;{
              // remove();  -&gt; error: eventResult is not defined
            })
          });
        });
        // remove();   -&gt; error: eventResult is not defined
        return context.sync();
      });
    }
  </code></pre>
  <p>The remove function:</p>
  <pre><code>async function remove() {
  await Excel.run(eventResult.context, function (context) {
      eventResult.remove();
      return context.sync()
        .then(function() {
            eventResult = null;
            console.log(&quot;Event handler successfully removed.&quot;);
        });
  })
  }
  </code></pre>