好的,快速查看一下,您似乎想要检查一系列单元格中的值,将它们与正则表达式进行比较,并根据结果更改单元格颜色。请记住,我以前没有使用过 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
}
}
}