Home > database >  How to get the first 3 digits from a cell
How to get the first 3 digits from a cell

Time:02-04

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. 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"

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

enter image description here

I would really appreciate an answer

If you need more info please let me know

Thank you.

CodePudding user response:

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)
    }

CodePudding user response:

const cell = "(303) 987-4567";
const first3 = cell.match(/\d{3}/)[0];
//result:303

String method match()

regular expression

BTW: you can test methods like this very easily in the console.log in the browsers developer tools.

  •  Tags:  
  • Related