我编写了下面的代码来搜索电子表格中的值。出于某种原因,当我尝试垂直搜索时,它会改为水平搜索。
我认为将 valores[cc][0]
更改为 valores[0][cc]
会做到这一点,但它不起作用。
知道我做错了什么吗?
function onEdit(e){
var a = e.source.getActiveSheet();
var SearchText = "4"
//x = mainSearch( a, 3, 1, "horizontal", SearchText);
x = mainSearch( a, 1, 1, "vertical", SearchText);
}
//mainSearch( targetSheet, row, column, alignment, searchText)
function mainSearch( folha, linha, coluna, procTipo, procTexto) {
if ( procTipo = "horizontal" ) {
var alcance = folha.getRange( linha, coluna, folha.getLastRow(), 1);
}
else if ( procTipo = "vertical" ) {
var alcance = folha.getRange( linha, coluna, 1, folha.getLastColumn());
}
else {
Browser.msgBox("mainSerch com procTipo errado");
}
var valores = alcance.getValues();
for(cc=0;cc<valores.length;++cc) {
if ( procTipo = "horizontal" ) {
Browser.msgBox("Horizontal --> " + valores[cc][0]);
if ( valores[cc][0] == procTexto ) {
return (cc + linha);
}
}
else if ( procTipo = "vertical" ) {
Browser.msgBox("Vertical --> " + valores[0][cc]);
if ( valores[0][cc] == procTexto ) {
return (cc + coluna);
}
}
}
return 0;
}
最佳答案
问题出在这里:
if ( procTipo = "horizontal" ) {
当您执行 procTipo = "horizontal"
时,您正在将 "horizontal"
分配给 procTipo
。您应该只测试它的值:
if ( procTipo == "horizontal" ) {
还有其他三个地方,您必须将 =
更改为 ==
。
有些人更喜欢使用 ===
因为 it doesn't do any type coercion , 但在这种情况下 ==
将同样有效。
您必须调整迭代限制,以便在垂直情况下正确搜索 valores
。目前你有这个:
for(cc=0;cc<valores.length;++cc) {
将其替换为这两行:
var limit = (procTipo == 'horizontal' ? valores.length : valores[0].length);
for (var cc = 0; cc < limit; ++cc) {
关于javascript - 垂直或水平文本的搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32425618/