none
Table Bindings and Filters RRS feed

  • Question

  • What is the specified behavior for reading/writing table bindings that currently have filters applied to them by the user?

    Ian

    Thursday, April 16, 2015 2:14 PM

Answers

  • Hi,

    Reading:

    Using Binding.getDataAsync method (JavaScript API for Office), the callback return value depends on the filterType parameter (FilterType enumeration).

    Enumeration

    Value

    Description

    Office.FilterType.All

    "all"

    Return all data (not filtered by the host application).

    Office.FilterType.OnlyVisible

    "onlyVisible"

    Return only the visible data (as filtered by the host application).

    Writing:

    The added rows will be appended to the end of the table.

    Sample code:

    (function () {
        "use strict";
    
        // The initialize function must be run each time a new page is loaded
        Office.initialize = function (reason) {
            $(document).ready(function () {
                app.initialize();
    
                $('#get-data-from-selection').click(getDataFromSelection);
    
                $('#set-data-to-selection').click(setDataToSelection);
    
                $('#get-data-from-binding').click(getDataFromBinding);
    
                $('#add-data-to-binding').click(addDataToBinding);
            });
        };
    
        // Reads data from current document selection and displays a notification
        function getDataFromSelection() {
            Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
                function (result) {
                    if (result.status === Office.AsyncResultStatus.Succeeded) {
                        app.showNotification('The selected text is:', '"' + result.value + '"');
                    } else {
                        app.showNotification('Error:', result.error.message);
                    }
                }
            );
        }
    
        function getDataFromBinding()
        {
            Office.select('bindings#sampletablebinding').getDataAsync(
                { filterType: Office.FilterType.OnlyVisible },
                function (result) {
                    if (result.status === Office.AsyncResultStatus.Succeeded) {
                        app.showNotification('The binding value is:', '"' + result.value + '"');
                    } else {
                        app.showNotification('Error:', result.error.message);
                    }
                });
        }
    
        function addDataToBinding()
        {
            Office.context.document.bindings.getByIdAsync('sampletablebinding', function (result) {
                if (result.status === Office.AsyncResultStatus.Succeeded) {
                    result.value.addRowsAsync([['Jossef', 'Goldberg', 'Group-D'], ['Dylan', 'Miller', 'Group-E']], addRowsCallback);
                } else {
                    app.showNotification('Error:', result.error.message);
                }
            });
        }
    
        function addRowsCallback(result)
        {
            if (result.status === Office.AsyncResultStatus.Succeeded) {
                
            } else {
                app.showNotification('Error:', result.error.message);
            }
        }
    
        function setDataToSelection() {
    
            var table = createTableData();
    
            Office.context.document.setSelectedDataAsync(table, function (result) {
                if (result.status === Office.AsyncResultStatus.Succeeded) {
    
                    Office.context.document.bindings.addFromSelectionAsync(
                        Office.BindingType.Table,
                        { id: 'sampletablebinding' },
                        bindTablecallback);
                    
                } else {
                    app.showNotification('Error:', result.error.message);
                }
            });
        }
    
        function bindTablecallback(result)
        {
            if (result.status === Office.AsyncResultStatus.Succeeded) {
    
                app.showNotification('Info:', 'Bind sample table succeeded');
    
            } else {
                app.showNotification('Error:', result.error.message);
            }
        }
    
        function createTableData() {
            var tableData = new Office.TableData();
    
            tableData.headers = [['First Name', 'Last Name', 'Group']];
    
            tableData.rows = [
                ['Ken', 'Sánchez', 'Group-A'],
                ['Terri', 'Terri', 'Group-B'],
                ['Roberto', 'Tamburello', 'Group-A'],
                ['Rob', 'Walters', 'Group-B'],
                ['Gail', 'Erickson', 'Group-C']
            ];
    
            return tableData;
        }
    })();

    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.

    • Marked as answer by ian.gillis Tuesday, April 21, 2015 2:20 PM
    Tuesday, April 21, 2015 8:29 AM
    Moderator