如何将特定单元格与列范围和 return 与列范围关联的字符串进行比较?
How do I compare a specific cell with a column range and return a string associated with the column range?
感谢阅读我的第一个问题。我刚开始 google sheets 请多多包涵。
Here is the sheet I'm working with
所以,这个 sheet 如果对于我玩的游戏,我们根据功率级别分配公会攻击。我想创建一个函数或脚本并将其放在 O 列中。我希望此函数将 F 列与 M 列中的特定单元格进行比较,然后 return 与 F 列关联的用户 > = 比列 M 中的特定单元格。像这样 enter image description here
我突出了前三个作为例子。
我显然可以手动执行此操作,但我需要时间,并且希望将此过程自动化以使其变得更有效率。我已经尝试过 Vlookups、MATCH、IF,但都没有成功。任何帮助将不胜感激。同样,我只是 google sheet 的初学者,所以请放轻松。 :)
解决方案
正如您提到的,您也会对脚本化解决方案感到满意,我创建了这个脚本,我相信它可以解决您的问题。它有评论逐步解释它是如何工作的:
function myFunction() {
// Get our sheet
var ss = SpreadsheetApp.getActive().getSheetByName('Automate Test');
// Get the values of the ranges of F and M. Flat will convert the 2D array we get from getValues() into a 1D one which is easier to work with
var valuesF = ss.getRange('F2:F16').getValues().flat();
var valuesD = ss.getRange('D2:D16').getValues().flat();
var valuesM = ss.getRange('M2:M16').getValues().flat();
var valuesN = ss.getRange('N17:N31').getValues().flat();
// We will iterate through all the players in column M to find their opponents
for(i=0;i<valuesM.length;i++){
// We create an empty array to be filled with the list of possible opponents to then choose a random one of the list
var playersHigherEqual = [];
// Iterate through the opponent list
for(j=0;j<valuesF.length;j++){
// If the opponent meets the condition
if(valuesF[j]>= valuesM[i]){
// Add it to the array of possible opponents
playersHigherEqual.push(ss.getRange(j+2, 2).getValue());
}
}
//Finally we will set the opponent by choosing a random one out of the list. Note that i+2 is because the arrays start from 0
ss.getRange(i+2, 15).setValue(playersHigherEqual[Math.floor(Math.random()*playersHigherEqual.length)]);
}
// We will iterate through all the players in column M to find their opponents
for(i=0;i<valuesN.length;i++){
// We create an empty array to be filled with the list of possible opponents to then choose a random one of the list
var playersHigherEqual = [];
// Iterate through the opponent list
for(j=0;j<valuesD.length;j++){
// If the opponent meets the condition
if(valuesD[j]>= valuesN[i]){
// Add it to the array of possible opponents
playersHigherEqual.push(ss.getRange(j+2, 2).getValue());
}
}
//Finally we will set the opponent by choosing a random one out of the list. Note that i+2 is because the arrays start from 0
ss.getRange(i+17, 15).setValue(playersHigherEqual[Math.floor(Math.random()*playersHigherEqual.length)]);
}
}
如果您还需要 sheet 这个问题的公式解,请告诉我。有关 Apps 脚本的更多信息,请查看 the documentation.
希望对您有所帮助。让我知道您是否需要其他任何东西或者您不明白什么。 :)
感谢阅读我的第一个问题。我刚开始 google sheets 请多多包涵。 Here is the sheet I'm working with
所以,这个 sheet 如果对于我玩的游戏,我们根据功率级别分配公会攻击。我想创建一个函数或脚本并将其放在 O 列中。我希望此函数将 F 列与 M 列中的特定单元格进行比较,然后 return 与 F 列关联的用户 > = 比列 M 中的特定单元格。像这样 enter image description here 我突出了前三个作为例子。
我显然可以手动执行此操作,但我需要时间,并且希望将此过程自动化以使其变得更有效率。我已经尝试过 Vlookups、MATCH、IF,但都没有成功。任何帮助将不胜感激。同样,我只是 google sheet 的初学者,所以请放轻松。 :)
解决方案
正如您提到的,您也会对脚本化解决方案感到满意,我创建了这个脚本,我相信它可以解决您的问题。它有评论逐步解释它是如何工作的:
function myFunction() {
// Get our sheet
var ss = SpreadsheetApp.getActive().getSheetByName('Automate Test');
// Get the values of the ranges of F and M. Flat will convert the 2D array we get from getValues() into a 1D one which is easier to work with
var valuesF = ss.getRange('F2:F16').getValues().flat();
var valuesD = ss.getRange('D2:D16').getValues().flat();
var valuesM = ss.getRange('M2:M16').getValues().flat();
var valuesN = ss.getRange('N17:N31').getValues().flat();
// We will iterate through all the players in column M to find their opponents
for(i=0;i<valuesM.length;i++){
// We create an empty array to be filled with the list of possible opponents to then choose a random one of the list
var playersHigherEqual = [];
// Iterate through the opponent list
for(j=0;j<valuesF.length;j++){
// If the opponent meets the condition
if(valuesF[j]>= valuesM[i]){
// Add it to the array of possible opponents
playersHigherEqual.push(ss.getRange(j+2, 2).getValue());
}
}
//Finally we will set the opponent by choosing a random one out of the list. Note that i+2 is because the arrays start from 0
ss.getRange(i+2, 15).setValue(playersHigherEqual[Math.floor(Math.random()*playersHigherEqual.length)]);
}
// We will iterate through all the players in column M to find their opponents
for(i=0;i<valuesN.length;i++){
// We create an empty array to be filled with the list of possible opponents to then choose a random one of the list
var playersHigherEqual = [];
// Iterate through the opponent list
for(j=0;j<valuesD.length;j++){
// If the opponent meets the condition
if(valuesD[j]>= valuesN[i]){
// Add it to the array of possible opponents
playersHigherEqual.push(ss.getRange(j+2, 2).getValue());
}
}
//Finally we will set the opponent by choosing a random one out of the list. Note that i+2 is because the arrays start from 0
ss.getRange(i+17, 15).setValue(playersHigherEqual[Math.floor(Math.random()*playersHigherEqual.length)]);
}
}
如果您还需要 sheet 这个问题的公式解,请告诉我。有关 Apps 脚本的更多信息,请查看 the documentation.
希望对您有所帮助。让我知道您是否需要其他任何东西或者您不明白什么。 :)