简体   繁体   English

根据值将单元格拆分为列

[英]Split cell to columns depending on the value

So i have this data in column A on my spreadsheet所以我在电子表格的 A 列中有这些数据

test1,test2,test3

It can be more than 3 values or less than 3 values and i want to split it into different columns with the comma as a delimiter.它可以是超过 3 个值或少于 3 个值,我想用逗号作为分隔符将它分成不同的列。 But i want to place it on a specific column depending on the value.但我想根据值将它放在特定的列上。 For example if it is test1 or test2 i want it to to column B. If it is test3 or test4 i want it to go to column C.例如,如果它是 test1 或 test2,我希望它到 B 列。如果它是 test3 或 test4,我希望它到 go 到 C 列。

Any ideas on how i can achieve that on google apps script?关于如何在谷歌应用脚本上实现这一目标的任何想法?

Thanks very much非常感谢

I think this is what you are looking for:我认为这就是你要找的:

 var str = 'test1,test2,test3,test4'; var colA = str.split(','); var colB = []; var colC = []; for (var i = 0; i < colA.length; i++){ var val = colA[i]; if (val == 'test1' || val == 'test2'){ colB.push(val); }else { colC.push(val) } } console.log(colB); console.log(colC);

Option 2 would be to use functions like this:选项 2 将使用如下函数:

 var str = 'test1,test2,test3,test4'; var colA = str.split(','); var colB = []; var colC = []; function move_to_b(value){ for (var i = 0; i < colA.length; i++){ var val = colA[i]; if (val == value){ colB.push(val); } } } function move_to_c(value){ for (var i = 0; i < colA.length; i++){ var val = colA[i]; if (val == value){ colC.push(val); } } } move_to_b('test1'); move_to_b('test2'); move_to_c('test3'); move_to_c('test4'); console.log(colB); console.log(colC);

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

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