Jump to content

Fill Google Spreadsheet with Random Data


Recommended Posts

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

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. to insert a cookie message