I have a variable that stores some HTML:
var rows = '<tr><td></td></tr>.....';
and I want to use jQuery to count the number of <tr> tags there are. I can do it when it's not in a variable:
$('#some-table tr').length
but can't using the variable. I have tried something like this:
$(rows)
but not sure what to do with it to get the number of <tr>s out.
jQuery-ify the rows, then query:
var rows = '<tr><td></td></tr>.....';
var numRows = $(rows).filter('tr').length;
or...
var rows = '<tr><td></td></tr>.....';
var numRows = $('<tbody>' + rows + '</tbody>').find('tr').length;
// or
var numRows = $('<tbody>' + rows + '</tbody>').children().length;
var count = rows.match(/<tr>/g);
alert(count.length);
Related
I am trying to fetch the last row's row id (located in the first column) to increment it by one once there is a new row added. However I am not sure on how to convert the string into a number to increment by 1
What I tried:
var url = "";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Data");
var rowID = ws.getRange(ws.getLastRow(), 1).getValues().toString().parseInt()+1;
console.log(rowID);
Which is not a function based on the log.
If the values of your id is 1, 2, 3,,, , how about the following modification?
From:
var rowID = ws.getRange(ws.getLastRow(), 1).getValues().toString().parseInt()+1;
To:
var rowID = (parseInt(ws.getRange(ws.getLastRow(), 1).getValue(), 10) || 0) + 1;
or
var rowID = (Number(ws.getRange(ws.getLastRow(), 1).getValue()) || 0) + 1;
or, in the event the value in the last row is known to always be a number or a blank, the following modification might be able to be used.
var rowID = ws.getRange(ws.getLastRow(), 1).getValue() + 1;
Reference:
parseInt()
Below is a code that I found here : https://webapps.stackexchange.com/questions/123670/is-there-a-way-to-emulate-vlookup-in-google-script
I tried to optimize it to my use case in which
to vlookup from source sheet 'data', and fill in values in destination sheet 's'. The problem is that this code does this only for one row. Is there a way to loop over all rows and vlookup and fill in efficiently?
/* recall that we want the follwoing columns => E, F, G, H, M
/*/
function khalookup(){
var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = SpreadsheetApp.openById("mysheetid");
var searchValue = s.getRange("B2:B").getValues();
var dataValues = data.getRange("A3:A").getValues();
var dataList = dataValues.join("ღ").split("ღ");
var index = dataList.indexOf([searchValue]);
var newRange = []
var row = index + 3;
var foundValue = data.getRange("E"+row).getValue();
var foundValue1 = data.getRange("F"+row).getValue();
var foundValue2 = data.getRange("G"+row).getValue();
var foundValue3 = data.getRange("H"+row).getValue();
var foundValue4 = data.getRange("M"+row).getValue();
s.getRange("K2").setValue(foundValue);
s.getRange("L2").setValue(foundValue1);
s.getRange("M2").setValue(foundValue2);
s.getRange("N2").setValue(foundValue3);
s.getRange("O2").setValue(foundValue4);
}
here is the source sheet where the vlookup shall happen based on the ID "Column A"
And here is how the destination sheet shall look like after the vlookup based on ID "Column B" have been made.
You can do iterate with a loop, e.g. a for loop
Assuming you would like to loop through all rows from index + 3 to the last row, you can modify your code as following:
...
var row = index + 3;
var lastRow = s.getLastRow();
for (var i = row; i <= lastRow; i++){
var foundValue = data.getRange("E"+i).getValue();
var foundValue1 = data.getRange("F"+i).getValue();
var foundValue2 = data.getRange("G"+i).getValue();
var foundValue3 = data.getRange("H"+i).getValue();
var foundValue4 = data.getRange("M"+i).getValue();
s.getRange("K" + (2+i-row)).setValue(foundValue);
s.getRange("L" + (2+i-row)).setValue(foundValue1);
s.getRange("M" + (2+i-row)).setValue(foundValue2);
s.getRange("N" + (2+i-row)).setValue(foundValue3);
s.getRange("O" + (2+i-row)).setValue(foundValue4);
}
Note that later on you might want to progress from using getValue() and setValue() to getValues() and setValues() - that will make your code execution faster.
Count duplicate value from table column using j query and show in new td as total duplicate value.
i try this code but not see actual results.
$(document).ready(function(){
var myarry =[];
var el = {};
$("tbody tr").each(function() {
var row = $(this).nextAll();
var first = row.find('td').eq(0).text();
var second =row.find('td').eq(1).text();
var third = row.find('td').eq(2).text();
if(el[first + second + third]) {
myarry.push(el);
}else {
var c = (parseInt(myarry.length) + parseInt(1));
console.log(c);
row.find('td').eq(3).attr("rowspan",2);
el[first + second + third] = 1;
myarry =[];
}
});
});
it should be dynamic .
I means to say table data will be change in future some time there are 3 duplicate and some time 5 or etc.
I have an array within a spreadsheet that has stock data. I want to find a column number "restock value", and the row number product/sku (Row) to eventually build a custom function. The column is indexing fine, but the Row keeps logging 0.
I've tested a few objects within my array. I've also tried removing the column index. but no dice. still logging 0.
I've also tried removing [0] from my code. It seems that this is part of the problem, because when I log lookupRowValues without [0] I get the value of each row with their own individual brackets, when I add [0] I only log the first row.
function UnitsToShip(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("US");
//Lookup Column
var lc = ss.getLastColumn();
var lookupColumnValues = ss.getRange(4,2,1,lc).getValues()[0];
var indexColumn = lookupColumnValues.indexOf("Restock Amount") + 1;
//Lookup Row
var lr = ss.getLastRow();
var lookupRowValues = ss.getRange(4,2,lr,1).getValues()[0];
var indexRow = lookupRowValues.indexOf("AH-POR-DIF-WHT")+1;
Logger.log(indexRow);
}
I'm testing it with a product in the array that should return 6.
Try this:
function UnitsToShip(){
var ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("US");
//Lookup Column
var lc = ss.getLastColumn();
var lookupColumnValues = ss.getRange(4,2,1,lc-1).getValues()[0];
var indexColumn = lookupColumnValues.indexOf("Restock Amount") + 2;
Logger.log('indexColumn: %s',indexColumn);
//Lookup Row
var lr = ss.getLastRow();
var lookupRowValues = ss.getRange(4,2,lr-3,1).getValues().map(function(r){return r[0];});
var indexRow = lookupRowValues.indexOf("AH-POR-DIF-WHT")+4;
Logger.log('indexRow: %s',indexRow);
var restockAmount=ss.getRange(indexRow,indexColumn).getValue();
Logger.log('Restock Amount: %s',restockAmount);
}
The problem I'm having is with my function calculate(). The purpose of the program is creating a dynamic spreadsheet with JavaScript and html, with a CSS for decoration purposes.
So far I have it insert the values into the spreadsheet, however when attempting to run a simple addition calculation on it the values that I'm grabbing from the cells are not properly being grabbed.
Here's the function in specific that's giving me the problem. My apologies for the sloppy code. Any help would be appreciated, I'm sure it's something simple that I'm missing.
EDIT:: Corrected the issue, for those of you wondering, I had to call not innerHTML.text, or value. It had to be simply the .innerHTML on the cell in question.
function calculate() {
//create two variables to get the values in the row and columns
var row1, row2, col1, col2, total, totalInsert;
var row = document.getElementById("row");
var col = document.getElementById("col");
var value = document.getElementById(row + "_" + col);
var formula = document.getElementById("inputText");
formula = formula.value; //=SUM(1,2,2,1)
formula1 = formula;
//get the raw values to ints
row = row.value;
col = col.value;
//get JUST the formula in questions part
formula = formula.substr(0,4);
//Parsing the the selected cells from =sum(1,1,2,1) into Cell ID's
col1 = parseInt(formula1.substr(5, 1));//row 1
row1 = parseInt(formula1.substr(7, 1));
col2 = parseInt(formula1.substr(9, 1));
row2 = parseInt(formula1.substr(11, 1)); // column2
//this gives us the proper cell's address. id=1_1
var td1 = col1 + "_" + row1;
var td2 = col2 + "_" + row2;
//problem starts around here
var sum = document.getElementById(td1);
var sum2 = document.getElementById(td2);
//this returns a undefined value
sum = sum.value;
sum2 = sum2.value;
//this restults in a NaN
sum = parseInt(sum);
sum2 = parseInt(sum2);
//creating the total value
total = sum + sum2;
//returning values
totalInsert = document.getElementById(td1).innerHTML = total;
}
This part seems odd to me, you are selecting two elements and then combining them as a string to find another element?
var row = document.getElementById("row");
var col = document.getElementById("col");
var value = document.getElementById(row + "_" + col);
I'm not sure that would do what you are expecting... In my experience you'd get something like this:
document.getElementById('[object Object]_[object Object]');
or:
document.getElementById('[object HTMLDivElement]_[object HTMLDivElement]');
... depending on the browser. Now obviously you could have an element with that kind of id, but that would be a little bit bonkers ;)
It looks to me like the row and col elements are inputs, so maybe you actually mean:
var row = document.getElementById("row");
var col = document.getElementById("col");
var value = document.getElementById(row.value + "_" + col.value);