Inserting multiple SVG images into a spreadsheet asynchronously

Mitch Miller 0 Reputation points
2024-01-17T16:15:06.83+00:00

This is an add-in for MS Excel built using the JavaScript API.

General task:

  1. Start with a worksheet that has a column of identifiers.
  2. User wants to add data. Uses checkboxes on form to select data fields. Clicks 'add data' button.
  3. Add-in reads identifiers and creates a query for each.
  4. Each query returns according to how long it takes and then a callback within the add-in transfers data to the sheet.
  5. Works fine for text data.
  6. In additional to text, images are generated for each row using code similar to
   dataCell.select();
       await context.sync();
   Office.context.document.setSelectedDataAsync(imageData, imageOptions, function (asyncResult) {
   
  1. This set of calls works fine when one row is processed at a time.
  2. When multiple rows are returned, the images are scrambled -- some rows have several images stacked on top of one another; other rows have no image.
  3. What I assume is happening: one row's results come back and the dataCell.select() happens. Before the setSelectedDataAsync call can happen, another row of results comes back and the selection changes.
  4. How can I make the above work? Is there a way of locking a JavaScript function so only one 'thread' can execute at a time?
Microsoft 365 and Office Development Office JavaScript API
Microsoft 365 and Office Development Other
Microsoft 365 and Office Excel For business Windows
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.