如何查询谷歌工作表列的特定值

时间:2016-06-03 12:02:36

标签: javascript google-apps-script google-sheets google-spreadsheet-api

我们说我的价值是10,而且我得到了一张包含许多值的第一列ID的Google工作表。我想运行一个遍历该列中所有值的脚本,一旦找到匹配项,就会在该行上设置活动选择

1 个答案:

答案 0 :(得分:1)

以下脚本在活动工作表的第一列中搜索值10,并在找到该值时设置选择。

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');
  }
}

在应用indexOf之前,数组idValues被展平,因为getValues将列作为双数组[[1], [2], [3]]返回。