我正在寻找帮助比较一个数组内部的单元格来说出一个特定的单词,如果说出这个单词,检查另一个相应的单元格是否包含一个字符串。 (在这种情况下,“Apple”是特定的单词,苹果的类型被列为“黄金”。)我不知道有多少用户,所以这有助于检查连续的每个有效用户。
在这个例子中,我正在看用户输入三个水果选择,如果他们指定Apple
,Pear
,Banana
或Plum
,他们必须填写相应的单元格,根据它是第一,第二或第三选择来说明什么类型。一些选定的关键词可能出现在其他选择中,例如Apple
作为Jessica
的第一选择,但John
的第三选择。我们不在乎它是出现在第二个还是第三个选择中。只关心以下选择:Apple
必须是首选,Pear
必须是第一选择,Banana
必须是第二选择,Plum
必须是第三选择。然后,我将使用脚本验证信息是真还是假。
以下是我所指的:
Name Fruit 1 Fruit 2 Fruit 3
Joanne Pear Orange Kiwi
John Berries Peach Apple
Juno Tomato Grapes Lemon
Jessica Apple Banana Pomegranate
(Type of Apple) = Golden
(Type of Pear) = Barley
(Type of Banana) =
(Type of Plum) =
在上面的图片中,“Banana”被列为Jessica的第二选择水果,但是在下面的“香蕉类型”中没有填写,所以这应该是假的。如果Banana包含一个随机字符串,并且由于其中一个用户将其列为他或她的第二选择,这将返回true。
这是我一直在尝试的代码,但它不断返回false,我不知道为什么:
function validateInfo() {
var data = SpreadsheetApp.getActive().getSheetByName('Sheet1')
.getDataRange().getValues();
Logger.log(data[8][2]);
var valid = true,
notBlank;
data.slice(8).every(function(row) {
notBlank = row[1].toString().length;
if (notBlank) {
valid = (row[2] === 'Apple' && data[0][6].toString().length > 0 || row[2] === 'Pear' && data[1][6].toString().length > 0 || row[3] === 'Banana' && data[2][6].toString().length > 0 || row[4] === 'Plum' && data[3][6].toString().length > 0);
}
return (notBlank && valid);
});
Logger.log(valid);
}
问题
OP的代码不断返回false。此外,根据我自己的测试,代码不是评估每个人,也不是准确评估水果选择。
原因
every(function(row)
没有有效运作。这被一个简单的“for”循环取代码
function so5480624202() {
// setup spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get data range and values
var range = ss.getSheetByName('Sheet1').getDataRange();
//Logger.log("DEBUG: the range is "+range.getA1Notation());//DEBUG
var data = range.getValues();
// define variables and existing values for the fruit types
var appletype = data[0][6];
var peartype = data[1][6];
var bananatype = data[2][6];
var plumtype = data[3][6];
//Logger.log("DEBUG: apple type: "+appletype+"("+appletype.toString().length+"), pear type: "+peartype+"("+peartype.toString().length+"), banana type: "+bananatype+"("+bananatype.toString().length+"), plum type:"+plumtype+"("+plumtype.toString().length+")");//DEBUG
// define variables
var valid = true,
notBlank;
// slice the data from the eight row to thr end.
var newdata = data.slice(8);
//Logger.log("DEBUG: newdata length: "+newdata.length);//DEBUG
// Loop through the sliced rows
for (x = 0; x < newdata.length; x++) {
// check the length of the name; if name - 0, then row is blank
notBlank = newdata[x][1].toString().length;
// define the fruittypes for this person
var fruittype1 = newdata[x][2];
var fruittype2 = newdata[x][3];
var fruittype3 = newdata[x][4];
// Logger.log("DEBUG: Loop#:"+x+" Name:"+newdata[x][1]+" Fruits: "+fruittype1+", "+fruittype2+", and "+fruittype3);//DEBUG
// if there is a name
if (notBlank) {
// test for "Apple"
if (fruittype1 == "Apple" || fruittype2 == "Apple" || fruittype3 == "Apple") {
// the Person has chosen an Apple
// Logger.log("DEBUG: there is an apple in this row"); //DEBUG
// if the appletype has a value
if (appletype.toString().length > 0) {
// do nothing
// Logger.log("DEBUG: there is an entry in the apple type, so do nothing");//DEBUG
} else {
//Logger.log("DEBUG: there is NO entry in the apple type, so something");//DEBUG
data[0][6] = "TBA";
}
} else {
// Logger.log("DEBUG: there was no Apple chosen in this row - ignore Apple Type\n"); //DEBUG
}
// test for Pear
if (fruittype1 == "Pear" || fruittype2 == "Pear" || fruittype3 == "Pear") {
// the Person has chosen a Pear
// Logger.log("DEBUG: there is a pear in this row");//DEBUG
// if the peartype has a value
if (peartype.toString().length > 0) {
// do nothing
// Logger.log("DEBUG: there is an entry in the Pear type, so do nothing\n");
} else {
// Logger.log("DEBUG: there is NO entry in the pear type, so something");// DEBUG
data[1][6] = "TBA";
}
} else {
// Logger.log("DEBUG: there was no pear chosen in this row - ignore Pear Type\n");//DEBUG
}
// test for Banana
if (fruittype1 == "Banana" || fruittype2 == "Banana" || fruittype3 == "Banana") {
// the Person has chosen a Banana
// Logger.log("DEBUG: there is a banana in this row");//DEBUG
// if the banana type has a value
if (bananatype.toString().length > 0) {
// do nothing
// Logger.log("DEBUG: there is an entry in the banana type, so do nothing\n");
} else {
// Logger.log("DEBUG: there is NO entry in the banana type, so something"); //DEBUG
data[2][6] = "TBA";
}
} else {
// Logger.log("DEBUG: there was no banana chosen in this row - ignore Banana Type\n");
}
// Test for Plum
if (fruittype1 == "Plum" || fruittype2 == "Plum" || fruittype3 == "Plum") {
// the Person has chosen a Plum
//Logger.log("DEBUG: there is a plum in this row");//DEBUG
// if the plum type has a value
if (plumtype.toString().length > 0) {
// do nothing
// Logger.log("DEBUG: there is an entry in the plum type, so do nothing\n");
} else {
// Logger.log("DEBUG: there is NO entry in the plum type, so something"); //DEBUG
data[3][6] = "TBA";
}
} else {
// Logger.log("DEBUG: there was no Plus chosen in this row - ignore Plum Type\n");
}
} else {
// Logger.log("this item must be blank");
}
}
// Update values
range.setValues(data);
}
ADDENDUM我误解了OP的目标。如果对于数组中的每个元素满足该语句,则OP希望整体返回“false”或返回“true”。以下修改纯粹是为了满足该要求。
function so5480624203() {
// setup spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get data range and values
var range = ss.getSheetByName('Sheet1').getDataRange();
//Logger.log("DEBUG: the range is "+range.getA1Notation());//DEBUG
var data = range.getValues();
// define variables and existing values for the fruit types
var appletype = data[0][6];
var peartype = data[1][6];
var bananatype = data[2][6];
var plumtype = data[3][6];
//Logger.log("DEBUG: apple type: "+appletype+"("+appletype.toString().length+"), pear type: "+peartype+"("+peartype.toString().length+"), banana type: "+bananatype+"("+bananatype.toString().length+"), plum type:"+plumtype+"("+plumtype.toString().length+")");//DEBUG
// define variables
var valid = true,
notBlank;
var mismatch = 0;
// slice the data from the eight row to thr end.
var newdata = data.slice(8);
//Logger.log("DEBUG: newdata length: "+newdata.length);//DEBUG
// Loop through the sliced rows
for (x = 0; x < newdata.length; x++) {
// check the length of the name; if name - 0, then row is blank
notBlank = newdata[x][1].toString().length;
// define the fruittypes for this person
var fruittype1 = newdata[x][2];
var fruittype2 = newdata[x][3];
var fruittype3 = newdata[x][4];
//Logger.log("DEBUG: Loop#:"+x+" "+newdata[x][1]+" Fruits: "+fruittype1+", "+fruittype2+", and "+fruittype3);//DEBUG
// if there is a name
if (notBlank) {
//if (mismatch !=0) { break;}
// test for "Apple"
if (fruittype1 == "Apple" || fruittype2 == "Apple" || fruittype3 == "Apple") {
// the Person has chosen an Apple
//Logger.log("DEBUG: there is an apple in this row"); //DEBUG
// if the appletype has a value
if (appletype.toString().length > 0) {
// do nothing
//Logger.log("DEBUG: there is an entry in the apple type, so do nothing");//DEBUG
} else {
//Logger.log("DEBUG: there is NO entry in the apple type, so something");//DEBUG
//data[0][6] = "TBA";
mismatch = 1;
//Logger.log("there is a mismatch");
break;
}
} else {
//Logger.log("DEBUG: there was no Apple chosen in this row - ignore Apple Type\n"); //DEBUG
}
// test for Pear
if (fruittype1 == "Pear" || fruittype2 == "Pear" || fruittype3 == "Pear") {
// the Person has chosen a Pear
//Logger.log("DEBUG: there is a pear in this row");//DEBUG
// if the peartype has a value
if (peartype.toString().length > 0) {
// do nothing
//Logger.log("DEBUG: there is an entry in the Pear Variety, so do nothing\n");
} else {
//Logger.log("DEBUG: there is NO entry in the Pear Variety, so something");// DEBUG
//data[1][6] = "TBA";
mismatch = 1;
//Logger.log("there is a mismatch");
break;
}
} else {
//Logger.log("DEBUG: there was no pear chosen in this row - ignore Pear Type\n");//DEBUG
}
// test for Banana
if (fruittype1 == "Banana" || fruittype2 == "Banana" || fruittype3 == "Banana") {
// the Person has chosen a Banana
//Logger.log("DEBUG: there is a banana in this row");//DEBUG
// if the banana type has a value
if (bananatype.toString().length > 0) {
// do nothing
//Logger.log("DEBUG: there is an entry in the banana type, so do nothing\n");
} else {
//Logger.log("DEBUG: there is NO entry in the banana type, so something"); //DEBUG
//data[2][6] = "TBA";
mismatch = 1;
//Logger.log("there is a mismatch");
break;
}
} else {
//Logger.log("DEBUG: there was no banana chosen in this row - ignore Banana Type\n");
}
// Test for Plum
if (fruittype1 == "Plum" || fruittype2 == "Plum" || fruittype3 == "Plum") {
// the Person has chosen a Plum
//Logger.log("DEBUG: there is a plum in this row");//DEBUG
// if the plum type has a value
if (plumtype.toString().length > 0) {
// do nothing
//Logger.log("DEBUG: there is an entry in the Plum variety, so do nothing\n");
} else {
//Logger.log("DEBUG: there is NO entry in the Plum variety, so something"); //DEBUG
//data[3][6] = "TBA";
mismatch = 1;
//Logger.log("there is a mismatch");
break;
}
} else {
//Logger.log("DEBUG: there was no Plum chosen in this row - ignore Plum Type\n");
}
} else {
//Logger.log("DEBUG: this item must be blank");//DEBUG
}
}
if (mismatch != 0) {
// there must be a mismatch somewhere
// return false
//Logger.log("DEBUG: There was a mis-match somewhere, so return false");//DEBUG
return false;
} else {
// there was no mismatch
// return true
//Logger.log("DEBUG: There was NO mis-match anywhere, so return true");//DEBUG
return true;
}
}