cedroid

Google Apps Script generate fake data in multiple tabs

Jan 18th, 2024 (edited)
1,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This script is ideal for demonstration purposes, showing how to automate the creation of multiple sheets with randomized data and manage sheets within a Google Spreadsheet programmatically used only to populate google sheets with random data in multiple tabs.
  3.  
  4. "createSampleSheets"
  5. This function creates 20 new sheets in your current Google Sheets document. Each sheet is named "SampleSheet" followed by a number (1 through 20). The sheets are populated with shuffled columns and fake data, simulating hotel reservation information. The columns include "Date", "Name", "Room", "Price", "Check-in", and "Check-out".
  6.  
  7. Shuffling Columns: The column headers are shuffled in each sheet to create variability.
  8. Fake Data Generation: Each column is filled with appropriate fake data. Dates are randomly generated between November 2023 and February 2024. Names are randomly generated as "Guest" followed by a number. Room numbers and prices are also randomly generated.
  9.  
  10. "shuffleArray"
  11. This utility function takes an array and shuffles its elements in a random order. It's used in createSampleSheets to randomize the order of column headers in each new sheet.
  12.  
  13. "generateFakeData"
  14. This function generates fake data appropriate to the type of data each column represents. For date-related columns ("Date", "Check-in", and "Check-out"), it creates random dates within a specified range (November 2023 to February 2024). For the "Name" column, it generates a guest name. For "Room", it produces a random room number, and for "Price", a random price value is generated.
  15.  
  16. "deleteAllSheetsExceptFirst"
  17. This function deletes all sheets in the active Google Spreadsheet, except for the first sheet. This is particularly useful for cleaning up after testing or demonstrations, but should be used with caution to avoid unintentional deletion of important data.
  18. */
  19.  
  20. function createSampleSheets() {
  21.   var ss = SpreadsheetApp.getActiveSpreadsheet();
  22.  
  23.   // Define the column names
  24.   var columnNames = ["Date", "Name", "Room", "Price", "Check-in", "Check-out"];
  25.  
  26.   // Create 20 sample sheets, you can generate more or less
  27.   for (var i = 0; i < 20; i++) {
  28.     var sheetName = "SampleSheet" + (i + 1);
  29.     var sheet = ss.insertSheet(sheetName);
  30.  
  31.     // Shuffle the column names for each sheet
  32.     var shuffledColumns = shuffleArray(columnNames.slice()); // Create a copy and shuffle it
  33.  
  34.     // Set the column names
  35.     sheet.getRange(1, 1, 1, shuffledColumns.length).setValues([shuffledColumns]);
  36.  
  37.     // Fill with fake data
  38.     for (var row = 2; row <= 21; row++) {
  39.       for (var col = 1; col <= shuffledColumns.length; col++) {
  40.         var fakeData = generateFakeData(shuffledColumns[col - 1]);
  41.         sheet.getRange(row, col).setValue(fakeData);
  42.       }
  43.     }
  44.   }
  45. }
  46.  
  47. function shuffleArray(array) {
  48.   for (var i = array.length - 1; i > 0; i--) {
  49.     var j = Math.floor(Math.random() * (i + 1));
  50.     var temp = array[i];
  51.     array[i] = array[j];
  52.     array[j] = temp;
  53.   }
  54.   return array;
  55. }
  56.  
  57.  
  58. function generateFakeData(columnName) {
  59.   // Generate fake data based on the column name
  60.   switch (columnName) {
  61.     case "Date":
  62.     case "Check-in":
  63.     case "Check-out":
  64.       // Generate a random date between November 2023 and February 2024
  65.       var startDate = new Date(2023, 10, 1); // November 1, 2023
  66.       var endDate = new Date(2024, 1, 28);   // February 28, 2024
  67.       var randomDate = new Date(startDate.getTime() + Math.random() * (endDate.getTime() - startDate.getTime()));
  68.       return Utilities.formatDate(randomDate, Session.getScriptTimeZone(), "MM/dd/yyyy");
  69.     case "Name":
  70.       return "Guest " + Math.floor(Math.random() * 100);
  71.     case "Room":
  72.       return Math.floor(Math.random() * 500) + 1; // Random room number
  73.     case "Price":
  74.       return (Math.random() * 300 + 50).toFixed(2); // Random price between 50 and 350
  75.     default:
  76.       return "Unknown";
  77.   }
  78. }
  79.  
  80. function deleteAllSheetsExceptFirst() {
  81.   var ss = SpreadsheetApp.getActiveSpreadsheet();
  82.   var sheets = ss.getSheets();
  83.  
  84.   // Loop through sheets and delete all except the first one
  85.   for (var i = sheets.length - 1; i > 0; i--) {
  86.     ss.deleteSheet(sheets[i]);
  87.   }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment