简体   繁体   中英

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

I am trying to create my own custom function in Google sheets based on my VBA code but its not working.

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

You simply need to use Math.random().

Example in Google Sheets:

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

Script:

在此处输入图片说明

Result:

在此处输入图片说明

 // 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)); 

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