所以我在为Google表格做一个小的搜索引擎,但我不希望它区分大小写,所以我让它声明“ RealComments”为要检索的实际内容,然后继续小写“ Comments”在主循环中逐行排列数组。
在我看来,它应该工作并不费劲,它们是不同的变量,我将返回RealComments,而不是Comment。但是我的代码中发生了某种奇怪的量子纠缠,因为无论我如何声明,每次我对RealComments或Comments数组应用toLowecase()都会被修改。
我不知道为什么这样做或如何解决。这是完整的代码:
/**
* Searches for relevant questions and answers
*
* @param {cell} Keywords Cell with comma separated list
* @param {cell} Query Cell with text in question format
* @param {cell} Type Validation cell with options, leave "Any" for no filter
* @param {column} TypeOfComment Array with the type of question column
* @param {array} Comments Array with the questions and answers in two columns
* @param {column} ScreenshotLinks Array with the screenshot link column
* @return Your answer. :)
* @customfunction
*/
function SearchQuery(Keywords,Query,Type,TypeOfComment,Comments,ScreenshotLinks) {
if(Keywords==""){
return "Please type your query/keywords above for results to be displayed here."
}
var ResultArray = [];
var Score = 0;
var MinimumScore = 2;
var MinimumScorePosition = 0;
var keywords = [];
var querywords = [];
var RealComments = Comments;
for(rows in RealComments){
RealComments[rows].push(ScreenshotLinks[rows][0]);
}
var RemaneingKeywords = CleanPunctuation(Keywords);
var RemaneingQuery = CleanPunctuation(Query);
var NextSpace = RemaneingKeywords.indexOf(' ');
while(NextSpace != -1){
keywords.push(RemaneingKeywords.substr(0,NextSpace));
RemaneingKeywords = RemaneingKeywords.substr(NextSpace+1,RemaneingKeywords.length);
NextSpace = RemaneingKeywords.indexOf(' ');
}
keywords.push(RemaneingKeywords);
NextSpace = RemaneingQuery.indexOf(' ');
while(NextSpace != -1){
querywords.push(RemaneingQuery.substr(0,NextSpace));
RemaneingQuery = RemaneingQuery.substr(NextSpace+1,RemaneingQuery.length);
NextSpace = RemaneingQuery.indexOf(' ');
}
querywords.push(RemaneingQuery);
for(rows in Comments){
Comments[rows][0] = Comments[rows][0].toLowerCase(); //Investigate lowercase glitch
Comments[rows][1] = Comments[rows][1].toLowerCase();
if(Type=="Any"||TypeOfComment[rows]==Type){
Score = 0;
for(keyword in keywords){
if(Comments[rows][0].indexOf(keywords[keyword])!=-1||Comments[rows][1].indexOf(keywords[keyword])!=-1){
Score = Score+10;
}
}
for(words in querywords){
if(Comments[rows][0].indexOf(querywords[words])!=-1||Comments[rows][1].indexOf(querywords[words])!=-1){
Score = Score+1;
}
}
if(ResultArray.length<20 && Score>2){
ResultArray.push(RealComments[rows]);
ResultArray.push(Score);
MinimumScore = FindMinimumScore(ResultArray);//Math.min.apply(null,ResultArray);
}
else{if(Score>MinimumScore){
MinimumScorePosition = ResultArray.indexOf(MinimumScore);
ResultArray.splice(MinimumScorePosition-1,2);
ResultArray.push(RealComments[rows]);
ResultArray.push(Score);
MinimumScore = FindMinimumScore(ResultArray);
}}
}
}
var ScoresForSorting = []; //Sorting the responses here
for(i=0;i<10;i++){
if(ResultArray[i+1]==undefined){ScoresForSorting.push(-2);}
else{ScoresForSorting.push(parseInt(ResultArray.splice(i+1,1)));}
}
var ResponseOrder = [];
for(i=0;i<10;i++){
MinimumScorePosition = ScoresForSorting.indexOf(Math.max(ScoresForSorting[0],ScoresForSorting[1],ScoresForSorting[2],ScoresForSorting[3],ScoresForSorting[4],ScoresForSorting[5],ScoresForSorting[6],ScoresForSorting[7],ScoresForSorting[8],ScoresForSorting[9]));
ResponseOrder.push(MinimumScorePosition);
ScoresForSorting[MinimumScorePosition]=-3;
}
var SortedResultArray = [];
for(results in ResponseOrder){
SortedResultArray.push(ResultArray[ResponseOrder[results]]);
}
if(SortedResultArray[0]==undefined){SortedResultArray = []; SortedResultArray.push("Sorry, No Result Was Found To Your Search. ) : ");}
return SortedResultArray
}
function FindMinimumScore(ResultArray){
return Math.min(ResultArray[1],ResultArray[3],ResultArray[5],ResultArray[7],ResultArray[9],ResultArray[11],ResultArray[13],ResultArray[15],ResultArray[17],ResultArray[19])
}
function CleanTwoLetterWords(words){
var badwords = ["do","in","at","it","of","as","be","if","or","we","by","an","or","no","my","vs"];
for(badword in badwords){words = words.replace(badwords[badword],"");}
return words
}
function CleanThreeLetterWords(words){
if(parseInt(words)>9){return ""} //remove numbers
var badwords = ["and","for"];
for(badword in badwords){words = words.replace(badwords[badword],"");}
return words
}
function CleanPunctuation(words){
words = words.toLowerCase();
var badlettergroup = ["{","}",",",":","+","-","™","®","?","!","(",")","'"];
for(letter in badlettergroup){while(words.indexOf(badlettergroup[letter]) != -1){words = words.replace(badlettergroup[letter]," ");}}
return words
}
function MakeThingsSingular(words){
var exceptions = ["this"];
if(exceptions.indexOf(words) != -1){return words}
var wordl = words.length;
if(words.substr(wordl-1,wordl) == "s" && words.substr(wordl-2,wordl) != "ss"){return words.substr(0,wordl-1)}
return words
}
我全部都是自己写的,但是我不认为这是特别出色的,所以可以随意复制。另外,如果有任何改进建议。但是最重要的是,如果您知道为什么将注释数组小写的行会影响RealComments和/或如何修复它,请告诉我。
编辑:
因此,经过更多研究和有用的回答之后,我尝试将RealComments声明为:
var RealComments = [];
for(rows in Comments){
RealComments.push(Comments[rows]);
}
在这种情况下,故障仍然存在。
var RealComments = [...Comments];
哪个返回语法错误。
var RealComments = Comments.slice();
在这种情况下,故障仍然存在。 ...
因此,基本上,我仍然不了解代码的问题,注释掉任何小写的行都会影响您希望在RealComments上看到的列,就像它们仍然被链接一样。
Comments[rows][0] = Comments[rows][0].toLowerCase();
Comments[rows][1] = Comments[rows][1].toLowerCase();
所以我想这篇帖子如果还没有解决...但是,谢谢您到目前为止的所有答复。
答案 0 :(得分:0)
在javascript中,数组分配创建引用而不是副本。
如果愿意
var a = [1,2,3,4];
var b = a;
b
将是a
的参考。这意味着,如果您修改b
,实际上是要修改a
。
快速解决方案
在你做的那一行
var RealComments = Comments;
请改为var RealComments = Comments.slice();
只要Comments
是数组slice
,方法将返回Comments
数组的新实例。因此,修改RealComments
将不再修改Comments
。
答案 1 :(得分:0)
TheMaster指出“ slice()将不起作用,因为Comment不是数组,而是数组的数组。”之后,为我完成的解决方案仅使用:
var newArray = JSON.parse(JSON.stringify(orgArray));
在我的情况下是:
var RealComments = JSON.parse(JSON.stringify(Comments));
感谢您的帮助!希望对其他用户有所帮助。