advanced-formula-environment icon indicating copy to clipboard operation
advanced-formula-environment copied to clipboard

Import module from Name Manager

Open KDean-Dolphin opened this issue 1 year ago • 2 comments

I have a large library of functions (~100) that I developed for a specific problem domain and I would like to package them as a module to distribute via Gist. It would be nice to be able to create a module from existing names (functions and formulas), but there's doesn't appear to be a way to do that.

The feature should recognize module names, as I have already stored my functions as ModuleName.FunctionName, which would mean stripping off ModuleName when bringing them over from Name Manager.

For now, I don't have much of a problem, as I maintain the functions in a worksheet and use some rudimentary VBA to publish them, so it will be easy for me to export them in module format. It would be nice in the future, though.

KDean-Dolphin avatar Sep 07 '23 16:09 KDean-Dolphin

Adding as an enhancement request.

The logic can be written using Script Lab if needed.

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

const modulePrefix = "tester";

async function run() {
  return Excel.run(async (context) => {
    const names = context.workbook.names.load({ name: true, formula: true, comment: true });
    await context.sync();
    const definitions = names.items.filter(x => x.name.startsWith(modulePrefix + "."));
    const bindings = definitions.map((n) => {
      let content = ""
      if (n.comment !== undefined && n.comment !== "") {
        content += `/** ${n.comment} */`;
        content += "\n";
      }
      content += `${n.name.substring(modulePrefix.length + 1)} ${n.formula};`
      return content
    });
    console.log(bindings.join("\n\n"));
  });
}

/** 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);
  }
}

In the default "New Snippet" you can drop this code and the run button will create a module definition for the "tester" module.

jack-williams avatar Sep 08 '23 09:09 jack-williams

I second the request for this enhancement. I posted an issue that I'll remove; I didn't use the right words to search (and did a kid-search, tbh).

pdtcaskey avatar Aug 05 '24 01:08 pdtcaskey