Skip to content

Commit

Permalink
fix varargs with object handles
Browse files Browse the repository at this point in the history
  • Loading branch information
fzumstein committed Oct 27, 2024
1 parent 4f574df commit 3103aa1
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions app/static/js/core/custom-functions-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ Office.onReady(function (info) {
contentLanguage = Office.context.contentLanguage;
});

function flattenArray(arr) {
const result = [];
const indices = [];

function recursiveFlatten(item, path) {
if (Array.isArray(item)) {
item.forEach((subItem, index) => {
recursiveFlatten(subItem, [...path, index]);
});
} else {
result.push(item);
indices.push(path);
}
}

recursiveFlatten(arr, []);
return { result, indices };
}

// Workbook name
let cachedWorkbookName = null;

Expand Down Expand Up @@ -106,11 +125,24 @@ async function base() {
const workbookName = await getWorkbookName();
const officeApiClient = localStorage.getItem("Office API client");

// For arguments that are Entities, replace the arg with their address (cache key)
args.forEach((arg, index) => {
if (arg && arg[0] && arg[0][0] && arg[0][0].type === "Entity") {
// For arguments that are Entities, replace the arg with their address (cache key).
// The issues is that invocation.parameterAddresses returns a flat list while args
// contains a nested array for varargs (in Office.js called 'repeating').
const { result: flatArgs, indices } = flattenArray(args);

flatArgs.forEach((item, index) => {
if (item?.type === "Entity") {
const address = `${officeApiClient}[${workbookName}]${invocation.parameterAddresses[index]}`;
args[index] = address;

let target = args;
const path = indices[index];

for (let i = 0; i < path.length - 1; i++) {
target = target[path[i]];
}

const lastIndex = path[path.length - 1];
target[lastIndex] = address;
}
});

Expand Down

0 comments on commit 3103aa1

Please sign in to comment.