当前位置:首页 > 未分类 > 正文内容

Find Matching Rows in Google Spreadsheets

ceacer3周前 (05-02)未分类2841

You have a Google Spreadsheet and you need to programmatically find if a cell with particular value exists in that sheet. Well, Google Scripts can help. You can either search cells in a single column (like A) or the script can search for all cells in the row and return the index of the matching row.

function findInColumn(column, data) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var column = sheet.getRange(column + ':' + column); // like A:A

  var values = column.getValues();
  var row = 0;

  while (values[row] && values[row][0] !== data) {
    row++;
  }

  if (values[row][0] === data) return row + 1;
  else return -1;
}

function findInRow(data) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var rows = sheet.getDataRange.getValues();

  for (var r = 0; r < rows.length; r++) {
    if (rows[r].join('#').indexOf(data) !== -1) {
      return r + 1;
    }
  }

  return -1;
}

相关文章

Send Personalized Emails with SMTP and Mail Merge in Google Sheets

Send Personalized Emails with SMTP and Mail Merge in Google Sheets

The Mail merge add-on lets you send personalized emails to multiple recipients in one go. The emails...

How to Email Screenshots of Google Sheets on a Schedule

How to Email Screenshots of Google Sheets on a Schedule

The finance team has created a revenue dashboard inside Google Sheets to track the company’s sales p...

How to Use Conditional Formatting in Google Sheets to Highlight Information

How to Use Conditional Formatting in Google Sheets to Highlight Information

Conditional formatting in Google Sheets makes it easy for you to highlight specific cells that meet...

How to Insert Images in Google Sheet Cells

How to Insert Images in Google Sheet Cells

This tutorial explores the different options for inserting images in Google Sheets. We’ll also discu...

How to Use Hyperlinks in Google Sheets

How to Use Hyperlinks in Google Sheets

This guide explains how you can easily create and manage hyperlinks in Google Sheets. An entire cell...

How to Create Dynamic Open Graph Images with Google Sheets

How to Create Dynamic Open Graph Images with Google Sheets

An open graph image (OG image) is the image that is displayed when any of your website links are sha...