0

I am having an issue calling a function into the main script whilst maintaining the excel context

async function main(context: Excel.RequestContext){ findMD(1) }

The function findMD(test) contains various ranges which draw from context.workbook and due to the function being defined outside of main I'm getting a cannot find name 'context' error. To try and solve this I changed function findMD(test){ to async function findMD(context: Excel.RequestContext,test){. However whilst it has solved the context errors I can still not run the script because the function call findMD(1) is now getting an error message of Expected 2 arguments but got 1

Would seriously appreciate any assistance one could offer as this is killing me! Thanks!

Jai Hirani
  • 99
  • 9

1 Answers1

0

When you define the findMD() function to take context as an input parameter, you also need to supply that parameter whenever you call it. Here is an example of a helper function which takes as input the context object, as well as a second parameter, much like what you are attempting to do.

async function helper(context: Excel.RequestContext, test) {
  let workbook = context.workbook; 
  let worksheets = workbook.worksheets;
  let selectedSheet = worksheets.getActiveWorksheet();

  selectedSheet.load("name");
  await context.sync();

  console.log( selectedSheet.name);
  console.log( test);
}

async function main(context: Excel.RequestContext) {
  helper(context,"test parameter");
}

Inside of main(), when I call helper(), I pass in both the context object, as well as the value for the test parameter which I want the function to see.

Jim Masson
  • 96
  • 2