Using arrays as parameters for custom functions leading to add-in loading issues

In an attempt to create custom functions for Excel that accept arrays (e.g., a range of cells) as input, and then return either a value or an array, I am facing some challenges.

Here's an example from my function.ts file:

/* global clearInterval, console, CustomFunctions, setInterval */

// OTHER FUNCTIONS

/**
 * Percent bias
 * @customfunction
 * @param Observed: Observed Data
 * @param Simulated: Simulated Data
 * @returns Percent bias
 */
function PBias(Observed: number[], Simulated: number[]): number {
  let sum_obs = Observed.reduce((partialSum, a) => partialSum + a, 0);
  let sum_sim = Simulated.reduce((partialSum, a) => partialSum + a, 0);

  return (100 * (sum_sim - sum_obs)) / sum_obs;
}
}

The above function gets built without any issues. However, when I start debugging, I encounter an error stating that my custom functions were not loaded.

If I remove the PBias function, other functions load correctly and work as expected.

These functions seem to be working appropriately as demonstrated in this TypeScript playground demo.

I have experimented with a fresh installation of Node.js (version 18.18.0), and Yeoman Generator for Office Add-ins (version 4.3.1, utilizing Excel Custom Functions with a Shared Runtime).

Answer №1

If you're looking to handle a range input, the most suitable option would be to utilize a 2D array known as number[][]. A range with more than one cell is essentially treated as a 2D array, although even a single cell can be viewed in this manner (such as "[[A1]]").

This issue may not be a recent development in Yeoman. For further information on 1D, 2D, and 3D arrays used as parameters in custom functions, refer to the following public document: https://learn.microsoft.com/en-us/office/dev/add-ins/excel/custom-functions-parameter-options?tabs=javascript#repeating-single-value-parameter. To summarize: 1D arrays for repeating single values, 2D arrays for ranges, and 3D arrays for repeating sets of ranges.

Answer №2

After some investigation, I managed to solve this issue or at least come up with a workaround. It appears that ranges are now being treated as 2D arrays. Therefore, the inputs should be declared as any[][]. This seems to be a recent change (or possibly a bug) in the latest version of Yeoman, as previous tutorials showed the use of number[] or any[] for input. Nevertheless, this modified version of my function loads correctly and functions as expected.

In this solution, I flatten the arrays before performing any calculations.

/**
 * Calculate percentage bias
 * @customfunction
 * @param Observed - Observed Data
 * @param Simulated - Simulated Data
 * @returns Percentage bias
 */
export function PBias(Observed: number[][], Simulated: number[][]): number {
  let sum_obs = 0;
  let sum_sim = 0;

  sum_obs = Observed.reduce(function (a, b) {
    return a.concat(b);
  }) // flatten array
    .reduce(function (a, b) {
      return a + b;
    }); // calculate sum

  sum_sim = Simulated.reduce(function (a, b) {
    return a.concat(b);
  }) // flatten array
    .reduce(function (a, b) {
      return a + b;
    }); // calculate sum

  return (100 * (sum_sim - sum_obs)) / sum_obs;
}

I am still curious to explore if there is a more optimal approach or a clearer explanation for this situation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling to display a PHP success message using AJAX

So I have this code where I am trying to create a form in PHP and send a message. The message is being submitted successfully, but I am facing an issue when it comes to displaying a success message. The page submits the data but does not show any output. H ...

Having issues with video playback on AngularJS

I've been working on creating a video player with angular js but I'm encountering issues with playing the video. Here's what I have attempted: videoPlayer.factory('controloptions',function() { var controloptions={}; co ...

Ways to verify if an array contains two identical object values?

I am in search of a way to determine whether my array contains duplicate object values or not. Let's say I have the following array of objects: const array = [ { id: "id1", quantity: 3, variation: "red", ...

Looking to create a format for displaying short comic strips in a grid pattern

I am struggling to create an image grid for my small comics. I want to have 2 columns and 3 rows, but all the divs end up in the same place. I tried using display: flex, but it didn't work as expected. Additionally, I want the grid to be responsive, b ...

What is the best way to empty an array within the state of a React component using JSX?

