0

I'm working on an add-in for excel 2016 using the javascript API. I can successfully get the range into an array and get the values to show in console.log. I've also been able to get the values into a JSON array using JSON.stringify();

I need to manipulate the array to remove the empty values (""). Can this be accomplished by using regular javascript array methods?

I'm thinking I can display the results back into a different worksheet using a similar approach like i did with var shWk

Here are some snippets of what I'm trying to do:


 (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();
                //document.getElementById("date").innerHTML = Date("MAR 30 2017");
                $('#deleteTab').click(deleteTab);
                $('#preview').click(preview);
                $('#publish').click(publish);

            });
        };

        function preview() {
            Excel.run(function(ctx) {
              //getting the colname from a date range in B2
            var colName = ctx.workbook.worksheets.getItem('preview').getRange("B2");
            colName.load('values');
            return ctx.sync().then(function() {
              //converting colname value to string for column name
                var wkN = (colName.values).toString();
                // displaying on the task pane
                document.getElementById("tst").innerText = wkN;
                // testing to confirm i got the correct colname
                var shWk = ctx.workbook.worksheets.getItem('preview').getRange("B3");
                shWk.values = colName.values;
                //building the column connection by setting the table name located on a different worksheet 
                var tblName = 'PILOT_ZMRP1';
                var tblWK = ctx.workbook.tables.getItem(tblName).columns.getItem(wkN);         
                //loading up tblWK
                tblWK.load('values');
                return ctx.sync().then(function(){
                //this is where my question is:
                    var arry = tblWK.values;
                    for (var i=0; i < tblWK.length; i++){
                        if (tblWK.values !== ""){
                        arry.values[i][0]) = tblWK.values[i][0]
                        };
                    };
                    console.log(arry.length); //returns 185
                    console.log (arry.values);//returns undefined
                    tblWK.values = arry;
                    var tblWeek = tblWK.values;
                    console.log(tblWeek.length);//returns 185
                    console.log(tblWK.values);//returns [object Array]  [Array[1],Array[2]
               })              
            });
}).catch(function (error) {
 console.log(error);
    console.log("debug info: " + JSON.stringify(error.debugInfo));
});
}

What am I missing? Can you point me to some resources for javascript array handling in the specific context of office.js?

j_unknown
  • 55
  • 1
  • 8
  • Possible duplicate of [Remove empty elements from an array in Javascript](http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript) – Blue Jan 22 '17 at 22:47
  • Are you trying to just replace the empty-string values with nulls (and why?), or does "remove" mean something else to you? What is the bigger-picture scenario that you're after? – Michael Zlatkovsky - Microsoft Jan 23 '17 at 02:14
  • I'm looking to remove the empty values returned in the array. Some cells contain a value and some contain "". These values will be different for each column. What I'm trying achieve is an array that only contains a true value not an array that contains "" and a value such as "text' or 16 (number). – j_unknown Jan 23 '17 at 02:31
  • It looks like you have bugs in your code. If all items are "", what you see is the outcome I would expect too. If there is an item that is not a "", I would expect an error. If you don't see the error, add a "use strict" between these 2 lines, and the compiler should force it: return ctx.sync().then(function(){ //this is where my question is: – Zlatko Michailov - MSFT Jan 23 '17 at 18:10

3 Answers3

1

I want to thank everyone for the time spent looking at this question. This is my second question ever posted on Stack Overflow. I see that the question was not written as clear as it could've been. What i was trying to achieve was filtering out the values in a 1D array that had "". The data populating the array was from a column in a separate worksheet that had empty values (hence the "") and numeric values in it. the code below resolved my issue.

//using .filter()
        var itm = tblWK.values;
        function filt(itm){
            return itm != "";
        }
        var arry = [];
        var sht = [];
        var j=0;
        var s=0;
        arry.values = tblWK.values.filter(filt);
//then to  build the display range to show the values:

 for (var i=0; i < itm.length-1; i++) {
        if (tblWK.values[i][0]){
        var arry;    //tblWK.values.splice(i,0); -splice did not work, maybe my syntax was wrong?
        console.log("this printed: "+tblWK.values[i][0]);
        var cl = ('D'+i);          //building the range for display
        j++;                        //increasing the range
s=1;//setting the beignning range
        var cll = cl.toString();//getRange() must be a string
        console.log(cll);//testing the output
        }       
}
//using the variable from the for loop
      var cl = ('D'+s+':D'+j);
      var cll = cl.toString();
      console.log(cll);//testing the build string
      sht = ctx.workbook.worksheets.getItem('Preview').getRange(cll);
      sht.values = arry.values; //displays on the preview tab
      console.log (arry.values); //testing the output

The question was probably easier said by asking what vanilla javascript functions does office.js support. I found a lot help reading Building Office Add-ins using Office.js by Micheal Zlatkovsky and by reading the MDN documentation as well as the suggested answer posted here.

Regards, J

Community
  • 1
  • 1
j_unknown
  • 55
  • 1
  • 8
0

I'm not sure what this check is trying to achieve: tblWK.values !== "". .values is a 2D array and won't ever be "".

For Excel, the value "" means that the cell is empty. In other words, if you want to clear a cell, you assign to "". null value assignment results in no-op.

Sudhi Ramamurthy
  • 2,358
  • 1
  • 10
  • 14
-1

You can just fetch the values form the array that contains null by using for each and can can push the null values into another array.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ishu
  • 1
  • 1