Skip to content

Autorun script when opening workbook #293

Description

@fzumstein

This works but might be simplified:

  1. While you have your workbook open, right-click the xlwings task pane > Inspect... In the dev tools, in the JS Console, type the following and hit Enter:
await Office.addin.setStartupBehavior(Office.StartupBehavior.load);

This will cause the Excel workbook to load the add-in automatically.

  1. In app/static/js/main.js, paste the following code (you will need to adjust it to your needs):
Office.onReady(async () => {
  if (await xlwings.getActiveBookName() === "your-workbook-name.xlsx") {
    let authResult = await globalThis.getAuth();
    await xlwings.runPython({
      include: "",
      exclude: "",
      scriptName: "hello_world",
      auth: authResult.token, // older versions of xlwings Server would require authResult instead of authResult.token
      errorDisplayMode: "taskpane",
    });
  }
});

This code is triggered after the add-in has been loaded by the workbook.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions