Create Rows from split array data - javascript

I have a table which looks like the example i have provided below with this data
Table
T som da pap
------------------------------
A | soma | label | nami |
----------------------------
B | a:b | acha | wen:nda |
-----------------------------
C | d | k | nd:ke |
-----------------------------
Using the data in javascript/jquery i would like to
create a table like this based on count of pap
T som da pap
------------------------------
A | soma | label | nami |
----------------------------
B | a | acha | wen |
-----------------------------
B | b | acha | nda |
-----------------------------
C | d | k | nd |
-----------------------------
C | d | k | ke |
-----------------------------
Suggestions, I have tried but so far the double data i can only put in td instead of tr

Assuming there are always the same number of items in both cells.
$("#tableID tr").each(function() {
var som = $(this).find("td:eq(1)").text();
var som_array = som.split(':');
if (som_array.length > 1) {
var pap_array = $(this).find("td:eq(3)").text().split(':');
$.each(som_array, function(i, som_el) {
var pap_el = pap_array[i];
var new_row = $(this).clone();
new_row.eq(1).text(som_el);
new_row.eq(3).text(pap_el);
$(this).after(new_row);
}
$(this).remove();
}
});
This loops over all the rows in the table, and checks whether the som column contains multiple items. If it does, it split it up, and also splits up the pap column. Then it loops over these values, makes a copy of the original row, and replaces those columns with the elements of the split strings. Finally it removes the original row.

Related

Restrict Find and Replace function using map() to single column in a 2d array JavaScript

I am working in Google sheets
I have a find and replace function gotten from here. It works great when the array is made from a single column of data
But, in certain instances in which I want to use my function I have all my data sitting in an array which is being passed from prior processing, the data if printed to the page looks like
| Id | Segment |Other Column| etc..
|----|--------------------|------------|
| 1 | AAA AA|AA|CaaL AA |stuff |
| 2 | AAA-AA|AA|CaaL |stuff |
| 3 | AAA, AA|AA|AA |AA |
| 4 | AA |stuff |
| 5 | AA AA | |
| 6 | AA, AA |AA |
| 7 | |stuff |
| 8 | CaaL |stuff |
| 9 | AA |stuff |
I only want to replace data in the Segment column
| Id | Segment ||Other Column| etc..
|----|-------------------|------------|
| 1 | AAA AA|zz|CaaL AA |stuff |
| 2 | AAA-AA|zz|Bob |stuff |
| 3 | AAA, AA|zz|zz |AA |
| 4 | zz |stuff |
| 5 | AA AA | |
| 6 | AA, AA |AA |
| 7 | |stuff |
| 8 | Bob |stuff |
| 9 | zz |stuff |
As it is the AA in the Other Column (could be in any column, I have no way of knowing) is being replaced which I do not want
Can the findReplace function be restricted to a single column in values updating that only that column in values ?
I have tried getting the column array I want to restrict values to with
function getColumn(matrix, col, startrow){
var column = [];
for(var i=startrow; i<matrix.length; i++){
column.push(matrix[i][col]);
}
return column;
}
and then attempting to restrict the input to this column
function fr(input) {
const aCOL = getColumn(values,2,0)
return input.aCOL.map(c => c.replace(regex, (x) => map[x]));
AND
return input.map(c => c.aCOL.map(y => y.replace(regex, (x) => map[x])));
}
I get Cannot read property 'map' of undefined
Thanks
function findReplace(values) {
var search_for = ["AA", "Caal"];
var replace_with = ["zz", "yy"];
var map = {};
search_for.forEach(function(item, i) {
map[item] = replace_with[i];
});
//map = { AA: 'zz', Caal: 'yy' };
const regex = new RegExp("(?<![^|])(?:" + search_for.join("|") + ")(?![^|])", "g");
range.setValues(values.map(fr));
function fr(input) {
return input.map(c => c.replace(regex, (x) => map[x]));
}
}
The code you got from your previous question works fine if you just change the range you're retrieving. So instead of creating a function to extract the column into an array, you can just adjust the range.
If the column you want to replace ('Segment') is the second one, then you can do the following changes.
From:
const col = ss.getRange(2, 3, ss.getLastRow()).getValues();
...
ss.getRange(2, 3, ss.getLastRow()).setValues(col.map(fr));
To:
const col = ss.getRange(2, 2, ss.getLastRow()).getValues();
...
ss.getRange(2, 2, ss.getLastRow()).setValues(col.map(fr));

Automatically increment value by search /compare and match two columns in Google Sheets Script

I currently have to manually enter Sheet1 column B, but I'd like to auto increment it every time the script runs.
I am using a Google Sheets script.
This part is working.
How it works:
Sheet2 column A is a drop down list imported from Sheet1 column A
if Sheet2 column B is anniversary of todays date Sheet 2 column F is automated to be 'reset'
the working script then deletes the rows in Sheet 2 where F is 'reset' and adds new rows for each to the bottom
Currently using this script
function writeupReset() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Sheet2');
var r = s.getRange('C:C');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--){
if(v[0,i]=='reset'){
s.insertRowsAfter(251,1);
s.deleteRow(i+1);
}
}
}
Example of the working bits using todays date - 6/2/20
Sheet1 Sheet2 (before script) Sheet2 (after script)
A B A B C A B C
|---------------| |-------------------------| |-------------------------|
1 | name | resets | 1 | name | date | delet | 1 | name | date | delet |
|---------------| |-------------------------| |-------------------------|
2 | bill | 1 | 2 | bill | 6/2/19 | reset | 2 | alex | 9/5/19 | |
|---------------| |-------------------------| |-------------------------|
3 | mark | 5 | 3 | alex | 9/5/19 | | 3 | adam | 11/6/19 | |
|---------------| |-------------------------| |-------------------------|
4 | holy | 2 | 4 | adam | 11/6/19 | | 4 | tony | 12/1/19 | |
|---------------| |-------------------------| |-------------------------|
5 | tony | 0 | 5 | mark | 6/2/19 | reset | 5 | | | |
|---------------| |-------------------------| |-------------------------|
6 | alex | 2 | 6 | tony | 12/1/19 | | 6 | | | |
|---------------| |-------------------------| |-------------------------|
7 | june | 1 | 7 | | | | 7 | | | |
|---------------| |-------------------------| |-------------------------|
8 | jack | 0 | to 252 rows (last two hidden) to 252 rows (last two hidden)
|---------------|
9 | adam | 2 |
|---------------|
So how can I edit the script to automatically increment Sheet1 column B when the script deletes those 'reset' rows in Sheet1?
I believe your goal as follows.
You want to increase the values of the column "B", that the name is the same , in "Sheet1" when the rows with reset in "Sheet2" are deleted by the script.
In your case, the same names are included in the column "A" in "Sheet2".
You want to achieve this by modifying your script in your question.
For this, how about this answer?
Modification points:
In this case, it is required to know the values of "Sheet1" and compare them with the values of "Sheet2". The flow is as follows.
Delete rows in "Sheet2".
At that time, an object including the information of the deleted rows is created.
Increase values of the column "B" in "Sheet1".
The values of the column "B" are updated using the object, and the updated values are put to the sheet.
When above points are reflected to your script, it becomes as follows.
Modified script:
function writeupReset() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// 1. Delete rows in Sheet2.
var s = ss.getSheetByName('Sheet2');
var r = s.getRange('A1:C' + s.getLastRow()); // Modified
var v = r.getValues();
var obj = {}; // Added
for(var i=v.length-1;i>=0;i--){
if(v[i][2] == 'reset') { // Modified
s.insertRowsAfter(251,1);
s.deleteRow(i+1);
var name = v[i][0] // Added
obj[name] = obj[name] ? obj[name] + 1 : 1; // Added
}
}
// I added below script.
// 2. Increase values of the column "B" in Sheet1.
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("A2:B" + sheet.getLastRow());
var values = range.getValues().map(([a, b]) => ([obj[a] ? b += obj[a] : b]));
range.offset(0, 1, values.length, 1).setValues(values);
}
Note:
In this modified script, it supposes that each value of the column "A" in "Sheet1" is the unique value in all values. From your question, I could understand like this.
If the sheet names of "Sheet1" and "Sheet2" are different from your actual situation, please modify them.
If your actual situation is different from the structure of your sample values in your question, this modified script might not work. So please be careful this.
References:
map()
setValues(values)

