Fill Google Spreadsheet with Random Data

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

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