简体   繁体   English

如何从单元格中获取前 3 位数字

[英]How to get the first 3 digits from a cell

So I have got a column and i want to get the first 3 digits only from it and store them in a function called wnS using the split function or any other method that would work.所以我有一个列,我想只从中获取前 3 位数字,并将它们存储在名为 wnS 的 function 或任何其他可行的方法中。 I want to get the first three digits before "_"我想得到“_”之前的前三位

I tried doing this but it didn't work, and I also kept getting "TypeError: wnC.split is not a function"我试过这样做,但没有用,而且我还不断收到“TypeError:wnC.split 不是函数”

    var ssh = ssPO.getSheetByName("PO for OR (East).csv")
    wnC = ssh.getRange("N2:N");
    var wnS = wnC.split("_");

在此处输入图像描述

I would really appreciate an answer我真的很感激一个答案

If you need more info please let me know如果您需要更多信息,请告诉我

Thank you.谢谢你。

After you define range, you have to get the values.定义范围后,您必须获取值。

function first_3_digs (){
    
        var ssh = ssPO.getSheetByName("PO for OR (East).csv")
        var wnC = ssh.getRange("N2:N");
        var values = wnC.getValues();
        const first_3_digs = values.filter(r => {
            if(r.toString().includes('_')){return r;}
        }).map(r=> r.toString().split('_')[0]);
        console.log(first_3_digs)
    }
const cell = "(303) 987-4567";
const first3 = cell.match(/\d{3}/)[0];
//result:303

String method match() 字符串方法 match()

regular expression正则表达式

BTW: you can test methods like this very easily in the console.log in the browsers developer tools.顺便说一句:您可以在浏览器开发人员工具的 console.log 中非常轻松地测试这样的方法。

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

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