OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
671 stars 95 forks source link

[Excel Online] workbook.names.add doesn't work with escaped strings #3770

Open MitchellNZ opened 11 months ago

MitchellNZ commented 11 months ago

Description

When trying to store escaped strings in a workbook level range name in Excel Online, it throws an error. The exact same code works fine in Excel for desktop on Windows.

The following code gives an error when running in Excel Online:

await Excel.run(async (context) => {
    context.workbook.names.add("Test_Range", "Hello \"Excel\" world!");
    await context.sync();
});

It still fails when trying variations of the ...add line such as:

context.workbook.names.add('Test_Range', 'Hello "Excel" world!');

Giving the following error:

...
code: "InvalidArgument"
message: "The argument is invalid or missing or has an incorrect format."
...

Your Environment

Expected behavior

Expect the code to work, as it does on Excel for desktop on Windows.

Current behavior

An error is thrown.

Steps to reproduce

  1. Open the ScriptLab addin.
  2. Copy/paste the failing code into the Run method in Excel for Web.
    await Excel.run(async (context) => {
    context.workbook.names.add("Test_Range", "Hello \"Excel\" world!");
    await context.sync();
    });
  3. Run and see error in console.

Link to live example(s)

N/A

Provide additional details

N/A

Context

Trying to store metadata in a workbook level range name, in the form of an escaped JSON object.

Useful logs

Console error:

[ERROR]: Error:
{
    "stack": "RichApi.Error: The argument is invalid or missing or has an incorrect format.\n    at new n (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:25:317442)\n    at i.processRequestExecutorResponseMessage (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:25:381888)\n    at https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:25:379951",
    "message": "The argument is invalid or missing or has an incorrect format.",
    "name": "RichApi.Error",
    "code": "InvalidArgument",
    "traceMessages": [],
    "innerError": null,
    "debugInfo": {
        "code": "InvalidArgument",
        "message": "The argument is invalid or missing or has an incorrect format.",
        "errorLocation": "NamedItemCollection.add",
        "statement": "var add = names.add(...);",
        "surroundingStatements": [
            "var workbook = context.workbook;",
            "var names = workbook.names;",
            "// >>>>>",
            "var add = names.add(...);",
            "// <<<<<"
        ],
        "fullStatements": [
            "var workbook = context.workbook;",
            "var names = workbook.names;",
            "var add = names.add(\"Test_Range\", \"Hello \\\"Excel\\\" world!\", undefined);"
        ]
    },
    "httpStatusCode": 400
}
microsoft-github-policy-service[bot] commented 11 months ago

Thank you for letting us know about this issue. We will take a look shortly. Thanks.