title: Excel JavaScript Apis call class function with event handler from command
body: <p>I am trying to add and remove event handler for <code>onSelectionChanged</code>.</p>
  <p>If I defined the functions like below with <code>export async function</code></p>
  <pre><code>export async function EnableCellHighlight(): Promise&lt;void&gt; {
    console.log(1);
    let _this: any = this;
      await Excel.run(async function(context){
          let workbook = context.workbook;
          workbook.onSelectionChanged.add(CellHighlightHandler);
          await context.sync();
      });
  }
  export async function CellHighlightHandler(event: Excel.SelectionChangedEventArgs): Promise&lt;void&gt;{
    await Excel.run(async (context) =&gt; {
        let workbook = context.workbook;
        let sheets = workbook.worksheets;
        sheets.load(&quot;items&quot;);
        await context.sync();
        // clear previous meekou conditional format
        await clearMeekouFormat();
        // add new conditional format
        let selection = workbook.getSelectedRange();
        selection.load(&quot;rowIndex,columnIndex&quot;);
        await context.sync();
        let rowConditionalFormat = selection.getEntireRow().conditionalFormats.add(Excel.ConditionalFormatType.custom);
        rowConditionalFormat.custom.format.fill.color = &quot;red&quot;;
        rowConditionalFormat.custom.rule.formula = `=ROW()= + ${(selection.rowIndex + 1)} + N(&quot;${AppConsts.Meekou}&quot;)`;
        let columnConditionalFormat = selection.getEntireColumn().conditionalFormats.add(Excel.ConditionalFormatType.custom);
        columnConditionalFormat.custom.format.fill.color = &quot;red&quot;;
        columnConditionalFormat.custom.rule.formula = `=Column()= + ${(selection.columnIndex + 1)} + N(&quot;${AppConsts.Meekou}&quot;)`;
        await context.sync();
    });
  }
  
  export async function clearMeekouFormat(): Promise&lt;void&gt; {
    await Excel.run(async function (context) {
      let workbook = context.workbook;
      let worksheets = workbook.worksheets;
      worksheets.load(&quot;items/name&quot;);
      await context.sync();
      for (let i = 0; i &lt; worksheets.items.length; i++) {
        let worksheet = worksheets.items[i];
        let conditionalFormats = worksheet.getRange().conditionalFormats;     
        conditionalFormats.load(&quot;items/type&quot;);
        await context.sync();
        let total = conditionalFormats.items.length;
        for(let j = total-1; j &gt;=0; j --){
          let conditionalFormat = conditionalFormats.items[j];
          if(conditionalFormat.type == Excel.ConditionalFormatType.custom){
            conditionalFormat.load(&quot;custom/rule/formula&quot;);
            await context.sync();
            if (conditionalFormat.custom.rule.formula.includes(AppConsts.Meekou)) {
              conditionalFormat.delete();
            }
          }              
        }            
      }      
      await context.sync();
    });
  }
  </code></pre>
  <p>It will works correctly.</p>
  <p>But, if I move above code to a class like below:</p>
  <pre><code>export class ExcelService {
  private cellHighlightHandler = this.CellHighlightHandler;
  
  //#region excel events
      async enableCellHighlight(): Promise&lt;void&gt; {
        console.log(1);
        let _this: any = this;
          await Excel.run(async function(context){
              let workbook = context.workbook;
              workbook.onSelectionChanged.add(_this.cellHighlightHandler);
              await context.sync();
          });
      }
      async disableCellHightlight(): Promise&lt;void&gt; {
        let _this: any = this;
          await this.clearMeekouFormat();
          await Excel.run(async function(context){
              let workbook = context.workbook;
              workbook.onSelectionChanged.remove(_this.cellHighlightHandler);
              await context.sync();
          });        
      }
      async CellHighlightHandler(event: Excel.SelectionChangedEventArgs): Promise&lt;void&gt;{
        let _this: ExcelService = this;
          await Excel.run(async (context) =&gt; {
              let workbook = context.workbook;
              let sheets = workbook.worksheets;
              sheets.load(&quot;items&quot;);
              await context.sync().catch(e =&gt; console.log(e));
              // clear previous meekou conditional format
              await _this.clearMeekouFormat().catch(e =&gt; console.log(e));
              // add new conditional format
              let selection = workbook.getSelectedRange();
              selection.load(&quot;rowIndex,columnIndex&quot;);
              await context.sync();
              let rowConditionalFormat = selection.getEntireRow().conditionalFormats.add(Excel.ConditionalFormatType.custom);
              rowConditionalFormat.custom.format.fill.color = &quot;red&quot;;
              rowConditionalFormat.custom.rule.formula = `=ROW()= + ${(selection.rowIndex + 1)} + N(&quot;${AppConsts.Meekou}&quot;)`;
              let columnConditionalFormat = selection.getEntireColumn().conditionalFormats.add(Excel.ConditionalFormatType.custom);
              columnConditionalFormat.custom.format.fill.color = &quot;red&quot;;
              columnConditionalFormat.custom.rule.formula = `=Column()= + ${(selection.columnIndex + 1)} + N(&quot;${AppConsts.Meekou}&quot;)`;
              await context.sync();
          });
      }
      /**
       * clear all meekou conditional format
       */
      async clearMeekouFormat(): Promise&lt;void&gt; {
          await Excel.run(async function (context) {
            let workbook = context.workbook;
            let worksheets = workbook.worksheets;
            worksheets.load(&quot;items/name&quot;);
            await context.sync();
            for (let i = 0; i &lt; worksheets.items.length; i++) {
              let worksheet = worksheets.items[i];
              let conditionalFormats = worksheet.getRange().conditionalFormats;     
              conditionalFormats.load(&quot;items/type&quot;);
              await context.sync();
              let total = conditionalFormats.items.length;
              for(let j = total-1; j &gt;=0; j --){
                let conditionalFormat = conditionalFormats.items[j];
                if(conditionalFormat.type == Excel.ConditionalFormatType.custom){
                  conditionalFormat.load(&quot;custom/rule/formula&quot;);
                  await context.sync();
                  if (conditionalFormat.custom.rule.formula.includes(AppConsts.Meekou)) {
                    conditionalFormat.delete();
                  }
                }              
              }            
            }      
            await context.sync();
          });
        }
  
  //#endregion
  }
  </code></pre>
  <p>The code will run stop <code> await _this.clearMeekouFormat().catch(e =&gt; console.log(e));</code>, but it did not throw any error</p>