简体   繁体   English

Google Spreadsheet Script RegEx on Cells of Cells

[英]Google Spreadsheet Script RegEx on Range of Cells

Background (Optional) : I wrote a working script in VBA that I'm trying to write in JS for Google Spreadsheets, but I'm having a tremendous amount of difficulty with the regex validation on a per cell basis. 背景(可选) :我在VBA中编写了一个工作脚本,我正在尝试用JS编写Google Spreadsheets,但是我在每个单元格的基础上进行正则表达式验证时遇到了很大的困难。

In this VBA code snippet, I set a range of data, and test each cell against a RegEx that is previously defined. 在此VBA代码段中,我设置了一系列数据,并针对先前定义的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 would allow me to look through the same range and perform the same operation. 我很好奇的是,实际的JavaScript函数可以让我查看相同的范围并执行相同的操作。 Here's 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
}

Here 2 examples how to work with RegEx in JS, the WScript.Echo is JScript for easier testing, replace with document.write or response.write or whatever 这里有两个如何在JS中使用RegEx的例子,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

Okay, quickly looking this over, it looks like you want to examine the values in a range of cells, compare them to regular expressions and change the cell colors based on the results. 好的,快速查看一下,看起来你想检查一系列单元格中的值,将它们与正则表达式进行比较,并根据结果更改单元格颜色。 Keeping in mind that I haven't used the google docs api before, it looks like there are just a couple of minor issues with your existing code. 请记住,之前我没有使用过google docs api,看起来你的现有代码只有一些小问题。

Firstly, you need to create your regular expressions correctly. 首先,您需要正确创建正则表达式。

Your line: 你的路线:

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)$");

Becomes: 变为:

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)$/];

The two changes there are dropping new Array in favor of just using the literal array syntax of [ ] (optional but considered good practice), and replacing the quotes in your array strings with forward slashes (the regular expression literal syntax). 这两个更改有new Array ,只使用[ ]的文字数组语法(可选但考虑好的做法),并用正斜杠(正则表达式文字语法)替换数组字符串中的引号。 The result is an array of regular expression objects instead of strings. 结果是一个正则表达式对象数组而不是字符串。 That will allow you to actually do something like re.test("some string"); 这将允许你实际做一些像re.test("some string"); like you've done in your evaluation code. 就像你在评估代码中所做的那样。

Secondly, you need to loop over the range of cells, get their values and do the comparison. 其次,您需要循环遍历单元格范围,获取其值并进行比较。 A quick examination of the documentation tells me that sheet.getRange returns a Range object. 快速检查文档告诉我sheet.getRange返回一个Range对象。 The range object has a getValues method that returns an array of arrays of cell values (array[][]). range对象有一个getValues方法,它返回一个单元格数组数组(array [] [])。

So some quickly approximate code for what you want is: 所以有些人会快速拟定您想要的代码:

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
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM