在所有工作表中搜索在其旁边的单元格中具有特定值的字符串
Search through all sheets for a string which have a specific value in the cell next to it
我有一个自定义公式脚本,它循环遍历所有标签名称中包含“@”的工作表。在那里,我然后查看范围以找到我正在搜索的字符串(作为参数给出)。如果我在该范围内找到该字符串,我 运行 一些其他代码来检查其他单元格。
这里的重要部分是找到范围内的字符串。这是我的代码。
/**
* Searches through the box scores and counts Goaltender Wins.
*
* @param {"Hollywood|Hattricks|HOL"} teamName The team to search for.
* @param {"Z1"} dummy The cell to store random number in order to refresh values on editing the box scores.
* @return {number} The number of times the item appears in the range(s).
* @customfunction
*/
function calcGoaltenderWins(username, dummy) {
var count = 0;
SpreadsheetApp.getActive().getSheets().forEach(function (s) {
//regular season sheets
if (s.getName().indexOf("@") >= 0 && s.getName().indexOf(":") === -1) {
//if sheet is finalized
if (s.getRange("U37").getDisplayValue() != "Live ⬤") {
//if the home team won
if (s.getRange("Q11").getValue() < s.getRange("Q12").getValue()) {
//check if goaltender is on the player list for that game
s.getRange("Y5:Y12")
.getValues()
.reduce(function (a, b) {
return a.concat(b);
})
.forEach(function (v) {
if (v === username) count += 1;
});
}
//if the away team won
if (s.getRange("Q12").getValue() < s.getRange("Q11").getValue()) {
//check if goaltender is on the player list for that game
s.getRange("D5:D12")
.getValues()
.reduce(function (a, b) {
return a.concat(b);
})
.forEach(function (v) {
if (v === username) count += 1;
});
}
}
}
});
return count;
}
我想弄清楚的是如何检查我正在检查的字符串是否在其左侧 2 个单元格处具有特定值。这个值应该是 "G".
例如,我使用了公式=calcGoaltenderWins("Name", dummy)
。如果在 D5:D12 范围内找到名称 "Name"(特别是在 D5 中),那么它也应该只检查 B5(左边的单元格 2)是否等于 "G" 按顺序继续 运行 宁代码。如果它不等于 "G",那么它不应该响应它找到的字符串。
试试这个:
function calcGoaltenderWins(username, dummy) {
var count = 0;
const ss=SpreadsheetApp.getActive();
const shts=ss.getSheets()
shts.forEach(function(s) {
if (s.getName().indexOf("@")>=0 && s.getName().indexOf(":")==-1) {
if (s.getRange("U37").getDisplayValue()!="Live ⬤") {
if (s.getRange("Q11").getValue() < s.getRange("Q12").getValue()) {
s.getRange("Y5:Y12").getValues().reduce(function(a,b){return a.concat(b);}).forEach(function(v){if (v==username)count+=1;});
}
if(s.getRange("Q12").getValue() < s.getRange("Q11").getValue()) {
let v=s.getRange("B5:B12").getValues();//added
s.getRange("D5:D12").getValues().reduce(function(a,b,i) {if(v[i][0]=="G"){return a.concat(b)}else{return a;};}).forEach(function(v){if (v==username)count += 1;//edited
});
}
}
}
});
return count;
}
我有一个自定义公式脚本,它循环遍历所有标签名称中包含“@”的工作表。在那里,我然后查看范围以找到我正在搜索的字符串(作为参数给出)。如果我在该范围内找到该字符串,我 运行 一些其他代码来检查其他单元格。
这里的重要部分是找到范围内的字符串。这是我的代码。
/**
* Searches through the box scores and counts Goaltender Wins.
*
* @param {"Hollywood|Hattricks|HOL"} teamName The team to search for.
* @param {"Z1"} dummy The cell to store random number in order to refresh values on editing the box scores.
* @return {number} The number of times the item appears in the range(s).
* @customfunction
*/
function calcGoaltenderWins(username, dummy) {
var count = 0;
SpreadsheetApp.getActive().getSheets().forEach(function (s) {
//regular season sheets
if (s.getName().indexOf("@") >= 0 && s.getName().indexOf(":") === -1) {
//if sheet is finalized
if (s.getRange("U37").getDisplayValue() != "Live ⬤") {
//if the home team won
if (s.getRange("Q11").getValue() < s.getRange("Q12").getValue()) {
//check if goaltender is on the player list for that game
s.getRange("Y5:Y12")
.getValues()
.reduce(function (a, b) {
return a.concat(b);
})
.forEach(function (v) {
if (v === username) count += 1;
});
}
//if the away team won
if (s.getRange("Q12").getValue() < s.getRange("Q11").getValue()) {
//check if goaltender is on the player list for that game
s.getRange("D5:D12")
.getValues()
.reduce(function (a, b) {
return a.concat(b);
})
.forEach(function (v) {
if (v === username) count += 1;
});
}
}
}
});
return count;
}
我想弄清楚的是如何检查我正在检查的字符串是否在其左侧 2 个单元格处具有特定值。这个值应该是 "G".
例如,我使用了公式=calcGoaltenderWins("Name", dummy)
。如果在 D5:D12 范围内找到名称 "Name"(特别是在 D5 中),那么它也应该只检查 B5(左边的单元格 2)是否等于 "G" 按顺序继续 运行 宁代码。如果它不等于 "G",那么它不应该响应它找到的字符串。
试试这个:
function calcGoaltenderWins(username, dummy) {
var count = 0;
const ss=SpreadsheetApp.getActive();
const shts=ss.getSheets()
shts.forEach(function(s) {
if (s.getName().indexOf("@")>=0 && s.getName().indexOf(":")==-1) {
if (s.getRange("U37").getDisplayValue()!="Live ⬤") {
if (s.getRange("Q11").getValue() < s.getRange("Q12").getValue()) {
s.getRange("Y5:Y12").getValues().reduce(function(a,b){return a.concat(b);}).forEach(function(v){if (v==username)count+=1;});
}
if(s.getRange("Q12").getValue() < s.getRange("Q11").getValue()) {
let v=s.getRange("B5:B12").getValues();//added
s.getRange("D5:D12").getValues().reduce(function(a,b,i) {if(v[i][0]=="G"){return a.concat(b)}else{return a;};}).forEach(function(v){if (v==username)count += 1;//edited
});
}
}
}
});
return count;
}