Free views, likes and subscribers at YouTube. Now!
Get Free YouTube Subscribers, Views and Likes

Programmatically create a new Google Doc and insert text into it

Follow
Laurence Svekis

Programmatically create a new Google Doc and insert text into it

Instructions:
Create a new Google Apps Script project and paste the code into the editor.
Save and run the createGoogleDoc function.
Check your Google Drive for the new document named "New Google Doc".
Explanation: This script creates a new Google Doc titled "New Google Doc" and then inserts a paragraph of text into the document.
Code:
function createGoogleDoc(){
const paraContents = 'My Name is Laurence Svekis';
const doc = DocumentApp.create('New Google Doc');
const body = doc.getBody();
body.appendParagraph(paraContents);
}


The function named createGoogleDoc() that uses Google Apps Script to create a new Google Doc document and add a paragraph of text to it.

Let's break down the code step by step:
const paraContents = 'My Name is Laurence Svekis';: This line declares a constant variable named paraContents and assigns the string 'My Name is Laurence Svekis' to it. This string represents the content that will be added to the Google Doc as a paragraph.
const doc = DocumentApp.create('New Google Doc');: This line creates a new Google Doc document with the title "New Google Doc" using the DocumentApp.create() method. It then assigns the reference to the newly created document to the constant variable doc.
const body = doc.getBody();: This line gets the body of the newly created Google Doc using the getBody() method of the doc object. The body variable now holds a reference to the body of the document where content can be added.
body.appendParagraph(paraContents);: This line appends a new paragraph to the body of the Google Doc. The content of the paragraph is specified by the paraContents variable, which contains the text "My Name is Laurence Svekis."

When you execute the createGoogleDoc() function, it performs the following actions:
Creates a new Google Doc with the title "New Google Doc."
Retrieves the body of the newly created Google Doc.
Appends a paragraph to the document's body with the text "My Name is Laurence Svekis."
As a result, you will have a new Google Doc document created with the specified content.

This is a basic example of how to programmatically create Google Docs and add content to them using Google Apps Script. You can further customize and format the document, add more content, and perform various other documentrelated operations as needed within your script.
Course content web development and web design courses with coding examples and source code for the lesson content. Source Code is available within my Github account. Lessons posted are designed to help students learn more about a specific topic related to modern web development and applying code.
Laurence Svekis is a professional top selling course author having instructed over 1 Million students both online and in person. Laurence Svekis is a Google Developer Expert specializing in Google Workspace automation using Google Apps Script Code.

Check out my Website for more details at https://basescripts.com/

posted by BaineeFeadwayrg