Programmatically create a new Google Doc and insert text into it

  Рет қаралды 252

Laurence Svekis

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 document-related 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 basescripts.com/

Пікірлер
Automated Google Documents with Google Apps Script
16:42
Matt Brigidi
Рет қаралды 5 М.
Je peux le faire
00:13
Daniil le Russe
Рет қаралды 14 МЛН
Incredible Dog Rescues Kittens from Bus - Inspiring Story #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 39 МЛН
Python RAG Tutorial (with Local LLMs): AI For Your PDFs
21:33
pixegami
Рет қаралды 227 М.
How to Create a Secure Login System Using Google Apps Script Web App
17:28
13 Things To Remove From Your Website Immediately
12:33
Orbit Media Studios
Рет қаралды 313 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 401 М.
JavaScript Tutorial: Toggle Image Visibility with a Click
6:23
Laurence Svekis
Рет қаралды 114
Use these instead of vh
6:06
Kevin Powell
Рет қаралды 506 М.
Je peux le faire
00:13
Daniil le Russe
Рет қаралды 14 МЛН