用于在多个单元格中生成随机数的脚本 [英] Script to generate random numbers in multiple cells

查看:37
本文介绍了用于在多个单元格中生成随机数的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一种在单元格c2:f2中从1-9生成随机数的方法;只要每个单元格中都有一个数字,重复的值就可以了,并且只有当我单击按钮或与此相关的内容时,值才会更改,而不是每次在单元格中键入内容时,这些值才会更改.就像您可以使用Excel和表单控件"框一样.

Looking for a way to generate random numbers from 1-9 in cells c2:f2; duplicate values are OK as long as its a number in each cell and the values change only when I click a button or something in that regard, not every time I type something in a cell. Kinda like you can do with Excel and "form control" boxes.

推荐答案

以下是一个脚本,它将使用1-9之间的随机数填充每个选定的单元格.可以从创建的菜单中激活它,称为随机填充".如果需要,您应该能够对其进行修改以适合您的特定要求:

Here is a script that will fill each the selected cells with a random number from 1-9. It can be activated from the menu it creates called "Fill random." You should be able to modify it, if needed, to suit your specific requirements:

function numbers19() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveRange();
  for (var x = 1; x <= range.getWidth(); x++) {
    for (var y = 1; y <= range.getHeight(); y++) {
      var number = Math.floor(Math.random() * 8) + 1;
      range.getCell(y, x).setValue(number);
    }
  }
};

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Numbers: 1-9",
    functionName : "numbers19"
  }];
  sheet.addMenu("Fill random", entries);
};

要仅更改特定范围,请对range使用以下值:

To only change a specific range, use the following value for range:

var range = sheet.getRange("c2:f2");

这篇关于用于在多个单元格中生成随机数的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