How to Fill a Google Spreadsheet with Random Data Using Apps Script – A Complete Guide
When you're building spreadsheets for development, testing, demos, training, or prototyping data workflows, it's often helpful to populate your sheet with random data. Whether you're simulating sales records, customer data, test scores, or random numbers, you can use Google Apps Script to automatically generate and fill random data into your Google Spreadsheet.
In this detailed blog post, you’ll learn how to use Apps Script to generate and fill Google Sheets with different types of random data including:
Numbers (integer, decimal)
Names
Email addresses
Dates
Cities or countries
Custom values (e.g., product types, categories)
Let’s walk through how to create this with full flexibility and customization.
Here are some common use cases:
Testing formulas and scripts before working on real data
Building mock dashboards or prototypes
Training teams on spreadsheet features
Populating examples for tutorials or demos
Developing templates or models without relying on sensitive data
Manual data entry can be time-consuming and repetitive. With Google Apps Script, you can automate the process and regenerate data any time with one click.
Open Google Sheets and set up your header row with fields like:
| Name | Age | City | Join Date | Sales |
|---|
We'll populate random values under each column.
Go to Extensions > Apps Script
Clear any existing code in the editor.
function fillRandomData() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const numRows = 100; // Number of rows of random data to generate
const headers = ["Name", "Email", "Age", "City", "Join Date", "Sales"];
sheet.clear(); // Clear existing content
sheet.getRange(1, 1, 1, headers.length).setValues([headers]); // Set headers
const names = ["Alice", "Bob", "Carol", "David", "Eva", "Frank", "Grace", "Henry", "Ivy", "Jake"];
const domains = ["example.com", "mail.com", "demo.org", "test.net"];
const cities = ["Mumbai", "Delhi", "Bangalore", "Kolkata", "Chennai", "Pune", "Hyderabad"];
const data = [];
for (let i = 0; i < numRows; i++) {
const firstName = names[Math.floor(Math.random() * names.length)];
const lastName = String.fromCharCode(65 + Math.floor(Math.random() * 26)) + String.fromCharCode(97 + Math.floor(Math.random() * 26));
const email = `${firstName.toLowerCase()}.${lastName.toLowerCase()}@${domains[Math.floor(Math.random() * domains.length)]}`;
const age = Math.floor(Math.random() * 43) + 18; // 18 to 60
const city = cities[Math.floor(Math.random() * cities.length)];
const joinDate = randomDate(new Date(2020, 0, 1), new Date()); // Jan 1, 2020 to today
const sales = (Math.random() * 10000).toFixed(2); // Random sales amount
data.push([`${firstName} ${lastName}`, email, age, city, joinDate, parseFloat(sales)]);
}
sheet.getRange(2, 1, data.length, data[0].length).setValues(data);
Logger.log("Random data filled successfully.");
}
// Generate a random date between two dates
function randomDate(start, end) {
const date = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
return Utilities.formatDate(date, Session.getScriptTimeZone(), "yyyy-MM-dd");
}
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const numRows = 100;
We define which sheet to work with and how many rows of random data to generate.
const names = ["Alice", "Bob", "Carol", "David"];
const domains = ["example.com", "demo.org"];
const cities = ["Mumbai", "Delhi", "Chennai"];
You can customize these arrays to match your context (countries, products, departments, etc.).
for (let i = 0; i < numRows; i++) {
// Generate random first/last names, email, city, date, etc.
}
Each row is a unique random combination of values.
function randomDate(start, end) {
...
}
This helper generates a random date in a specific format between two given dates.
Save the project (Ctrl + S) and name it something like "Random Data Generator".
Click the Run button ▶.
The first time, you’ll need to authorize the script.
Once executed, your spreadsheet will be filled with 100 rows of random data.
| Name | Age | City | Join Date | Sales | |
|---|---|---|---|---|---|
| Eva Lb | eva.lb@example.com | 37 | Mumbai | 2021-09-14 | 8534.76 |
| Henry Np | henry.np@test.net | 26 | Bangalore | 2022-01-20 | 2043.90 |
| Bob Tg | bob.tg@mail.com | 44 | Delhi | 2020-11-03 | 7212.30 |
| Alice Vn | alice.vn@example.com | 52 | Pune | 2023-06-07 | 635.12 |
You can expand this script to include:
Phone numbers
Departments or job titles
Boolean fields (e.g., active/inactive)
Custom number ranges
Multi-sheet generation
Gender, salary, scores, etc.
For example, to generate random phone numbers:
const phone = "+91 " + Math.floor(1000000000 + Math.random() * 9000000000);
Developers creating UI dashboards with placeholder data
Trainers needing example data for students
Analysts mocking test cases for dashboards
Product managers designing data templates
QA engineers simulating performance on large datasets
To add a custom menu in the spreadsheet so users can regenerate data:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu("⚙️ Utilities")
.addItem("Fill Random Data", "fillRandomData")
.addToUi();
}
Add this function at the end of your script. Now every time you open the spreadsheet, a menu called “⚙️ Utilities” will appear with an option to "Fill Random Data".
Generating random data in Google Sheets using Apps Script is a powerful way to automate and simulate datasets. With this flexible script, you can populate spreadsheets in seconds, enabling easier testing, demos, templates, and educational materials.
Whether you’re a developer, analyst, instructor, or just need a working dataset fast, this tool will save time and add professionalism to your work. You can always extend the script to match your exact requirements. Use it, customize it, and build your spreadsheet environments faster and smarter.