Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search spreadsheet column for text in a string and return a result in another column

Using google apps script and spreadsheet, I have been trying to do a simple thing but can't figure out the problem. I have a sheet, with a blank column and a column with text. The columns are not next to each other. I am trying to search for text in each cell in one column, and if the text is found, then set the value of the cell in the empty column as 'Yes'.

Example (sorry no code - I've gone round and round with it for hours, and what I have is so convoluted, it's best to just provide an example):

Column A with text  Empty Column F
abcd efg hij
klmn opq rstu  
vwxzy                  Yes

What is the simplest code to search Column A for 'xyz' and return a 'Yes' in Column F?

I've looked at and tried about a dozen different code examples online, and can't get any of them work. Appreciate any help with this!!

EDIT (Final hopefully) for my use (I have some backend utilities that get me the column number based on the header name, that code not included in this, fyi):

var sskey = SpreadsheetApp.openById('**********************')

function otherfunction(){
  addCustomValue('POCs', 'Groups', 'Champion', 'Champion', 'Yes');
}

function addCustomValue(sheetNamestr, searchColnamestr, writeColnamestr, searchKeystr, writeValstr) {
  var sheet = sskey.getSheetByName(sheetNamestr);
  var searchColnum = MyUtilities.getColIndexByName(sheet, 1, searchColnamestr);
  var writeColnum = MyUtilities.getColIndexByName(sheet, 1, writeColnamestr);
  var data = sheet.getDataRange().getValues();
  for (n=0; n<data.length; ++n) {
    if (data[n][searchColnum-1].toString().match(searchKeystr)==searchKeystr){ data[n][writeColnum-1] = writeValstr};
    }
  sheet.getRange(1,1,data.length,data[0].length).setValues(data);
}

Thanks Serge! Now I can run this over my spreadsheets based on any columns and conditions!

like image 801
user1783229 Avatar asked Nov 10 '12 23:11

user1783229


People also ask

How do I check if a value in one column exists in another column?

You can use the MATCH() function to check if the values in column A also exist in column B. MATCH() returns the position of a cell in a row or column. The syntax for MATCH() is =MATCH(lookup_value, lookup_array, [match_type]) . Using MATCH, you can look up a value both horizontally and vertically.

How do you search a column in Excel for a string?

On the Home tab, in the Editing group, click Find & Select, and then click Find. In the Find what box, enter the text—or numbers—that you need to find. Or, choose a recent search from the Find what drop-down box. Note: You can use wildcard characters in your search criteria.

How do I find cells containing specific text in Google Sheets?

One of the best ways to search your data to see if a cell contains a certain value is to use the REGEXMATCH function. This function will search a cell and return TRUE if a piece of text matches your regular expression, or FALSE if it does not.


1 Answers

This is a possible simple script that does what you need. (If your sheet contains formulas or custom function then it should be modified to take it into account)

function test(){
  var sh = SpreadsheetApp.getActiveSheet();
  var data = sh.getDataRange().getValues(); // read all data in the sheet
  for(n=0;n<data.length;++n){ // iterate row by row and examine data in column A
    if(data[n][0].toString().match('xyz')=='xyz'){
      // if column A contains 'xyz' then set value in index [5] (is column F)
      data[n][5] = 'YES'
    };
  }
  Logger.log(data)
  sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet
}
like image 73
Serge insas Avatar answered Oct 06 '22 23:10

Serge insas