weixin_33690963 2018-05-16 10:34 采纳率: 0%
浏览 34

网页查询Excel JS API

I am trying to find the Excel Javascript API alternative for the VBA function to get data from an URL to an Excel sheet. In VBA I use Querytable with the add method to get data from an URL using this code snippet:

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

This is the core of the VBA snippet:

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"))

This works fine in VBA

But it does not work with an XMLhttp request in Office JS API and there is no information at all on the microsoft website nor anywhere else. I use the following function found in this post here on stack overflow

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);   
        }
    }

there is never a readystate or xmlhttp status but if I go to Excel->data->from web and use the same URL the data is correctly imported. So clearly I am not doing it right but I have no idea where to search

Update: here the snippet for calling the XmlHTTP request:

 // 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();
            });

As matter in fact, after digging deeper I indeed saw that this is a CORS error:

HTML1300: Navigation occurred. FunctionFile.html SEC7120: Origin https://localhost:44301 not found in Access-Control-Allow-Origin header. FunctionFile.html SCRIPT7002: XMLHttpRequest: Network Error 0x80700013, Could not complete the operation due to error 80700013. FunctionFile.html

But how to solve this without creating and hosting a web service to consume the data from the other domain?

Many thanks for your help

regards

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line