【问题标题】:Parse a string of mixed numbers and text [duplicate]解析一串混合数字和文本[重复]
【发布时间】:2019-12-11 22:07:39
【问题描述】:

我在谷歌表格中有一个单元格,上面写着“24 盎司”之类的食谱项目。有没有办法将 24 解析为脚本中的变量?

我试图在一个简单的数学公式中使用这个变量。它是动态的,这就是为什么我需要为它编写代码。

【问题讨论】:

    标签: javascript google-sheets


    【解决方案1】:
    var your_input = "24 oz"; 
    //find spaces and convert the input into an array
    // each time split(" ") find a space , it add an element to the array
    var array = your_input.split(" ");
    // now array contain two elements
    // array[0]="24" and array[1]="oz"
    //now we parse "24" into a Long so we can use it in math operations
    var result = parseInt(array[0]);
    

    【讨论】:

    • 请在您的代码中添加一些解释,以便其他人可以从中学习
    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 2011-09-17
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多