当前位置:首页 > 未分类 > 正文内容

Fill Google Spreadsheet with Random Data

ceacer3周前 (05-02)未分类7182

The spreadSheetFill function will fill the cells of the currently active Google Spreadsheet with random data using Google Scripts. The random values are computed using the Math.random() method.

We could have used the rand() method of Google Spreadsheet directly but the disadvantage is the data will change / refresh every time you open the sheet or edit any cell.

Open the Google Apps Script editor inside the Google Spreadsheet and copy-paste the code. Next choose SpreadSheetFill from the Run menu and authorize.

/* Written by https://gist.github.com/thomaswilburn */

var rowConfig = 'timestamp name favorite note season'.split(' ');

var rowMapper = function (data) {
  var row = [];
  for (var key in data) {
    var index = rowConfig.indexOf(key);
    if (index > -1) {
      var value;
      if (key in data) {
        value = data[key];
      } else {
        value = '';
      }
      row[index] = data[key];
    }
  }
  for (var i = 0; i < row.length; i++) {
    if (typeof row[i] == 'undefined') {
      row[i] = '';
    }
  }
  return row;
};

function spreadSheetFill() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var count = 1000;
  var firstNames = ['Alice', 'Bob', 'Charles', 'Dawn', 'Erin', 'Fred', 'Gwen', 'Harry'];
  var lastNames = ['I.', 'J.', 'K.', 'L.', 'M.', 'N.'];
  var getRandom = function (arr) {
    return arr[Math.floor(Math.random() * arr.length)];
  };
  for (var i = 0; i < count; i++) {
    var position = Math.PI + Math.PI / 4 - Math.random() * Math.PI * 0.75;
    var distance = 5 * Math.random() + 7;
    var params = {
      timestamp: Date.now(),
      name: getRandom(firstNames) + ' ' + getRandom(lastNames),
      season: Math.random() > 0.5 ? true : '',
      favorite: Math.round(Math.random() * 90),
      note: Utilities.base64Encode(
        Utilities.computeDigest(
          Utilities.DigestAlgorithm.MD5,
          Math.round(Math.random() * 100000000) + '',
          Utilities.Charset.US_ASCII
        )
      ),
    };
    var row = rowMapper(params);
    sheet.appendRow(row);
  }
}

相关文章

How to Automatically Rename Files in Google Drive with Apps Script and AI

How to Automatically Rename Files in Google Drive with Apps Script and AI

Do you have image files in your Google Drive with generic names like IMG_123456.jpg or Screenshot.pn...

How to Sort Google Sheets Automatically with Apps Script

How to Sort Google Sheets Automatically with Apps Script

This Google Spreadsheet on Udemy courses has about 50 sheets, one for each programming language, and...

How to Make Personalized Place Cards with Guest Names

How to Make Personalized Place Cards with Guest Names

Whether it is a wedding party or a business conference, those tent-shaped place cards are ideal for...

Color Codes for Google Spreadsheets

This Google Script converts the currently active Google Spreadsheet into a square grid of randomly c...

How to Use Conditional Formatting in Google Sheets to Highlight Information

How to Use Conditional Formatting in Google Sheets to Highlight Information

Conditional formatting in Google Sheets makes it easy for you to highlight specific cells that meet...

How to Extract URLs from HYPERLINK Function in Google Sheets

The HYPERLINK formula of Google Sheets lets you insert hyperlinks into your spreadsheets. The functi...