Wiseguy
Wiseguy

Reputation: 20873

InvalidArgument error when calling Binding.getRange()

When I call the getRange() method on a Binding object, I receive the following error:

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

This seems nonsensical because the method does not have any parameters according to the API linked above.

Even a new project with the docs' example code for getRange() causes this error for me (after I've added a binding, of course):

Excel.run(function (ctx) { 
    var binding = ctx.workbook.bindings.getItemAt(0);
    var range = binding.getRange();
    range.load('cellCount');
    return ctx.sync().then(function() {
        console.log(range.cellCount);
    });
}).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
});

Is there some seemingly unrelated issue going on here?

I'm on Windows 10 running Office Pro Plus 2016, Version 1709 (Build 8528.2139), which supports ExcelApi 1.6.


Update

I'm also seeing this error when trying to add a binding with bindings.add() or .addFromNamedItem() like so:

ctx.workbook.bindings.addFromNamedItem("Name", Office.CoercionType.Text, "Name");

yielding:

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

My older code using the Shared API method addFromNamedItemAsync() successfully adds a binding without issue, however.

I'll continue experimenting in an attempt to determine if this is occurring with all Binding-related methods in the ExcelApi or with any non-Binding-related or Shared API methods.

Upvotes: 1

Views: 1119

Answers (1)

Sudhi Ramamurthy
Sudhi Ramamurthy

Reputation: 2478

You are adding the binding the wrong way. First, the address should contain the sheet name as the bindings collection is on the workbook. So, it doesn't know which range you are referring to. Second, you should create binding of type Range. Lastly, using getItemAt is limiting as position can change based on delete actions. getItem with the key is a better method to get the binding object.

...
ctx.workbook.bindings.add("Sheet1!D3", "Range", "Binding2");
await ctx.sync();
var binding = ctx.workbook.bindings.getItem("Binding2");
var range = binding.getRange();
range.load('cellCount');
return ctx.sync().then(function () {
     console.log(range.cellCount);
});
...

Upvotes: 1

Related Questions