Can't get range from a defined name

元气小坏坏 提交于 2019-12-08 03:11:41

问题


Excel 2016 (Office 365) 32 bits, 16.0.6965.2115, Visual Studio 14.0.25425.01 Update 3 I'm quite sure the statement below used to work, but now it doesn't work anymore:

var range = ctx.workbook.names.getItem("Countries").getRange();

I get an error stating that there is no support for getRange method, but it should be supported as documented here.
What am I'm doing wrong?

--- EDIT: this is the code I'm using ---

function paintRange() {
    Excel.run(function (ctx) {
        var range = ctx.workbook.names.getItem("Countries").getRange();
        range.format.fill = "green";

        return ctx.sync();
    }).catch(function (error) {
        app.showNotification("Error", error);
    })
}

paintRange is attached to a button. There is a global scope defined name called Countries. I don't have any more details of the error besides the one I mentioned, I also tried opening the quick watch window to get more clues.


回答1:


UPDATE: The issue is fixed with an update to the CDN. You should be able to use namedItem.getRange() now. Thanks for reporting the issue, and allowing us to do a quick turn-around on it.

================

Felipe, looks like you're absolutely right. This is definitely a bug. Let me talk to the right folks to get this regression fixed as soon as we can. I'll see if we can put in some processes to avoid this in the future, as well.

From an immediate-workaround perspective, two options:

  1. Use the BETA CDN (esp if it's for an in-development add-in, rather than a production one). That URL is: https://appsforoffice.microsoft.com/lib/beta/hosted/office.js

  2. Do a temporarily filling in of the inadvertently-removed getRange functionality. Inside of Office.initialize, include the following code:

    if (!Excel.NamedItem.prototype.getRange) {
        Excel.NamedItem.prototype.getRange=function () {
            return new Excel.Range(this.context,
                OfficeExtension.ObjectPathFactory.createMethodObjectPath(
                    this.context, this, "GetRange",
                    OfficeExtension.OperationType.Read, [], false, true, null
                )
            );
        };
    }
    

The workaround in #2 should not cause harm even after the functionality is restored, but I would none-the-less recommend making a mental note to remove this after we've fixed the issue. I'll update this thread once we have fixed the underlying bug, hopefully within a weeks' time (as a very rough estimate, pending any complications that might delay it).

Thanks for bringing it to our attention -- both the individual bug, and the underlying process that let the regression to this one API go unnoticed.



来源:https://stackoverflow.com/questions/41434857/cant-get-range-from-a-defined-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!