Google Script RegEx spreadsheet by cell range

Background (optional) . I wrote a working script file in VBA that I am trying to write to JS for Google spreadsheets, but I had huge difficulties with regular expression validation based on each cell.

In this VBA code snippet, I set the data range and check each cell for a previously installed RegEx.

Set rRange = Range(arrLetters1(i) & intRange1, arrLetters2(i) & intRange2)

For Each rCell In rRange.Cells
  If re.Test(rCell) Then
    rCell.Interior.Color = RGB(0, 250, 0)
  Else
    Cells((intRange1 - 1), rCell.Column).Interior.Color = RGB(250, 0, 0)
    rCell.Interior.Color = RGB(250, 0, 0)
  End If
Next rCell

What I'm curious about is the actual JavaScript function that will allow me to view the same range and perform the same operation. Here is what I have:

var re = "[a-z]+"
var rRange = sheet.getRange(arrLetters1(i) + intRange1, arrLetters2(i) + intRange2)

for (var rCell in rRange) {
  if (rCell //is a "re" match) {
      //do some code
  }
} else {
     //do something else
}
+3
source share
2 answers

2 , RegEx JS, WScript.Echo - JScript , document.write response.write -

var rRange = "this string is for testpurposes"
var re = /[a-z]+/
var regExp = new RegExp(re);
if (rRange.match(regExp)) {
  WScript.echo("Successful match");
} else {
  WScript.echo("No match");
}

=>Successful match

var str="The rain in SPAIN stays mainly in the plain"; 
var n=str.match(/ain/g);
WScript.echo(n); 
=> ain,ain,ain
+2

, , , , . , google docs api, , .

-, .

:

var arrPatterns = new Array("^([A-Z]{2}(-[0-9]{5}){4})$", "^[A-Z]{2}$", "^[0-9]{5}$", "^[a-z]{1,}$", "^\(\d\d\d\) \d\d\d-\d\d\d\d$", "^([a-z0-9]{1,}[,]{0,}){1,}$", "^(\d\d?:\d\d[aApP][mM] - \d\d?:\d\d[aApP][mM])|(Closed)|(All Day)$");

:

var arrPatterns = [/^([A-Z]{2}(-[0-9]{5}){4})$/, /^[A-Z]{2}$/, ... /^(\d\d?:\d\d[aApP][mM] - \d\d?:\d\d[aApP][mM])|(Closed)|(All Day)$/];

new Array [ ] (, ) ( ). . - re.test("some string");, .

-, , . , sheet.getRange Range. range getValues, (array [] []).

, , :

var rRange = sheet.getRange(arrLetters1(i) + intRange1, arrLetters2(i) + intRange2);
var values = rRange.getValues();
var re = arrPatterns[0];

var row, col;
for (row = 0; rows < values.length; row++) {
  for (col = 0; col < values[row].length; col++) {
    if (re.test(values[row][col])) {
        /*do passes regex, looks like you probably need to pass the row/col to getCell
          to get a range with the desired cell in it and then call setBackgroundColor on
          that range.  I'm also leaving looping through your array of regular expressions to you,
          as it looked like you have a dependency on the expression being used and the array of
          letters i didn't take the time to understand.*/
    }
    else {
      //do fails regex
    }
  }
}
+2

All Articles