[英]Google Apps Script Filtering
I have some data in google sheet which I want to filter based on a certain criteria and return a corresponding value from another column.我在 google 工作表中有一些数据,我想根据特定条件进行过滤并从另一列返回相应的值。 Lastly, count the number of elements in the returned column.
最后,计算返回列中的元素数。 Here is a sample data:
这是一个示例数据:
A![]() |
B![]() |
|
---|---|---|
1 ![]() |
Initials![]() |
Application Reference![]() |
2 ![]() |
MWB.KBB ![]() |
1001 ![]() |
3 ![]() |
JET,JJB ![]() |
1002 ![]() |
4 ![]() |
KBB ![]() |
100,310,041,005 ![]() |
5 ![]() |
MKGC ![]() |
1006 ![]() |
6 ![]() |
KBB ![]() |
1007 ![]() |
Let's say I want to filter the data by searching for "KBB".假设我想通过搜索“KBB”来过滤数据。 I want to get all cells that contain the word "KBB" which should be three (3) cells.
我想获取所有包含单词“KBB”的单元格,这应该是三 (3) 个单元格。 However, I am only getting two in return.
但是,我只得到两个回报。 The 1st row that contain two elements in a single cell is not included but it should be included.
在单个单元格中包含两个元素的第一行不包括在内,但应该包括在内。 Lastly, count the elements in the returned column based on the criteria.
最后根据条件统计返回列中的元素。
Here's the code I have tried:这是我试过的代码:
function filter(){
//opened ss via url
const ws = ss.getSheetByName("Sample");
const range = ws.getRange(2,1,ws.getLastRow() - 1,2).getValues();
const initial = range.map(function(n){return n[0];});
const filtered = initial.filter(filterLogic);
Logger.log(initial); // [MWP, KBB, JET, JJB, KBB, MKGC, KBB]
Logger.log(filtered); // [KBB, KBB]
}
function filterLogic(name){
if(name == "KBB"){
return true;
} else {
return false;
}
}
The above code is only for the criteria.上面的代码仅用于标准。 Not included is the counting of elements for the returned value from another column after the filter is applied.
不包括应用过滤器后从另一列返回的值的元素计数。
What should I do so I can include the first row that contains the text "KBB" as well in my filtered data.我应该怎么做才能在过滤后的数据中包含包含文本“KBB”的第一行。 Is there any other way around this?
有没有其他办法解决这个问题?
Code:代码:
function searchForKBB(n = "KBB") {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const osh = ss.getSheetByName("Sheet1");
let o = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).createTextFinder(n).matchEntireCell(false).findAll().map(rg => [rg.getA1Notation()]);
o.unshift(["Ranges"]);
osh.getRange(1,1,o.length,o[0].length).setValues(o)
}
Data:数据:
A![]() |
B![]() |
|
---|---|---|
1 ![]() |
Initials![]() |
Application Reference![]() |
2 ![]() |
MWB.KBB ![]() |
1001 ![]() |
3 ![]() |
JET,JJB ![]() |
1002 ![]() |
4 ![]() |
KBB ![]() |
100,310,041,005 ![]() |
5 ![]() |
MKGC ![]() |
1006 ![]() |
6 ![]() |
KBB ![]() |
1007 ![]() |
Results:结果:
Ranges![]() |
---|
A2 ![]() |
A4 ![]() |
A6 ![]() |
Maybe you can do this:也许你可以这样做:
```
function getAllKBBs(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ss1 = ss.getSheetByName("YOUR_SHEET_NAME");
var range = ss1.getRange(1,1,ss1.getLastRow(),4).getValues();
output = whenTextContains("KBB", range, 1, 1);
Logger.log(output.length);
} ```
where whenTextContains() function is in this repository https://github.com/NikolaPlusEqual/GoogleAppsScriptFilters/blob/main/Functions其中 whenTextContains() function 在此存储库中https://github.com/NikolaPlusEqual/GoogleAppsScriptFilters/blob/main/Functions
Or, you can copy this into you code and call above function:或者,您可以将其复制到您的代码中并在上面调用 function:
function letterToColumn(letter){
var column = 0, length = letter.length;
for (var i = 0; i < length; i++)
{
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return column;
}
////// source for letterToColumn() function :
////// https://stackoverflow.com/questions/21229180/convert-column-index-into-corresponding-column-letter
var setData = {}
function whenTextContains(txt, rng, col, targetCol = 0){
if (typeof col == "number" && typeof txt == "string"){
setData.col = col;
setData.txt = txt;
}
else{
return;
}
var output = rng.filter(wtc);
if(targetCol == 0){
return output;
}
else if(typeof targetCol == "number"){
var result = output.map(function (item) {
return item[targetCol-1];
});
return result;
}
else if(typeof targetCol == "string"){
var targetnum = letterToColumn(targetCol);
var result = output.map(function (item) {
return item[targetnum-1];
});
return result;
}
else{
return;
}
}
function wtc(ar){
var txt = setData.txt;
var col = setData.col - 1;
var str = ar[col].toString();
return str.includes(txt);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.