Easy comparison between table rows - javascript

I am using DataTables JQuery plugins and I would like to ask the following two questions.
Is it possible to have MIN or MAX or AVG values calculated (dynamically) at the end of each column?
For example:
A/A A B C
L1 2 3 4
L2 3 4 5
If I would like to prin line 3 with the Min Values of each columnt should have been:
L3 2 3 4
Is it possible to create another column D which will contain the result of a formula for each row? For example Di = Ai + Bi?
According to the above example the resulted table should be the following:
L1 2 3 4 5
L2 3 4 5 7
Can I somehow perform those operations through DataTables or I have to prepare the data before passing it to the DataTables?
Thanks.
PS:
I suppose I need something similar to this trirand.net/examples/grid/functionality/footer/default.aspx but for DataTables plugin. In addition to that I need some formulas for rows as well. – salamis just now edit

Use mRender ( http://datatables.net/ref#mRender ) for the last column. mRender, when used as a function, has access the the data source object for the row (third parameter), so you can easily do a quick sum and then return the calculated value :-).

Related

How to compile a table of data in google sheets?

I am a total script kitty at best and am trying to use google apps scripts to automate a process for me. Here a sample table like the one I am working with(data is in google sheets spreadsheet):
Variety
Category
Peter
Courtney
DP
HBC
0.00
4.5
DP
MNG
2.00
0
UB
THN
7.00
0
471H
THN
5.00
0
471H
THN
0.00
5
GRH
FST
4.00
0
GRH
THN
8.00
0
GRH
THN
0.00
8
GRH
THN
0.00
8
HM
HBC
6.50
0
HM
HBC
0.00
6.5
HM
MNG
2.00
0
HM
MNG
0.00
2
CL
HBC
8.50
0
CL
HBC
7.00
0
PSV
HBC
2.50
0
PSV
HBC
7.00
0
PSV
HBC
0.00
2.5
The table shows the employees reported hours broken down by Variety and category. My goal is to compile this data so if the employees work at the same category and same variety I want all hours expressed in a single line. For instance if you look at the last two rows of the data you'll notice that the Variety and Category columns are matching which means the data should be compiled. So instead of:
Variety
Category
Peter
Courtney
PSV
HBC
7.00
0
PSV
HBC
0.00
2.5
I want to compile the data like this:
Variety
Category
Peter
Courtney
PSV
HBC
7.00
2.5
I am trying to do this from the bottom up because I heard going from the top down can cause issues but I am open to any suggestions. I think I need to write a for loop that compares the variety box of the active row to the variety box of the above row AND compares if the category box of the active row is equal to the category box of the above row and if both are true add the hours of both the "Peter" column and "Courtney" column.
I know there is more to do on top of that but this for loop is what I really need help on, unless you can suggest a better way?
Thanks for looking at my work,
JP
Stackoverflow member Ruben has asked me to elaborate more, here is where I am at:
I found a stackoverflow answer that pushed me in the direction of how to parse the data with the for loop. Here is the for loop skeleton I am working with:
function project() {
var ss = SpreadsheetApp.getActiveSheet();
var sheet = ss.getSheetByName("gest");
var data = sheet.getRange('A2:F19').getValues();
// Category and Variety columns
var leadV = sheet.getRange('B18');
var followV = sheet.getRange('B19');
var leadC = sheet.getRange('C18');
var followC = sheet.getRange('C19');
// Hour columns
var leadH1 = sheet.getRange('E18');
var followH1 = sheet.getRange('E19');
var leadH2 = sheet.getRange('F18');
var followH2 = sheet.getRange('F19');
for (var i = 0 ;i < data.length ; i++)
if leadV === followV AND leadC === followC,
// add both hours columns together
leadH1.getValue()+followH1.getValue()
leadH2.getValue()+followH2.getValue();
//Delete active row(not made yet)
else continue;
sheet.getRange(18-i, 2).activate();
My idea was to have a "lead" row and a "follow" row that would compare themselves and work up the data one by one through the for loop. I realize I need to use getActiveCell(), rather then getRange() but I don't understand how to compare an adjacent cell to the active cell while wrapping it into the for loop. I am just a bit overwhelmed and I think I might be over complicating my situation.
If you are open to using a formula instead of script, I can propose a solution. (In my own practice of decades, I save script for only those times when formulas cannot produce the desired result; this cuts down greatly on potential problems.)
This solution uses information from the script in your post to ascertain that the name of the source sheet is 'gest' and the ranges of data to include in the new report are B:C and E:F.
Create a new sheet and place the following formula in cell A1:
=ArrayFormula({gest!B1:C1, gest!E1:F1; QUERY({gest!B2:C, E2:F},"Select Col1, Col2, SUM(Col3), SUM(Col4) WHERE Col1 Is Not Null GROUP BY Col1, Col2 LABEL SUM(Col3) '', SUM(Col4) ''")})
This single formula will produce all headers and results, and will "keep up" as you add new rows of data in the 'gest' sheet.
A virtual array is formed between the curly brackets { }. This is a way of "sticking together" non-contiguous data in new ways.
The headers are grabbed by the two references preceding the semicolon.
Then a QUERY is formed from the non-header information in the target columns.
In plain English, the Select clause of the QUERY reads "Return four columns: exactly what is in the first requested column and the second requested column followed by the categorized sums from the third and fourth requested columns, separating (i.e., "GROUPing") those sums by each unique pairing from the first two columns." The LABEL section just removes technical headers from the sum columns created (which would otherwise have placed stock headers of 'sum' above each of the summed columns).

How to setup a range in "Script lab" for Office Excel add-ins , using variables (and not hard coded values for cell references)

Script lab shows examples of how to get a range using rows, columns and cell values here:
https://learn.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-ranges-advanced
However, all the values in examples were hardcoded. I could not find on any page any example of how to use variables in the get range? Maybe its a simple javascript thing but I am totally new to it. Can anyone share an example of how to do say?
sheet.getRange("4:9") using variables for number 4 and number 9?
And if you do know that answer for above, can you also share, how to do this using cell references in the below example?
Assume that I can find the values of rows and column names and set them in some variables. How would I use it in below code replacing the values for G1, A1 and E1?
sheet.getRange("G1").copyFrom("A1:E1");
Thanking you in advance for your help!
P.S: I already tried searching on stack overflow with keywords for "script lab range variables" but found no answers. Hence asking here.
Excel.run(function (context) {
var sheet = context.workbook.worksheets.getItem("Sample");
// Group the larger, main level. Note that the outline controls
// will be on row 10, meaning 4-9 will collapse and expand.
sheet.getRange("4:9").group(Excel.GroupOption.byRows);
// Group the smaller, sublevels. Note that the outline controls
// will be on rows 6 and 9, meaning 4-5 and 7-8 will collapse and expand.
sheet.getRange("4:5").group(Excel.GroupOption.byRows);
sheet.getRange("7:8").group(Excel.GroupOption.byRows);
// Group the larger, main level. Note that the outline controls
// will be on column R, meaning C-Q will collapse and expand.
sheet.getRange("C:Q").group(Excel.GroupOption.byColumns);
// Group the smaller, sublevels. Note that the outline controls
// will be on columns G, L, and R, meaning C-F, H-K, and M-P will collapse and expand.
sheet.getRange("C:F").group(Excel.GroupOption.byColumns);
sheet.getRange("H:K").group(Excel.GroupOption.byColumns);
sheet.getRange("M:P").group(Excel.GroupOption.byColumns);
return context.sync();
}).catch(errorHandlerFunction);
So, I got an answer on this here: https://github.com/OfficeDev/office-js-docs-pr/issues/1699
Thanks to AlexJerabek (you can find his id on the above github link).
In summary:
The getRange method takes in a string representing a cell range in the worksheet, such as "A1:D4", "A:D" for just the columns, or "1:4" for just the rows. As long as your variables map to Excel rows or columns, you can use string concatenation to build the argument. You may need to use range.columnIndex, range.rowIndex, and range.getCell to translate to and from zero-based numbers into the letter-based columns.
Based on this answer, I tried below (and it worked):
const sheet = context.workbook.worksheets.getActiveWorksheet();
var firstrow = 1
var lastRow = 6
var range = sheet.getRange(firstrow + ":" + lastRow)

Comparing values in two ranges in Google Sheets and counting the values greater than the other

I'm trying to solve a problem and I've been experimenting but can't get it to work.
Suppose I have two columns and I am trying to count the number of rows where the value in the first column are greater than the value in the second column.
So if the values in column A are 2,3,3,5 and the values in column B are 1,3,3,4 then the answer should be 2 because 2 is greater than 1 and 5 is greater than 4.
Any suggestions?
P.S. seems like I'm struggling to define a criterion to compare against that includes a value in a range.
=COUNTIF(ARRAYFORMULA(A1:A4>B1:B4), TRUE)
ARRAYFORMULA(COUNTIF(A1:A4,">"&B1:B4))

Put a new tr row between highest and lowest values based on its number using jq/js

I have the following table in html built with json (there are columns that describe what the numbers are also):
1
2
7
10
11
15
Now from a button i want is when user enters a new row, I want the number to go in its place. E.g. if 4, it should be between 2 and 7. If 14, it should be between 11 and 15.
$('tbody#days > tr:nth-child(' + daynumber-1 + ')').after(new_row_data);
failed to work because the index couldn't be found. It works sometimes if for e.g. I have 3 and 5 and tried to insert 4.
I thought have looping the column top to bottom but i usually have 100-113 rows and I am wondering if there is a practical and efficient way of doing this.
Any tips?
The snippet you posted should work fine. However, this will throw an error if the all days are greater than the input.
For efficiency, you could maintain an array of already inserted days and populate it as more days are being inserted.
Before each insertion, look through the array of days and find a spot for that day and just insert it right away. Code for that will be
$('tbody#days > tr').eq( lowerIndex ).after( new_row_data );

Titanium SQLite: Database pseudo array

I have a working app that needs to store up to 4 matrixes of integer data per record. I'm not sure how to get there with Titanium and SQlite.
A record will contain at least 1 but up to 4 matrixes of integers:
The matrix size is variable, each matrix consists of:
1 - 20 rows with 3 columns per row
OR
1 - 20 rows with 6 columns per row
The matrix structure will be identical for each record, i.e. 3 3x20 matrixes in
a record or 4 6x10 matrixes in a record. At this point my app starts, allows the user to choose the matrix parameters then accepts the data entry to fill in the matrix values. The matrixes are actually an JS array of arrays. How can I store an array of arrays and read it back in when I need to?
Edit: Let me see if I can clarify...
The app I'm working on is a scorecard for archery tournaments, similar in concept to a scorecard in golf. In archery you shoot for a set number of ends with a set number of arrows shot per end. The app asks for the number of ends (up to 20) and the number of arrows shot per end (3 or 6). After each shot the archer enters the score (an integer value). so for argument's sake say we're scoring for three ends with three arrows per end. We might see something like this:
arrow scores
8 8 9 (end 1)
7 9 10 (end 2)
9 9 10 (end 3)
There's my matrix that I need to save for this individual record. However, the next tournament I need to score may have a different number of ends and arrows:
arrow scores
7 8 9 10 10 10 (end 1)
10 9 9 7 8 10 (end 2)
9 6 6 6 9 9 (end 3)
7 8 6 7 8 8 (end 4)
10 10 9 8 8 8 (end 5)
Let's simplify and say that I want to store the scorecard for one archer per record. I already have my data entry and score tabulation working. I just don't understand the best way to store matrices as illustrated above.
I would recommend against storing an array of arrays. Generally speaking, its not terribly efficient to write abstractions with code that are meaningful for reasoning about matrices when make array of arrays a firm concept. The only exception I've seen is matlab/octave.
I've always found I end up with simpler code when I flatten the data. With a flat array you'll have to manage the indexing yourself. A few helper functions make that simple to reason about.
I'm not given much info to go on, but I'd assume that putting the data into two different tables will make things simpler.
CREATE TABLE mat3x20 (i1j1, i1j2, i1j2 ....
CREATE TABLE mat6x10 (i1j1, i1j2, i1j2 ....
Otherwise you have some weird behavior flag in the data which will make it less obvious what the code is supposed to be doing somewhere in the call stack.

Categories