none
How to write Table in Bindings.AddFromPromptAsync method RRS feed

  • Question

  • HI , 

     Here is my code .. 

    function addFormPromptAsync (){
    
           
       var myTable = new Office.TableData();
       myTable.headers = new Array(1);
       myTable.headers[0] = ["NAME","AGE"];
       myTable.rows= new Array(1);
       myTable.rows[0]= ["RAM","24"];
        Office.context.document.bindings.addFromPromptAsync(Office.BindingType.Table, { id: 'MyBinding', promptText: 'Select text to bind to.' }, function (asyncResult) {
            //write('Added new binding with type: ' + asyncResult.value.type + ' and id: ' + asyncResult.value.id);
            
             asyncResult.value.setDataAsync(myTable,{coercionType: "table"},function(ste){});
        });
    }
    I am getting the error "Data source Reference is invalid".. I want to understand that is it possible to prompt user to bind for table ??? 


    Regards,

    Ram

    Tuesday, October 7, 2014 12:29 PM

Answers

  • Hi Ram,

    According to your code, I think you want to create a new table and add a data binding to this table.

    If so, before binding, you need to create the table first.

    The code as following is for your reference:

    /// <reference path="/Office/1.1/office.js" />
    
    var TableFactory = (function () {
        var configuration = {
            headers: [["Order ID", "Product", "Quantity", "Created"]],
            data: [
                [1, "Bike", 1, "5/12/2014"], 
                [2, "Car", 2, "5/10/2014"],
                [3, "Aircraft", 1, "5/11/2014"],
                [4, "Boat", 1, "4/1/2014"]
            ]
        }
    
        function createNewTable() {
            // Build table.
            var myTable = new Office.TableData();
    
            myTable.headers = configuration.headers;
    
            myTable.rows = configuration.data;
    
            var bindingName = "formatTableBinding";
    
            Office.context.document.setSelectedDataAsync(myTable, { coercionType: Office.CoercionType.Table },
                function (result) {
                    var error = result.error
                    if (result.status === "failed") {
                        app.showNotification(error.name + ":", error.message);
                    } else {
                        Office.context.document.bindings.addFromSelectionAsync(
                            Office.BindingType.Table,
                            { id: bindingName },
                            function (result) {
                                var error = result.error
                                if (result.status === "failed") {
                                    app.showNotification(error.name + ":", error.message);
                                } else {
                                    // add binding handler here
                                }
                            }
                        );
                    }
                });
        }
    
        return {
            create: createNewTable
        };
    })();
    
    TableFactory.create();

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 10, 2014 2:28 AM
    Moderator

All replies

  • Hi,

    After testing your code, I find that you use single quotes to define the id of binding object and use "asyncResult.value" for the setDataAsync method. You can edit it as followed.

    function addFormPromptAsync() {
    
    
        var myTable = new Office.TableData();
        myTable.headers = new Array(1);
        myTable.headers[0] = ["NAME", "AGE"];
        myTable.rows = new Array(1);
        myTable.rows[0] = ["RAM", "24"];
        Office.context.document.bindings.addFromPromptAsync(Office.BindingType.Table, { id: "MyBinding", promptText: "Select text to bind to."}, function (asyncResult) {
            //write('Added new binding with type: ' + asyncResult.value.type + ' and id: ' + asyncResult.value.id);
    
            Office.select("bindings#MyBinding").setDataAsync(myTable, { coercionType: "table" }, function (ste) { });
        });
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 8, 2014 9:13 AM
    Moderator
  • Getting the same issue after replacing the above code.. It says in a prompt window "Data Source Reference is not valid"..

    Am i wrong in using the excel 2013 ( Microsoft Profession Plus Version) which is neither of type SP1 nor Excel RT...

    How can i check this function in excel online .. ? is there a provision to run office js on excel online using localhost Apps... ?? 
    Wednesday, October 8, 2014 11:50 AM
  • Hi Ram,

    According to your code, I think you want to create a new table and add a data binding to this table.

    If so, before binding, you need to create the table first.

    The code as following is for your reference:

    /// <reference path="/Office/1.1/office.js" />
    
    var TableFactory = (function () {
        var configuration = {
            headers: [["Order ID", "Product", "Quantity", "Created"]],
            data: [
                [1, "Bike", 1, "5/12/2014"], 
                [2, "Car", 2, "5/10/2014"],
                [3, "Aircraft", 1, "5/11/2014"],
                [4, "Boat", 1, "4/1/2014"]
            ]
        }
    
        function createNewTable() {
            // Build table.
            var myTable = new Office.TableData();
    
            myTable.headers = configuration.headers;
    
            myTable.rows = configuration.data;
    
            var bindingName = "formatTableBinding";
    
            Office.context.document.setSelectedDataAsync(myTable, { coercionType: Office.CoercionType.Table },
                function (result) {
                    var error = result.error
                    if (result.status === "failed") {
                        app.showNotification(error.name + ":", error.message);
                    } else {
                        Office.context.document.bindings.addFromSelectionAsync(
                            Office.BindingType.Table,
                            { id: bindingName },
                            function (result) {
                                var error = result.error
                                if (result.status === "failed") {
                                    app.showNotification(error.name + ":", error.message);
                                } else {
                                    // add binding handler here
                                }
                            }
                        );
                    }
                });
        }
    
        return {
            create: createNewTable
        };
    })();
    
    TableFactory.create();

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 10, 2014 2:28 AM
    Moderator
  • Hi Ram,

    Since we haven't heard from you for a long time, I temporarily close this case. I mark useful reply as answer. If you have any concerns, please free feel to reopen it or submit a new question.

    Thanks for your understanding.

    Best regards

    Fei

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 11, 2014 7:31 AM
    Moderator