简体   繁体   中英

How transcribe remove number custom function Excel VBA function to Google Sheets Google Script (Javascript)

I need to transcribe a VBA script to Javascript but i got stuck in the java part

The main meaning is to remove numbers from a text like "texthere 123456789"

My VBA code is:

Function RemoveNum(Txt As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "[0-9]"
RemoveNum = .Replace(Txt, "")
End With
End Function

My Javascript attempt was:

function RemoveNumbers(RemoveNumbers) {
  var RemoveNumbers;
  //var str = RemoveNumbers.toString();
  var str = RemoveNumbers;
  str.Value.replace(/[0-9]/g, '');
}

Or even:

function rn(remvnum) {
var str = remvnum;
var n = str.toString();
var res = n.replace(/[0-9]/gmi, '');

}

What does stop me reaching the result is, the .Replace function needs to be string content otherwise will return error of undefined value, also I can't convert toString because it returns error of undefined value.

This example bellow works well as the name of the function is written in the Google Sheet cell as a custom function, but I didn't achieve the remove number desire:

function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match, offset, string) {
    return (offset ? '-' : '') + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}

Does someone knows what I did wrong?

Thanks in advance.

Try removeNumbers = removeNumbers.replace(/[0-9]/g, ''); or set a new var like noNums = removeNumbers.replace(/[0-9]/g, '');

You just need to return the replaced value:

function rn(remvnum) {
 //var str = remvnum;
 //var n = str.toString();
 var res = remvnum.toString().replace(/[0-9]/gmi, '');
 return res; //Added
}

Google sheets supports regex( ) as a built-in function unlike excel. So, You don't need macros. Even if you want a global replace, The Find and replace menu works well with regex.

=REGEXREPLACE(A2,"\d",)

or

=ARRAYFORMULA(REGEXREPLACE(A2:A5,"\d",))
  • \\d is d igit(== [0-9] )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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