简体   繁体   English

Java脚本的新功能。 如何使用应用程序脚本将我的VBA自定义函数转换为在工作表中工作

[英]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.

相关问题 使用Java脚本在Google Maps v3上无法进行标记拖动。 怎么做? - Marker dragging doesn't work on google maps v3 using java script. How to do it? 如何将删除数字自定义函数 Excel VBA 函数转录到 Google Sheets Google Script (Javascript) - How transcribe remove number custom function Excel VBA function to Google Sheets Google Script (Javascript) 使用Java脚本上传Twitter图片。 - Twitter image upload using java script. 在新标签页中打开我的应用程序后,如何调用Java脚本函数? - How to call a Java script function after my app is open in a new tab? 如何使用html和Java脚本在Windows Azure移动服务中存储文件(恢复)? - How to Store file (Resume) in windows azure mobile service using html and java script.? Java脚本。 如何选择数组的任何几项 - Java Script. How to choose any few items of array Java脚本新手,例如如何将(1; 2; 3)转换为Java脚本数组 - New to Java script , How to convert for example (1;2;3) in to an java script array 如何修复错误:类型错误:无法正确读取未定义的谷歌应用程序脚本的“setValues”。 如何将它实现到我的代码中? - How to fix the Error : TypeError : Cannot read properly 'setValues' of undefined google app script. How to implement it to my the code? 在谷歌表外部库中使用 Java Script 原型函数 - Using Java Script prototype Function in a google sheets external Library 如何使用Java脚本在多个Excel工作表中下载.csv数据? - How to download .csv data in multiple excel sheets using java script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM