我尝试使用indexOf(),findText()和其他一些方法在google app脚本中查找文本中的字符串模式。上述方法均无效。
var str="task is completed";
我从谷歌电子表格中获取此字符串。
我只想查找上面的字符串是否包含字符串“task”。
答案 0 :(得分:9)
您需要检查Dim CopyFrom As Range
Application.ScreenUpdating = False
Set DMA_wb = Workbooks("DMA_metered_tool_v12_SICTEST.xlsm")
Set DMA_sht = DMA_wb.Worksheets("DMA list")
FPath = DMA_sht.Range("c8").Text
FName = ("DMA_customers_SICTEST.xlsx")
Workbooks.Add.SaveAs FileName:=FPath & "\" & FName
Set cust_DMA = Workbooks("DMA_customers_SICTEST.xlsx")
Set Billed_sheet = Workbooks("Billed_customers_SICTEST.xls").Sheets("Non Household Metered Users")
With Billed_sheet
.AutoFilterMode = False ' clear any existing filter to get accurate row count
Lrow = .Range("a" & .Rows.count).End(xlUp).row
Set filter_range = .Range("a1:v" & Lrow) '''try changing to a:v to avoid missing anything
End With
With DMA_sht
List_row = .Range("a" & .Rows.count).End(xlUp).row
For i = 2 To List_row '- c1 removed '-1 as it was missing the last value, starting at 2 already accounts for list_row having more items in it than needed.
filter_val = .Cells(i, 1).Value
filter_range.AutoFilter Field:=8, Criteria1:=filter_val ''''autofilter field should be 8 as h is column 8
cust_DMA.Sheets.Add.Name = filter_val
Set CopyFrom = Billed_sheet.Range("a1:v" & Lrow).SpecialCells(xlCellTypeVisible) ' set range as filtered values only
CopyFrom.copy 'copy filtered values
.AutoFilterMode = False 'remove filters
cust_DMA.Sheets(filter_val).Range("a1").PasteSpecial xlPasteValues
Next i
Application.ScreenUpdating = True
End With
是否存在:
str
或者,您可以使用if (str) {
if (str.indexOf('task') > -1) {
// Present
}
}
和test
:
regex
/task/.test("task is completed");
/task/.test(str);
:正则表达式匹配'任务' /task/
:针对regex测试字符串并返回boolean 答案 1 :(得分:4)
一个简单的str.indexOf("test")>=0
就可以了。有用。不知道为什么你说它不起作用,因为你没有显示任何代码来指出问题
如果你想检查无论使用str.toLowerCase().indexOf("test")