[英]Custom function in google sheet using indexOf
I've got a list of emails that I need to clean and identify which of these emails are company emails (ie info@, hello@, etc)我有一个需要清理的电子邮件列表,并确定其中哪些是公司电子邮件(即 info@、hello@ 等)
I've had an idea to add rows in one google sheet, then check this against another google sheet with a column of company alias'.我有一个想法在一个谷歌工作表中添加行,然后将其与另一个具有公司别名列的谷歌工作表进行检查。 This will then return
True
of False
in the column Company Alias?
然后,这将在
Company Alias?
列中返回True
of False
? in the Input sheet.在输入表中。
Here is my Google sheet example.这是我的Google 表格示例。
I think it needs to iterate through the values of the second sheet and compare for email 1
field in the first sheet to see if it contains that value.我认为它需要遍历第二张表的值并比较第一张表中的
email 1
字段以查看它是否包含该值。 I have made an apps script below:我在下面制作了一个应用程序脚本:
function CHECKALIAS(x) {
var app = SpreadsheetApp;
var aliasSheet = app.getActive().getSheetByName('Company_Alias').getRange(2, 1, 45, 1);
var aliasRange = aliasSheet.getValues();
var str = x;
if(str.indexOf(aliasRange) !== -1){
return false;
} else {
return true;
}
}
I get the below error:我收到以下错误:
TypeError: Cannot read property 'indexOf' of undefined (line 13, file "Code")
Are you running the function without passing it a value?您是否在不传递值的情况下运行 function? It's giving you the error because
str
has no value.它给你错误,因为
str
没有价值。
Anyway, there's no need for a custom function here.无论如何,这里不需要自定义 function。 This will return true if there's a match with one of the aliases.
如果与其中一个别名匹配,这将返回 true。
=REGEXMATCH(A1, JOIN("|",FILTER(Company_Alias!A2:A, NOT(ISBLANK(Company_Alias!A2:A)))))
str
is a string and aliasRange
is an array. str
是一个字符串,而aliasRange
是一个数组。 Instead of searching for the array in the string, you should search for the string in the array.您应该在数组中搜索字符串,而不是在字符串中搜索数组。
Another problem is that .getValues()
returns an array of arrays:另一个问题是
.getValues()
返回一个 arrays 数组:
[["info @"], ["support @"], ["sales @"], ...].
To transform it into a 1D array, use the flat()
function:要将其转换为一维数组,请使用
flat()
function:
function CHECKALIAS(x) {
let app = SpreadsheetApp;
let aliasSheet = app.getActive().getSheetByName('Company_Alias').getRange(2, 1, 45, 1);
let aliasRange = aliasSheet.getValues().flat(); // Transforms 2D array in 1D
let userDomain = x.split('@'); // split "admin@somedomain.com" into [ "admin", "somedomain.com" ]
let usr = userDomain[0] + '@'; // "admin@
return aliasRange.indexOf(usr) !== -1; // Is usr in aliasRange?
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.