简体   繁体   English

单个下拉菜单中的多项选择

[英]Multiple selection in single Dropdown

I found this code on reddit that works quite well.我在 reddit 上发现这段代码运行良好。 Anyway, when I try to delete the information that I completed with the dropdown, or modify something, an undefined value shows up and I can not delete it.无论如何,当我尝试删除通过下拉列表完成的信息或修改某些内容时,会出现一个未定义的值,我无法删除它。 How could I do that?我怎么能那样做?

Code:代码:

function onEdit(e){
  if(e.range.columnStart != 6 || e.oldValue == undefined) return;
  e.range.setValue(e.oldValue+" | "+e.value);
}

截屏:

Besides, when more than 1 option is added on my cell, an 'invalid' message shows up.此外,当我的单元格上添加了多个选项时,会显示“无效”消息。 How could I delete it?我怎么能删除它? It's a default config from the Data Validation.这是来自数据验证的默认配置。 无效消息

So far your code snippet works on mine properly.到目前为止,您的代码片段在我的身上正常工作。 (I can't seem to replicate your issue) (我似乎无法复制您的问题)

But for you, it isn't (that I am not sure).但对你来说,它不是(我不确定)。 Try to include the condition where e.value is null (that happens when you delete the cell value).尝试包含e.value为 null 的条件(删除单元格值时会发生这种情况)。 Your code should look like this.您的代码应如下所示。

Code:代码:

function onEdit(e){
  if(e.range.columnStart != 6 || e.oldValue == null || e.value == null) return;
  e.range.setValue(e.oldValue+" | "+e.value);
}

Note:笔记:

  • Try to log the values before comparing them in a condition.在比较条件之前尝试记录这些值。 It's true that in a == sense, null and undefined are the same as both of them return FALSE , but they have different types.确实,在==意义上, nullundefined与它们都返回FALSE相同,但它们具有不同的类型。 If you have used === , then they are not equal.如果您使用过=== ,则它们不相等。

  • If the cell is deleted (as you can see on the image below), both oldValue and value becomes null .如果单元格被删除(如下图所示), oldValuevalue都变为null

Cloud logs (blank -> yes -> no -> delete cell):云日志(空白 -> 是 -> 否 -> 删除单元格):

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM