在Google表格中,我有第一个标签,其中A列中有数据条目。我想使用arrayformula为B列中的每个条目分配一个类别。应按以下方式确定类别:如果在第二个选项卡中某列的字符串之一与条目匹配,则应将该列的标题分配为该类别。
[我感觉应该有一种方法可以使用query
函数来执行此操作,但无法弄清楚。
This是示例工作表。如果解决方案需要,我很乐意稍微更改工作表的设置,但希望避免炸毁工作表。我也对最简单的解决方案感兴趣。
在这种情况下,我将使用自定义函数,以便您可以更好地控制工作表的逻辑和结构。首先,您应该更改将类别表移动到行结构中,因为Google表格会以这种方式获取值。
现在让我们构建一个从类别中提取数据并将每个名称分配给相应名称的函数。
这是一个自定义函数,因此我构建了适当的docs标头,以便在您的电子表格上下文中可见。
/**
* Assign the name the corresponding category.
*
* @param {input} the name to assign to the category
* @return The matching category
* @customfunction
*/
function MATCHCATEGORY(input) {
// if the input is from ARRAYFORMULA I will reiterate along the array with a map
if (input.map) {
return input.map(MATCHCATEGORY)
} else {
// Recursion base case
// I will get the categories rows
var rows = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CATEGORIES").getDataRange().getValues();
var category = "";
// Now I will map a string matching function along the rows
rows.map(row => {
for (var i=1; i<row.length; i++) {
if (input.includes(row[i])) {
// A match is found, the category string is populated.
category += row[0];
}
}
});
// Finally I return the category
return category
}
}
现在我们可以在ARRAYFORMULA中使用我们的自定义函数:
=ARRAYFORMULA(MATCHCATEGORY("ENTRY COLUMN RANGE"))