I need assistance with a React and JSX project where I am creating input fields that can be removed by clicking a button. Currently, I have an empty array stored in the state of the Page component. This array is updated using the addItems function. Howev ...

What is the best way to search for and isolate an array object within an array of objects using Javascript?

I am attempting to narrow down the list based on offerings const questions = [ { "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]}, { "id": 1505, "offerings": [ ...

What is the process of creating and customizing popovers in Bootstrap 5 with jquery?

Is there a way to dynamically create and add content to Bootstrap 5 popovers using JavaScript or jQuery? In Bootstrap 3, I used the following method: $('#element').popover({ placement : 'left', trigger : 'focus', html: true } ...

Ways to calculate the memory utilization of a JavaScript object

Suppose I want to compare the efficiency of storing bits of a static canvas/image with Alpha more or less than 0.5 using an "array of array of number" versus an "array of string," which would be better in terms of memory usage and speed? var c = $('m ...

Issue with useEffect causing a delay in updating the state value

I'm facing an issue with a component that displays the number of people who have liked a book. The problem is, I can't seem to consistently get the correct result in my states. Here's the code snippet: ///Fetching the book details cons ...

ajax-jquery request declined

I have a jquery-ajax function that is being called multiple times with different IP addresses each time. This function makes a call to an action in the mvc4 controller responsible for executing a ping and returning the results. After analyzing the request ...

Displaying a single image on various surfaces of a BoxGeometry in Three.js

I am currently utilizing three.js to generate 3D Objects. My goal is to develop a 3D canvas similar to the one showcased here in my project. I aim to display a single image on all sides of the object (box) except for the back. I came across a relevant exa ...

What is the correct way to iterate through a list of images fetched with getStaticProps and display them within the same component?

What is the proper way to map a list of images returned using getStaticProps? I had successfully implemented this by passing a prop to the gallery component in another page. However, I now want to consolidate all the getStaticProps code within the gallery ...

Step-by-step guide to sending RESTful requests using Angular's $http service

Currently, I am working with Nodejs and my route has this structure: router.get("/profil/:id",function (req,res) { }); I am looking to push data from my angular controller using the $http service. ...

tag containing inner text of span tag tagged with anchor tag

Using JavaScript, I have dynamically assigned an anchor tag within a span tag. However, the href attribute of the anchor tag is being formed incorrectly. Here is the JavaScript code: var HF1Id , HF2Id , SpanId , HF1Id = '<%=Request("HF1Id") %> ...

Leveraging NodeJS to handle server-side tasks and operations

Background: I am exploring the use of NodeJS for a project that involves scraping and storing content in Mongo. This process needs to be automated according to a set schedule. In addition, I need functions that can extract items from the Mongo database, o ...

What is the designated destination for JWT Tokens?

For my user login/signup process, I'm utilizing JWT and have a query regarding how the token is transmitted. At present, I am saving the token as a property in a JSON object on the server side, then passing it to the front-end. Upon receiving the obj ...

Obtaining HTML elements from JSON using jQuery (i.e., the opposite of serializing with Ajax)

I am looking to extract values from a set of controls (INPUT, SELECT, TEXTAREA) within a DIV and send them as JSON via Ajax to a server. Utilizing jQuery's serializeArray makes this process easy. After sending the JSON data, I expect the server to re ...

Creating an HTML list based on a hierarchical MySQL table structure

I have retrieved a hierarchical table showing different elements and their parent-child relationships as follows: id| name | parent_id | header 1 | Assets | 0 | Y 2 | Fixed Assets | 1 | Y 3 | Asset One | 2 | N 4 | ...

What is the best way to dynamically center text within a circle shape?

My dilemma lies in the design of a circular div that contains random text. While I am able to manually align the text inside the circle for static content, I seek a dynamic solution. Is there an automated way to adjust the size of the circle based on the t ...

Combining JSON and JavaScript for embedding a SPARQL query into an HTML document

I have successfully implemented an HTML + SPARQL + JSON + JavaScript program, which can be viewed here: Below is the code snippet for the SPARQL + JSON + JavaScript: function retrieveData() { var query = "PREFIX : <http://dbpedia.org/resource/> P ...