自定义编码字符串

时间:2015-08-03 11:43:51

标签: javascript google-apps-script google-sheets

如何将字符串中的每个字符转换为编码数字。

我的编码密钥将是。

a = 01
b = 02
c = 03
d = 04
.
.
z = 26

所以我想进入" dog"在我的单元格中,它应该转换为" 04 15 07"。

将在Google电子表格中使用,因此JavaScript函数会有所帮助。

2 个答案:

答案 0 :(得分:2)

根据您的评论,我创建了以下Javascript代码:

var letters =  ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

function giveNumbercode(someString){
    var numbercode="";
    for(var i = 0; i < someString.length; i++){
        var letter = someString[i];
        var number = letters.indexOf(letter) + 1;
        if(number < 10){
            number = "0"+number
        }
        numbercode += " "+number
    }
    alert("Your Numbercode: "+numbercode+"");
    return numbercode;
}

所有你需要做的就是调用函数,它会给你一个例子的数字代码:

giveNumbercode(“dog”)将返回“您的号码:04 15 07”

答案 1 :(得分:0)

选择您要处理的单元格并运行此短宏:

Sub EnCoder()
    Dim r As Range
    For Each r In Selection
        v = r.Text
        For i = 97 To 122
            v = Replace(v, Chr(i), Format(i - 96, " 00"))
        Next i
        r.Value = v
    Next r
End Sub

在:

enter image description here

之后:

enter image description here