查找包含特定字符串的单元格的位置 [英] Find position of a cell containing a specific string

查看:96
本文介绍了查找包含特定字符串的单元格的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来搜索某个范围内的字符串,并找到找到的单元格的位置.
这是我用来查找行号的方法

I'm looking for a way to search for a string in a range and get the position of the cell once found.
Here's what I use to find the row number

var ss = SpreadsheetApp.getActiveSheet();
var values = ss.getRange("B:B").getValues();
var i=rownum=0;
for(i=0;i<values.length;i++) {
  if(values[i]=='string') rownum=i+1;  

然后我使用以下代码查找列号

I then use the following code to find the column number

var colvalue;
for (j=1;j<ss.getLastColumn();j++) {
if (ss.getRange(3,j).getValue() == "string" {
colvlaue = j;
  }
}

是否可以在包含特定字符串的范围内搜索整个行和列,并在找到后返回其单元格位置?

Is there a way to search both entire rows and columns within a range that contains a specific string and return its cell position once it finds it?

推荐答案

您需要使用嵌套的 for 循环.这段代码可以做到:

You need to use a nested for loop. This code will do it:

function findValueInRange (po) {
  /*
    po.whatToFind - the string value to find

  */

  var colvalue,i,j,L,L2,ss,theLastColumn,theLastRow,thisRow,values;

  ss = SpreadsheetApp.getActiveSheet();
  theLastColumn = ss.getLastColumn();
  theLastRow = ss.getMaxRows();
  
  //Returns a two dimensional array of both rows and columns
  values = ss.getRange(1,1,theLastRow,theLastColumn).getValues();
       
  
  whatToFind = po.whatToFind;
  
  L = values.length;

  for(i=0;i<L;i++) {
    thisRow = values[i];
    L2 = thisRow.length;

    for (j=0;j<L2;j++) {
      colvalue = thisRow[j];

      if (colvalue === whatToFind) {
        Logger.log("The string is in row: " + (i + 1) + " and column: " + (j+1));
      };
    };
  };
};

这篇关于查找包含特定字符串的单元格的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