none
excel add-in, delete range only partially RRS feed

  • Question

  • Hi

    I wrote some code to select and delete a table for my office add-in. But it deletes only partially the bodyrange. If I execute the function 3 times with an different argument, it seems it only executes the function with the last argumen.

    Is there a easy solution for this?

    Thanks,

    Emiel

    function deleteTable(name) {
            Excel.run(function (ctx) {
                var tableName = name;
                var table = ctx.workbook.tables.getItem(tableName);
                var tableDataRange = table.getDataBodyRange();
                tableDataRange.load('address')
                //var range = tableDataRange.address;
                //tableDataRange.address.delete();
                return ctx.sync().then(function () {
                    //console.log(tableDataRange.address);
                    var range = tableDataRange.address;
                    var rangeAddress = range.substring(range.indexOf('!') + 1);
                    localStorage.setItem('rangeAddress', rangeAddress);
                    var sheetName = range.substring(0, range.indexOf('!'));
                    localStorage.setItem('sheetName', sheetName);
                    //app.showNotification(sheetName);
                    
                });
            }).catch(function (error) {
                console.log("Error: " + error);
                if (error instanceof OfficeExtension.Error) {
                    console.log("Debug info: " + JSON.stringify(error.debugInfo));
                }
            });
    
            Excel.run(function (ctx) {
                var range = ctx.workbook.worksheets.getItem(localStorage.getItem("sheetName")).getRange(localStorage.getItem("rangeAddress"));
                //app.showNotification(localStorage.getItem("rangeAddress"));
                range.delete();
                return ctx.sync();
            }).catch(function (error) {
                //app.showNotification("Error: " + error);
                if (error instanceof OfficeExtension.Error) {
                    //app.showNotification("Debug info: " + JSON.stringify(error.debugInfo));
                }
            });
        };


    • Edited by beemihae Thursday, August 4, 2016 1:58 PM
    Thursday, August 4, 2016 1:48 PM

Answers

  • Hi beemihae,

    Do you mean you want to delete entire table? You could get Range from table, and delete this Range. Your issue is caused by that you get DataBodyRange instead of table Range.

    Here is a simple code:

        function deleteTable() {
            Excel.run(function (ctx) {
                var table = ctx.workbook.tables.getItem(tableName);
                var tableRange = table.getRange();
                tableRange.delete();            
            }).catch(function (error) {
                console.log("Error: " + error);
                if (error instanceof OfficeExtension.Error) {
                    console.log("Debug info: " + JSON.stringify(error.debugInfo));
                }
            });
        }

    Best Regards,

    Edward


    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, August 5, 2016 2:25 AM