[英]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. 我正在尝试根据我的VBA代码在Google表格中创建自己的自定义函数,但无法正常工作。
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(). 您只需要使用Math.random()。
Example in Google Sheets: 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;
}
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.