Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import module from Name Manager #56

Open
KDean-Dolphin opened this issue Sep 7, 2023 · 1 comment
Open

Import module from Name Manager #56

KDean-Dolphin opened this issue Sep 7, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@KDean-Dolphin
Copy link

KDean-Dolphin commented Sep 7, 2023

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.

@jack-williams jack-williams added the enhancement New feature or request label Sep 8, 2023
@jack-williams
Copy link
Contributor

jack-williams commented Sep 8, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants