自定义Google表格功能可将项目添加到以逗号分隔的列表中 [英] Custom Google Sheets function to add an item to a comma delimited list
问题描述
我在Google表格中使用逗号分隔列表,例如"apple,banana".我需要一个函数将元素添加到此列表中,例如=SETADD("apple,banana","carrot")
,该函数应返回值"apple,banana,carrot".
I have comma separated lists in a Google Sheet, like "apple,banana". I need a function to add elements to this list, like so =SETADD("apple,banana","carrot")
, which should return the value "apple,banana,carrot".
function SETADD(list, item) {
var array = list.split(',');
if (!array.includes(item)) {
return list + ',' + item;
}
else {
return list;
}
}
此函数返回错误TypeError: Cannot find function includes in object apple,banana. (line 3).
似乎该函数的第一个参数不是字符串?我在做什么错了?
This function returns with an error TypeError: Cannot find function includes in object apple,banana. (line 3).
It looks like the first argument to the function is not a string? What am I doing wrong?
推荐答案
很遗憾,在当前阶段,Array.includes()
尚不能在Google Apps脚本中使用.因此,作为一种变通办法,如何进行此修改?
Unfortunately, in the current stage, Array.includes()
cannot be used at Google Apps Script, yet. So as a workaround, how about this modification?
if (!array.includes(item)) {
收件人:
if (array.indexOf(item) === -1) {
参考文献:
- 基本的JavaScript功能
- includes()
- indexOf()
- Basic JavaScript features
- includes()
- indexOf()
References:
这篇关于自定义Google表格功能可将项目添加到以逗号分隔的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!