簡體   English   中英

Java腳本的新功能。 如何使用應用程序腳本將我的VBA自定義函數轉換為在工作表中工作

[英]New to java script. How to convert my vba custom function to work in sheets using app scriptor

我正在嘗試根據我的VBA代碼在Google表格中創建自己的自定義函數,但無法正常工作。

   Function MyRandBetween(lowint As Long, highint As Long) As Long
        Randomize 
        MyRandBetween = Round(lowint + Rnd() * (highint - lowint), 0)
        End Function

您只需要使用Math.random()。

Google表格中的示例:

function myFunction() {
    var sheet = SpreadsheetApp.getActiveSheet();   
  SpreadsheetApp.getActiveSheet().getRange('A1').setValue(MyRandBetween(1,100));
}


function MyRandBetween(low, high) {
    return Math.floor(Math.random() * high) + low;
}

腳本:

在此處輸入圖片說明

結果:

在此處輸入圖片說明

 // Function MyRandBetween(lowint As Long, highint As Long) As Long // Randomize // MyRandBetween = Round(lowint + Rnd() * (highint - lowint), 0) // End Function const getRandom = (low, hi) => { return Math.round(low + Math.random() * (hi - low)); }; console.log(getRandom(1, 10)); console.log(getRandom(30, 100)); console.log(getRandom(20, 30)); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM