BOT's #5: A simple Google App Script to save you time when creating multiple documents

BOT's #5: A simple Google App Script to save you time when creating multiple documents

Are you trying to find a faster yet simple way to create multiple documents with a template? If yes, you can use Canva to design your template and place it on Google docs. Better just create one directly on google docs without Canva. The steps below will guide you on how to do this with ease.

Google App Script allows you to automate tasks and create powerful scripts that can be used with various Google services including Google Docs. Here's how you can create multiple documents using a template:


1. First, create a Google Doc that will act as your template.

2. Open the Script Editor in Google Docs by selecting "Tools" > "Script editor".

3. In the Script Editor, type the following code:


javascript

function createMultDocuments() { 
 //ID of the document template created in stage #1
 var gDocTemplate = DriveApp.getFileById('TEMPLATE_DOC_ID');
 // ID of the Google Drive folder to save new documents
 var destFolder = DriveApp.getFolderById('FOLDER_ID')
 //Here we store the sheet as a variable
 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SHEET_TAB_NAME')
 //Now we get all of the values as an array of data
 var rows = sheet.getRange(starting_row_integer_of_the_range, starting_column_integer_of_the_range, sheet.getLastRow() - 1, last_column_integer_of_the_range).getDisplayValues();
 var hasdata = 1;
 //Check if row has a data to enable creation from the template
 rows = rows.filter(function (r) { return r[use_any_column_in_the_range_to_validate] !== '' });
 //Start processing each spreadsheet row


 rows.forEach(function (row, index) {
 ? //Check if this row is the headers, if so we skip it
 ? if (index === 0) return;


 ? //******WORKING WITH ARRAYS STARTS HERE*////
 ? //Guide to working with array of data A=0, B=1, C=2, D=3, E=(column integer - 1).....
 ? //Check if a document has already been generated by looking at 'Document Link', if so we skip it
 ? if (row[replace_with_correct_column_integer]) return;
 ? //Using the row data in a template literal, we make a copy of our template document in our destination Folder
 ? var copy = gDocTemplate.makeCopy(`${row[replace_with_correct_column_integer]}`, destFolder)
 ? //Once we have the copy, we then open it using the DocumentApp
 ? var doc = DocumentApp.openById(copy.getId())
 ? //All of the content lives in the body, so we get that for editing
 ? var body = doc.getBody();


 ? //In these lines, we replace our replacement tokens with values from our spreadsheet row
 ? body.replaceText('{{REPLACE_WITH_TEXT_ONE}}', row[replace_with_correct_column_integer]);
 ? body.replaceText('{{REPLACE_WITH_TEXT_TWO}}', row[replace_with_correct_column_integer]);


 ? //We make our changes permanent by saving and closing the document
 ? //******WORKING WITH ARRAYS ENDS HERE****/
 ? doc.saveAndClose();
 ? //Store the url of our new document in a variable
 ? var url = doc.getUrl();
 ? //Write that value back to the 'Document Link' column in the spreadsheet.
 ? sheet.getRange(index + 1, replace_with_correct_column_integer_where_the_doc_url_should_be).setValue(url)


 ? // Make sure the cell is updated right away in case the script is interrupted
 ? SpreadsheetApp.flush();
 })
}        

4. Replace all placeholders with their correct equivalents?

5. Update the 'data' array with the information for each new document you want to create. Each row should contain the document name and the content you want to replace in the template.

6. Save the script and run the 'createMultDocuments' function by selecting "Run" > "createMultDocuments".


The script will create a new document for each row in your 'data' array, replacing the placeholder text in the template with the corresponding content. The new documents will be saved in the destination folder you specified.


I hope this helps!

Use Cases

  • Contract Issuing
  • PaySlip Issuing
  • Event Planning

About the author

Rogers is a data and emerging tech enthusiast. He is the Co-founder and VP of Operations at?OurPass.

OurPass is a global neobank providing businesses of all sizes with access to every banking, payment, and business management tool they need to start, grow and scale their businesses.

Our mission is to create a borderless world of successful businesses and we are constantly building new tools to ensure that whatever stage a business is in, they have access to every tool they need to thrive, all in one place.

Founded in 2021 as a one-click checkout company, we made a pivot in July 2022 when we saw that beyond offering a niche service, we could provide end-to-end solutions that helped entrepreneurs grow every aspect of their businesses.

In just a few months since our transition, we have become a leading business bank serving thousands of customers including some of the biggest retail outlets in Nigeria such as Spar, Shoprite, and Eat N Go (parent company of Dominos, Cold Stone Creamery, and Pinkberry), processing about a million transactions monthly.

OurPass also holds a Microfinance Banking License from the Central Bank of Nigeria.

To unlock limitless smart business banking that OurPass app offers, download the app from the?App Store?or?Google Play Store, create an account, and join thousands of businesses growing with us.

Connect with us, join our vibrant community on?Facebook,?Twitter,?Instagram,?LinkedIn,?and?YouTube, and stay up to date with the latest information about our services.

Adebayo Opesanya

Experienced Technical Leader & Manager. Fascinated by complex systems and obsessed with building and scaling highly effective engineering teams.

1 年

You’re finally sharing your genius with us. Thank you boss ????

要查看或添加评论,请登录

Rogers Mugisa的更多文章

社区洞察

其他会员也浏览了