Script : set in bold highest value

I got an array in Google Sheet that looks something like this :
+-----+---------+------------+------------+------------+-----+
| | A | B | C | D | ... |
+-----+---------+------------+------------+------------+-----+
| 0 | | Mission #1 | Mission #2 | Mission #3 | ... |
+-----+---------+------------+------------+------------+-----+
| 1 | Item #1 | 100 | 250 | 250 | ... |
+-----+---------+------------+------------+------------+-----+
| 2 | Item #2 | 200 | 1000 | 500 | ... |
+-----+---------+------------+------------+------------+-----+
| 3 | Item #3 | 300 | 3000 | 800 | ... |
+-----+---------+------------+------------+------------+-----+
| ... | ... | ... | ... | ... | ... |
+-----+---------+------------+------------+------------+-----+
I want to create a script that bold the highest value for each item. But there is a problem, as you can see, some items get the same value in different missions, what I need is in this case, only the "most right" needs to be bold.
So what I expect to do :
+-----+---------+------------+------------+------------+-----+
| | A | B | C | D | ... |
+-----+---------+------------+------------+------------+-----+
| 0 | | Mission #1 | Mission #2 | Mission #3 | ... |
+-----+---------+------------+------------+------------+-----+
| 1 | Item #1 | 100 | 250 | **250** | ... |
+-----+---------+------------+------------+------------+-----+
| 2 | Item #2 | 200 | **1000** | 500 | ... |
+-----+---------+------------+------------+------------+-----+
| 3 | Item #3 | 300 | **3000** | 800 | ... |
+-----+---------+------------+------------+------------+-----+
| ... | ... | ... | ... | ... | ... |
+-----+---------+------------+------------+------------+-----+
** = is bold
So I started to think :
var Item1High = max(b1;d1);
For each row (X1) if value == Item1High then set to bold
But at this state the most difficult part is to set in bold only the most right value (So the highest value of row)
And I'm stuck at this state.
Could you help me with that ?
I'm using Google Sheet Script in JavaScript.
Thanks !
You want to set the bold type to the maximum value in each row of Google Spreadsheet.
For example, when the column "B" and "C" is the same value and the maximum value, you want to set the bold to the column "C".
You want to achieve this using Google Apps Script.
The flow of this sample script is as follows.
Flow:
Retrieve all values from the sheet.
Create text styles.
Set the created text styles.
Sample script:
Before you run the script, please set the sheet name.
function myFunction() {
const sheetName = "Sheet1";
// 1. Retrieve all values from the sheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getDataRange();
const values = range.getValues();
values.shift();
// 2. Create text styles.
const tempStyle = SpreadsheetApp.newTextStyle().setBold(false).build();
const bold = SpreadsheetApp.newTextStyle().setBold(true).build();
const styles = values.map(r => {
r.shift();
let ar = Array(r.length).fill(tempStyle);
ar[r.lastIndexOf(Math.max(...r))] = bold;
return ar;
});
// 3. Set the created text styles.
range.offset(1, 1, styles.length, styles[0].length).setTextStyles(styles);
}
References:
newTextStyle()
setTextStyles()
Added:
In this sample script, the selected range is used.
function myFunction() {
const sheetName = "Sheet1";
const selectRange = "B2:D4"; // Please set the range you want to use.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const tempStyle = SpreadsheetApp.newTextStyle().setBold(false).build();
const bold = SpreadsheetApp.newTextStyle().setBold(true).build();
const range = sheet.getRange(selectRange);
const values = range.getValues();
const styles = values.map(r => {
let ar = Array(r.length).fill(tempStyle);
ar[r.lastIndexOf(Math.max(...r))] = bold;
return ar;
});
range.setTextStyles(styles);
}

