I want to get the value of all the cells of rows. But not from all the rows, only rows having specific name in a specific column.
I attached a screenshot. I want to get all row values which "userID" is "user3". It means row 4 row 6 data.
I used following code.
function getByName(colName, row) {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var col = data[0].indexOf(colName);
if (col != -1) {
return data[row-1][col];
}
}
Above I getting only specific cell value in the user ID column. Not all data in the ROW.
function getAllrowData() {
var user3 = getByName("userID",2);
var values = user3.getDisplayValues()
Logger.log(values)
return values
}
getByname
giving only cell value.
I want following result so I can display data in html, belongs to only "user 3" in the image. Help me to correct my code.
C 25 30 0 16 user3
E 28 36 6 19 user3
CodePudding user response:
Remove [col]
from return data[row-1][col];
The above because sheet.getDataRange().getValues();
return an Array of Arrays of values. Using two indexes returns a cell value, using only the first index will return an Array having all the row values.
CodePudding user response:
Get Data by Row Number and Column Name:
function getByName(colName = 'COL25', row = 19) {
const sheet = SpreadsheetApp.getActiveSheet();
const data = sheet.getDataRange().getValues();
const hA = data.shift();
const idx = hA.reduce((a, h, i) => (a[h] = i, a), {});
Logger.log(data[row - 2][idx[colName]]);
}
A | B | C | D | E | F | G | H | I | J |
---|---|---|---|---|---|---|---|---|---|
COL1 | COL2 | COL3 | COL4 | COL5 | COL6 | COL7 | COL8 | COL9 | COL10 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |