Solved

kendo spreadsheet data return

Posted on 2016-07-22
15
70 Views
Last Modified: 2016-07-26
hey guys. looking for a better solution. I am using kendo spreadsheet.  I need my data to return to a certain row. trying to make this happen dynamically.

Not to sure to go about this but looking for some great mines that will help in this case.
have a foreach loop. when a title is found the append json data. for this case 2 rows  but the way the format returns for this spreadsheet does not return correctly for more than one row. not sure.
    $scope.mysiz = []
angular.forEach($scope.get, function(value) {
            if (value.rowTitle == 'Meanze') {
                valuesM2 = ([value.current, '', value.cal, '', value.traili, ''
               , value.t, '', value.trail, '', value.tr]);
  $scope.mysiz +=valuesM2;
            }
        }, log);
         sheet.range("C9:M19").values($scope.mysize);


var Return as= [
            [ "1.3,1.3,-2.2,4.5,5.0,4.61.0,0.6,-2.9,4.1,4.8,4.2 ]
        ];

var Need to return as this format, because of brackets.= [
            [ "1.3,1.3,-2.2,4.5 ],
            [ "2",  8,  9, "3",  8,  9  ],
            [ "5",  6,  4,  "65", 4, 9  ]
        ];

Open in new window

0
Comment
Question by:sevensnake77
  • 7
  • 4
  • 4
15 Comments
 
LVL 27

Assisted Solution

by:BigRat
BigRat earned 250 total points
Comment Utility
You are actually creating a row of objects, each object being nothing more than an array. The following code creates a two dimensional matrix. I haven't a clue of why you include formatting strings in the data, nor do I know anything about "kendo" spreadsheets. But one thing is certain, updating scope each time in a loop is a performance bottleneck. Also updating globals is not very performant, so a bit of functional programming would not go a miss. The function formatData() takes the data and returns a matrix which is directly assigned to scope, so the rebuilding of the HTML DOM takes place only one. One last point. Why is the "get" data in $scope?

function formatData(data) {
   var matrix = [];
   angular.forEach(data,function(value) {
      if(value.rowTitle==='Meanze') {
         var thisRow = [value.current, '', value.cal, '', value.traili, ''
               , value.t, '', value.trail, '', value.tr];
         matrix.push(thisRow);
      };
   });
   return matrix;
}

  $scope.matrix = formatData($scope.get);
 

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
To expand on BigRat's solution you could also have done the following.
In your code you specified the context parameter log - but you don't seem to be using it. The following code demonstrates how you can use the context parameter to assign the rows to the return array
      var log = [];
      angular.forEach($scope.get, function(value) {
        if (value.rowTitle == 'Meanze') {
          var valuesM2 = [
            value.current, 
            '', 
            value.cal, 
            '', 
            value.traili, 
            '', 
            value.t, 
            '', 
            value.trail, 
            '', 
            value.tr
          ];
          // THE CONTEXT PARAMETER (log) BECOMES 'this' IN THE 
          // ITERATOR FUNCTION
          this.push(valuesM2);
        }
      }, log);
      $scope.mysiz = log;

Open in new window

1
 
LVL 9

Author Comment

by:sevensnake77
Comment Utility
Thats great. ok I done some testing and the problem is when i push it creates as on long json for example.

i get
'13,15,-2.2,4.5,5.0,4.6,1.0,0.6,-2.9,4.1,4.8,4.2,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA'

Open in new window


But for each for loop it should create a new row not one long string. What could I do about this.
0
 
LVL 9

Author Comment

by:sevensnake77
Comment Utility
for a better exAMPLE i need to have my json return in this format.
 var values = [
            [ "C 1", "C 2", "C 3" ],
            [ new Date(2000, 10, 10), 5, 6 ],
            [ new Date(2010, 10, 10), 8, 9 ],
            [ null,                   8, 9 ],
            [ new Date(2000, 1, 1),   3, 9 ]
        ];

Open in new window

0
 
LVL 9

Author Comment

by:sevensnake77
Comment Utility
There is no get in the scope, just an example. Thanks guys.  The array is creating one open array instead of breaking each row into multiply arrays
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Please post your code. The code I posted earlier does not create one long array.
0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
Just as a matter of interest, Julian, what is "this" in line 19 of your code?

http://justjs.com/posts/this-considered-harmful
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
@BigRat - this refers to the log (context) parameter to the forEach function
Refer Angular forEach

Specifically
Arguments
Param       Type         Details
obj         ObjectArray  Object to iterate over.
iterator    Function     Iterator function.
context     Object       Object to become context (this) for the iterator function.
(optional)

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Further to the above. The OP included the log parameter in his original source but never used it - I provided an example to show how it could be used (assumed he copied it from somewhere - wanted to put it in context so to speak).
0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
I personally find "this" very confusing. In the forEach case one has to think twice about it, because in any other context it would be completely different. The problem is always, that when somebody takes your code, mangles it, they might forget what exactly "this" was and as a consequence it would end up being miss-used. Given the fact that sevensnake77 now gets a single dimensional array a "this.push()" is highly likely to have produced that if he had mangled your code. "log" was probably there due to copying and probably referred to logging "if(log) log.write(...)" sort of thing.

If I may be permitted to quote Gwendolen, a girl after my own heart : " In matter of grave importance it is style which is the thing". Which is why passing a variable called log, which is really a matrix and referring to it as "this" is a break in style.
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 250 total points
Comment Utility
referring to it as "this" is a break in style
Well, you will need to take that up with the Angular team

The example was provided for clarity given the use of the log variable in the original code. It could just as easily have been expressed as the following with the same result. Note the removal of the log parameter and the change of this to log in the iteration function
      var log = [];
      angular.forEach($scope.get, function(value) {
        if (value.rowTitle == 'Meanze') {
          var valuesM2 = [
            value.current, 
            '', 
            value.cal, 
            '', 
            value.traili, 
            '', 
            value.t, 
            '', 
            value.trail, 
            '', 
            value.tr
          ];
          log.push(valuesM2);
        }
      });
      $scope.mysiz = log;

Open in new window

0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
Well, you will need to take that up with the Angular team

Well, they don't listen to old rats like me, who learnt their computer science almost fifty years ago.

And using the variable name "log" is still a break in style.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
And using the variable name "log" is still a break in style.
Perhaps but it came from the original code ...
0
 
LVL 9

Author Closing Comment

by:sevensnake77
Comment Utility
Thanks guys, I thought I closed this question. I got everything working thanks to your guys first 2 solutions.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Viewers will learn how to find and create templates in Excel 2013.
Viewers will learn how to create a PivotTable and make basic changes to it in Excel 2013.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now