Hide rows in table A that appear in table B

I have Table (Employees) A:
+----+---------+
| ID | NAME |
+----+---------+
| 1 | ROBERT |
| 2 | JAMES |
| 3 | RICHARD |
| 4 | KANYE |
| 5 | DYLAN |
| 6 | JOHN |
| 7 | JEAN |
| 8 | LOKI |
| 9 | ADAM |
+----+---------+
And Table (Employees) B:
+----+---------+
| ID | NAME |
+----+---------+
| 1 | ROBERT |
| 2 | JAMES |
| 3 | RICHARD |
| 4 | KANYE |
| 5 | DYLAN |
+----+---------+
Those people are my "employees". The names from table B are all the employees on my database and the names from table A are all the employees that I've assigned to project n°1.
I would like to HIDE all names in Table B that already appear in Table A because I don't want to re-assign the same employee to project n°1.
How can I resolve this using jQuery?
a clientside check would be:
function compareLists(listA, listB){
var resultList = [];
listA.forEach(function(itemA){
listB.forEach(function(itemB){
if (itemB.name !== itemA.name)resultList.push(itemB);
}
}
return resultList;
}
or at the serverside (database):
select * from table_b where name not in (select name from table_a)
greetings
Normally you should do that on the server side using SQL as already answered. However, since you ask about javascript. This is the way to filter it at client side:
//collect list of names in A
var table = document.getElementById("TableA");
var names = []
for (var i = 0, row; row = table.rows[i]; i++) {
names.push(row.celss[1]);
}
//check names in B
table = document.getElementById("TableB");
for (var i = 0, row; row = table.rows[i]; i++) {
if(jQuery.inArray(row.cells[1], names) !== -1){
//show row
}else{
//hide row
}
}
I dont have that much knowlwdge in Jquery..
But this query increases perfomance in a large DB.
SELECT
FROM TableB
LEFT JOIN TableA ON TableB.name=TableA.name // id will be better than name
WHERE TableB.name IS NULL
Hope this helps

Add autocalculate summation row and column in asp.net Listview

I'm trying to create ListView which contains ASPxTextBox that the user can enter only numeric value. There are many rows and columns of data. I want to add new row and column that calculate total value from each row and column
For example, if I have data like this:
Student Name | Subject1 | Subject2 | Subject3
----------------------------------------------------------
John ----------- | ------- 23 | --------45 | --------37
Smith ---------- | ------- 41 | --------22 | --------11
Linda ---------- | ------- 35 | --------38 | --------23
I want to be like this
Student Name | Subject1 | Subject2 | Subject3 | Total
------------------------------------------------------------------
John ----------- | ------- 23 | --------45 | --------37 | 105
Smith ---------- | ------- 41 | --------22 | --------11 | --74
Linda ---------- | ------- 35 | --------38 | --------23 | --96
------------------------------------------------------------------
Total----------- | --------99 | -------105 | --------71 | 275
The number of columns (Subject) is fixed but the number of rows depends on data in database.
There are 2 main problems here:
1. I want the summation row and column to execute calculation in client side using javascript. So when one of textboxes is lost focus, it will recalculate the summation value in each row and column.
2. I can't access the value from ASPxTextBox using javascript. I tried using:
var test = document.getElementById('<%# Container.FindControl("txt_Subject1").ClientID %>')
var data = test.value;
but it doesn't seem to work.
The following code also cause error:
var test = document.getElementById('<%# Container.FindControl("txt_Subject1").ClientID %>')
var data = test.GetValue();
And when I tried ClientInstanceName, it always return the value of the last row only:
var data = txt_Subject1.GetValue(); //doesn't work?
Also, I don't want to change ASPxTextBox to asp TextBox because I want to use some of its feature.
You could dinamically assign ClientInstanceName values based on row number:
txt_Subject1_1
txt_Subject1_2
...
txt_Subject1_n
Then use javascript to calculate sum:
var sum = 0;
for (var i = 1; i <= rowCount; i++)
{
sum += parseInt(window['txt_Subject1_' + i].GetValue());
}

Categories

Resources