Javascript Web查询Excel JS api

Javascript Web查询Excel JS api,javascript,ajax,excel,office-js,Javascript,Ajax,Excel,Office Js,我正在尝试为VBA函数寻找Excel Javascript API替代方案,以从URL获取Excel工作表的数据。在VBA中,我使用Querytable和add方法,使用以下代码段从URL获取数据: Dim URLprefix As String Dim ProjectID As String Dim SnapshotID As String Dim ResultType As String Dim Hash As String Dim lngNumberOfCols As Long URL

我正在尝试为VBA函数寻找Excel Javascript API替代方案,以从URL获取Excel工作表的数据。在VBA中,我使用Querytable和add方法,使用以下代码段从URL获取数据:

Dim URLprefix As String
Dim ProjectID As String
Dim SnapshotID As String
Dim ResultType As String
Dim Hash As String
Dim lngNumberOfCols As Long


URLprefix = "https://mywebsite.com"

With Sheets("Definition")
    ProjectID = .Range("B3")
    SnapshotID = .Range("B4")
    Hash = .Range("B5")
End With


Set ws = Worksheets("Import")
ws.Select

ws.Cells.Select

Cells.Delete


    'Get the Produc definitions from the EvalDB query
    ResultType = "product"
    Set QT = ws.QueryTables.Add("URL;" & URLprefix & ProjectID & "&id_snapshot=" & SnapshotID & "&type=" & ResultType & "&hash=" & Hash, Destination:=Range("$A$3"))
    QT.Refresh BackgroundQuery:=False
    'Get the property definitions
    ResultType = "property"
    Set QT = ws.QueryTables.Add("URL;" & URLprefix & ProjectID & "&id_snapshot=" & SnapshotID & "&type=" & ResultType & "&hash=" & Hash, Destination:=Range("$D$1"))
    'get the results
    ResultType = "result"
    QT.Refresh BackgroundQuery:=False
    **Set QT = ws.QueryTables.Add("URL;" & URLprefix & ProjectID & "&id_snapshot=" & SnapshotID & "&type=" & ResultType & "&hash=" & Hash, Destination:=Range("$D$3"))**
    QT.Refresh BackgroundQuery:=False
这是VBA代码段的核心:

Set QT = ws.QueryTables.Add("URL;" & URLprefix & ProjectID & "&id_snapshot=" & SnapshotID & "&type=" & ResultType & "&hash=" & Hash, Destination:=Range("$A$3"))
    QT.Refresh BackgroundQuery:=False
    'Get the property definitions
    ResultType = "property"
    Set QT = ws.QueryTables.Add("URL;" & URLprefix & ProjectID & "&id_snapshot=" & SnapshotID & "&type=" & ResultType & "&hash=" & Hash, Destination:=Range("$D$1"))
这在VBA中运行良好

但它不能在OfficeJSAPI中处理XMLhttp请求,而且在微软网站或其他任何地方都没有任何信息。我使用下面在这里找到的关于堆栈溢出的函数

var HttpClient = function () {
        this.get = function (aUrl, aCallback) {
            var anHttpRequest = new XMLHttpRequest();
            anHttpRequest.onreadystatechange = function () {
                if (anHttpRequest.readyState === 4 && anHttpRequest.status === 200)
                    aCallback(anHttpRequest.responseText);
        }

            anHttpRequest.open("GET", aUrl, true);
            anHttpRequest.send(null);   
        }
    }
从来没有readystate或xmlhttp状态,但如果我从web转到Excel->data->并使用相同的URL,数据将正确导入。很明显,我做得不对,但我不知道在哪里搜索

更新:下面是调用XmlHTTP请求的代码段:

 // Run a batch operation against the Excel object model
            Excel.run(function (ImportData) {
                // Create a proxy object for the active sheet
                var sheet = ImportData.workbook.worksheets.getActiveWorksheet();
                // Queue a command to write the sample data to the worksheet
                var client = new HttpClient();

                sheet.getRange("A1").values = client.get(QueryString, function (response) {
              });


                // Run the queued-up commands, and return a promise to indicate task completion
                return ImportData.sync();
            });
事实上,在深入挖掘之后,我确实发现这是一个CORS错误:

HTML1300:发生导航。 FunctionFile.html SEC7120:在访问控制允许原点标头中找不到原点。 FunctionFile.html SCRIPT7002:XMLHttpRequest:网络错误0x80700013,由于错误80700013,无法完成操作。 FunctionFile.html

但是,如何在不创建和托管web服务来使用来自其他域的数据的情况下解决这个问题呢

非常感谢你的帮助


问候

欢迎来到Stack Overflow!既然你是新来的,我建议你阅读一些技巧。这里有很多VBA代码,但对Office.js中的操作知之甚少。你能提供你试图使用的代码吗?这里的
httpGetAsync
方法没有解释它是如何被调用的,或者您希望得到什么。只是一种预感:这可能是CORS问题吗?您好。的确,这是一个CORS问题。但是有人@Microsoft想到了如何应对这个问题吗?以更一般的方式。如果我想访问服务器上的XML文件,我可以使用VBA完美地实现这一点,但当我使用Office API执行同样的操作时,它就不起作用了-(.直到现在,我还没有找到任何其他解决方案,然后要求主机应用程序的管理员禁用CORS检查服务器端,这会带来巨大的安全风险…嗨,大家好,没有解决方案如何处理此类错误?如何从无法控制的服务器获取数据?