1

I have problems executing an office addin in multiple Excel instances. One stops running when both are executed hat the same time.

I did 2 quick ScriptLab samples, where you can reproduce some issues (I pasted them). One contains an UDF-Function, just register it in ScriptLab. The other on is a sample which produces one of my problems.

First register the UDF, than before using the second part, create 2 workbooks with each having 100 worksheets that contain the following function (depending on the Snippent name, which is in my case "Blank snippet (1)", if your name is different, please adjust the formula name here and also in the addin-code in the function "findAllOrNullObject").

=SCRIPTLAB.BLANKSNIPPET1.ADD(1;2)

The quickest way to do this is: Create ten sheets with that function and copy this ten worksheets ten times to the end of the workbook. After this is done, save the workbook a second time with a different name. Afterwards, open both workbooks and click "Run" (in both sheets). Than click into another application while both are running or open one. On the console you will see a counter that indicates, on which sheet the addin is actually working. You should expect "INDEX: 100" in both instances but one instance will stop, when you click into another application or start one and will not reach 100. If you will not have the problem directly, just try again, it will sure appear.

Code for UDF:

/**
 * Adds two numbers.
 * @customfunction
 * @param first First number
 * @param second Second number
 * @returns The sum of the two numbers.
 */
/* global clearInterval, console, setInterval */

function add(first: number, second: number): number {
  return first + second;
}

Code for Addin:

$("#run").click(() => tryCatch(run));

async function run() {
  this.refreshWorkbook();
}

async function refreshWorkbook() {
  let sheets: Excel.WorksheetCollection;

  Excel.run(async (context) => {
    sheets = context.workbook.worksheets;
    sheets.load("items/name");
    await sheets.context.sync();
    if (sheets.items.length >= 1) {
      for (let sheetIndex = 0; sheetIndex < sheets.items.length; sheetIndex++) {
        console.log("INDEX : " + sheetIndex);
        const sheet = sheets.items[sheetIndex];
        await this.getInfo(sheet.name).then((information) => {
          // some stuff
        });
      }
    }
  });
}

async function getInfo(worksheetName: string): Promise<string> {
  return new Promise<string>((resolve, reject) => {
    Excel.run(async (context) => {
      const sheet: Excel.Worksheet = context.workbook.worksheets.getItem(worksheetName);
      sheet.load("name");
      await context.sync();
      const usedRange = sheet.getUsedRangeOrNullObject();
      if (usedRange) {
        const functionCells = sheet.findAllOrNullObject("=SCRIPTLAB.SCRIPTLAB.BLANKSNIPPET1.ADD(", {
          matchCase: false,
          completeMatch: false
        });
        functionCells.load("areaCount");
        await context.sync();
        if (functionCells) {
          resolve("A");
        } else {
          reject("X");
        }
      }
    });
  });
}

/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
    console.error(error);
  }
}

If I delete the search part it works.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32
Marco Siebert
  • 109
  • 10

1 Answers1

1

We can repro this issue, it can be repro without CF. so this is more API related issues. a bug 4124929 was created to track this issue. more likely an issue in findAllOrNullObject API, we are doing some investigation on this issue.

Raymond Lu
  • 2,178
  • 1
  • 6
  • 19
  • Thanks for your reply, I first thought it was a CF problem as I could not reproduce it with standard built in UDFs. Could you give me the link to follow the bug you mentioned? – Marco Siebert Apr 28 '20 at 10:42
  • I am sorry, the bug database cannot be accessed outside of Microsoft corpnet. I share the number here is easier tracking this issue with internal bug status. – Raymond Lu Apr 28 '20 at 11:20
  • Could you please share us your scenario, I would like to learn the use case of 2 workbook – Raymond Lu Apr 28 '20 at 11:52
  • 1
    I am not sure if customers will use 2 workbooks at the same time but I suspect this could happen. If a customer creates a workbook with 100 UDF functions to request reports and than updates this workbook, he maybe does not want to wait until the update is finished. As the update could take some time (data wil be requested from server, written to Excel and styled afterwards), he could use the "runtime" to do other things in another workbook. Or he has splitted the UDF functions to 2 workbooks. You never know, but I had customers in the past who worked in more than one workbook at the same time. – Marco Siebert Apr 28 '20 at 11:58
  • Thanks, it makes sense to open 2 workbooks, but I am not sure the scenario of 2 workbooks with same add-in, it looks to me an uncommon scenario? maybe you could share a bit more on this for us to evaluate the impact of this issue, could you introduce me a bit for your add-in? – Raymond Lu Apr 28 '20 at 12:03
  • Sure, I could introduce you some more, but it would be to much for a comment :-( – Marco Siebert Apr 28 '20 at 12:10
  • But maybe something about the addin: It has several functions for example to define functions with parameters for calls from the backend, update functions of those defined functions (sheet, workbook) and a function to transfer data to the backend (more functions will be added in the future). As a usecase the user may update reports defined in workbook 1 and transfer data to the backend in workbook 2 at the same time without waiting for the update process in workbook 1 to be finished. – Marco Siebert Apr 28 '20 at 12:24
  • Our target group are controllers of large companies, where it is the normal case that several Excel workbooks are open at the same time. – Marco Siebert Apr 28 '20 at 12:36
  • 1
    2 Workbooks: many desktop Application-level addins or UDF libraries are designed to be used with multiple workbooks open. This is a fairly frequently encountered use case. Does not happen with Web Excel because it does not support multiple workbooks. – Charles Williams Apr 28 '20 at 12:38
  • @CharlesWilliams Are you sure with Excel in the web? I can open multiple workbooks when I use Excel at office.com – Marco Siebert Apr 29 '20 at 09:14