Google Apps Script code to create 3 Google Docs with some random content within each


Here’s a Google Apps Script code to create 3 Google Docs with some random content within each

function createBlogPosts() {
var docTitles = ["Blog Post 1", "Blog Post 2", "Blog Post 3"];

// Loop through each title to create a document
for (var i = 0; i < docTitles.length; i++) {
var doc = DocumentApp.create(docTitles[i]);
var body = doc.getBody();

// Generate random content for the blog post
var content = generateRandomContent();

// Append the content to the document body
body.appendParagraph(content);

// Save and close the document
doc.saveAndClose();
}
}

// Function to generate random content
function generateRandomContent() {
var paragraphs = [];

// Define possible content for the blog post
var contentOptions = [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
];

// Generate random number of paragraphs (between 3 and 7)
var numParagraphs = Math.floor(Math.random() * 5) + 3;

// Randomly select content for each paragraph
for (var i = 0; i < numParagraphs; i++) {
var randomIndex = Math.floor(Math.random() * contentOptions.length);
paragraphs.push(contentOptions[randomIndex]);
}

// Join paragraphs into a single string with line breaks
return paragraphs.join("\n\n");
}

To use this script:

  1. Go to script.google.com.
  2. Create a new script file and paste the above code.
  3. Save the script with an appropriate name.
  4. Run the function createBlogPosts(). It will create three Google Docs titled “Blog Post 1”, “Blog Post 2”, and “Blog Post 3”, each with random content generated by the generateRandomContent() function.