how to query a google sheet column for a certain value

 ̄綄美尐妖づ 提交于 2020-01-17 01:22:10

问题


Let's say i have value 10, and I got a google sheet with first column id that has many values. I would like to run a script that goes through all the values in that column, and once it finds a match, it sets the active selection on that row


回答1:


The following script searches the first column of the active sheet for the value of 10, and sets the selection on that value if found.

function setActive() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var idValues = sheet.getRange(1, 1, sheet.getLastRow(), 1).getValues().map(function(a) {
    return a[0];
  });
  var row = idValues.indexOf(10) + 1;
  if (row > 0) {
    SpreadsheetApp.setActiveRange(sheet.getRange(row, 1));
  }
  else {
    throw new Error('Id not found');
  }
}

The array idValues is being flattened before applying indexOf, since a column is returned by getValues as a double array [[1], [2], [3]].



来源:https://stackoverflow.com/questions/37613891/how-to-query-a-google-sheet-column-for-a-certain-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!