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

Error reading Excel workbook containing named formulas #218

Closed
jackmansean64 opened this issue Jan 3, 2025 · 5 comments
Closed

Error reading Excel workbook containing named formulas #218

jackmansean64 opened this issue Jan 3, 2025 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@jackmansean64
Copy link

jackmansean64 commented Jan 3, 2025

OS (e.g. Windows 10 or macOS Sierra)

Windows 10

Versions of xlwings, Excel and Python (e.g. 0.11.8, Office 365, Python 3.7)

xlwings 0.33.3, Office 365, Python 3.12

Describe your issue (incl. Traceback!)

A JavaScript error is thrown when calling xlwings.runPython(window.location.origin + "/end-point"); for my workbook. The error reads:

{
    "code": "InvalidOperation",
    "message": "This operation is not permitted for the current object.",
    "errorLocation": "NamedItem.getRange",
    "statement": "var range = name.getRange();",
    "surroundingStatements": [
        "var workbook = context.workbook;",
        "var names = workbook.names;",
        "var name = names.getItem(...) /* originally getItem(...) */;",
        "// >>>>>",
        "var range = name.getRange();",
        "// <<<<<",
        "// Instantiate {range}",
        "var worksheet = range.worksheet;",
        "worksheet.load([\"position\"]);",
        "var range1 = name.getRange();",
        "// Instantiate {range1}",
        "..."
    ],
    "fullStatements": []
}

Debugging the error in excel-win32.js leads me to believe that the cause is named references in my workbook.
The workbook contains numerous named references pointing to formulas like the following: =OFFSET(BalancesByMonth!$B$4,0,0,COUNTA(BalancesByMonth!$B:$B)-1,COUNTA(BalancesByMonth!$5:$5)).

I can reproduce the error outside of xlwings by running the following in ScriptLab:

  await Excel.run(async (context) => {
    const namedRanges = context.workbook.names.load();
    await context.sync();
    console.log("This workbook contains " + namedRanges.items.length + " named items.");
    for (let i = 0; i < namedRanges.items.length; i++) {
      console.log(JSON.stringify(namedRanges.items[i])) + "\n";
      console.log(namedRanges.items[i].getRange())
    }
    await context.sync();
  });

Which results in the same error. I've managed to find a couple stackoverflow posts similar issue such as https://stackoverflow.com/questions/78257772/excel-office-add-in-office-js-throws-error-when-trying-to-read-a-nameditems-r and https://stackoverflow.com/questions/70791127/evaluation-by-named-ranges-by-javascript-api-does-not-work-for-certain-formulas which seems to indicate that the operation .getRange() is not supported for certain named formulas.

The problem appears to be that xlwings.runPython automatically calls getRange() on all named references, which doesn't work when the named references contain certain formulas.

Is there a workaround or does this prevent me from using xlwings server until the issue is resolved?

@jackmansean64 jackmansean64 changed the title xlwings server error reading Excel workbook containing names referencing formulas xlwings server error reading Excel workbook containing named formulas Jan 3, 2025
@jackmansean64 jackmansean64 changed the title xlwings server error reading Excel workbook containing named formulas Error reading Excel workbook containing named formulas Jan 3, 2025
@fzumstein fzumstein transferred this issue from xlwings/xlwings Jan 3, 2025
@fzumstein fzumstein added the bug Something isn't working label Jan 3, 2025
@jackmansean64
Copy link
Author

jackmansean64 commented Jan 4, 2025

Thanks for the quick response! Commenting out that loop worked for a short-term fix. Assuming the error originates there, wrapping it in a try-catch, skipping the problematic named items, and maybe warning the user somehow would probably be a better fix. I found debugging the office.js code a bit tough, there's very little information provided about the object being parsed.

@fzumstein
Copy link
Member

If you have an example of a named range that causes the failure, that would help to reproduce, thanks!

@jackmansean64
Copy link
Author

Here is the named range I mentioned previously:
image

@fzumstein fzumstein added this to the 0.10.1 milestone Jan 16, 2025
@fzumstein
Copy link
Member

fzumstein commented Jan 17, 2025

To replicate: Go to the Name Manager and add the following Name:

Name: name1
Scope: Sheet
Refers to: =100 + 200

fzumstein added a commit that referenced this issue Jan 17, 2025
…n line with workbook scoped named ranges)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants