Apps Script Coding Questions
๐ Exciting Insights into Google Apps Script: Mastering DocumentApp! ๐
Question 1: Creating a New Document
Question: How do you create a new Google Document using Google Apps Script?
Answer:
function createNewDocument() {
var doc = DocumentApp.create(‘New Document’);
Logger.log(doc.getUrl());
}
Explanation: This script uses DocumentApp.create() to create a new document titled ‘New Document’. The URL of the created document is then logged.
Question 2: Adding Text to a Document
Question: How can you add a paragraph of text to a document?
Answer:
function addTextToDocument() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
var body = doc.getBody();
body.appendParagraph(‘This is a new paragraph.’);
}
Explanation: This code snippet opens an existing document using its ID, gets its body, and then appends a new paragraph with the specified text.
Question 3: Formatting Text
Question: How do you change the font size and style of a specific paragraph?
Answer:
function formatText() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
var body = doc.getBody();
var paragraph = body.getParagraphs()[0];
paragraph.setFontSize(12).setBold(true);
}
Explanation: This function accesses the first paragraph of the document’s body and changes its font size to 12 and sets it to bold.
Question 4: Inserting a Table
Question: How can you insert a 2×2 table into a document?
Answer:
function insertTable() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
var body = doc.getBody();
var table = body.appendTable([[‘Cell 1’, ‘Cell 2’], [‘Cell 3’, ‘Cell 4’]]);
}
Explanation: This script adds a table with two rows and two columns to the document, with each cell containing predefined text.
Question 5: Adding a Header
Question: How do you add a header to a document?
Answer:
function addHeader() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
var header = doc.addHeader();
header.appendParagraph(‘This is a header.’);
}
Explanation: This function creates a header in the document and adds a paragraph of text to it.
Question 6: Inserting an Image
Question: How can you insert an image into a document?
Answer:
function insertImage() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
var body = doc.getBody();
var imageUrl = ‘https://example.com/image.png’; // Replace with your image URL
var image = UrlFetchApp.fetch(imageUrl).getBlob();
body.appendImage(image);
}
Explanation: This script fetches an image from a URL and inserts it into the document’s body. Note that the URL must be publicly accessible.
Question 7: Creating a Footer
Question: How do you create a footer in a document?
Answer:
function addFooter() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
var footer = doc.addFooter();
footer.appendParagraph(‘This is a footer.’);
}
Explanation: This function adds a footer to the document and then inserts a paragraph of text into it.
Question 8: Changing Document Properties
Question: How can you change the title of a document?
Answer:
function changeDocumentTitle() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
doc.setName(‘New Title’);
}
Explanation: This code snippet changes the title of an open document to ‘New Title’.
Question 9: Finding and Replacing Text
Question: How do you find and replace text in a document?
Answer:
function findAndReplaceText() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
var body = doc.getBody();
body.replaceText(‘oldText’, ‘newText’);
}
Explanation: This script searches the entire body of the document for ‘oldText’ and replaces it with ‘newText’.
Question 10: Removing Content from a Document
Question: How can you delete a specific paragraph from a document?
Answer:
function deleteParagraph() {
var doc = DocumentApp.openById(‘YOUR_DOCUMENT_ID’);
var body = doc.getBody();
var paragraph = body.getParagraphs()[1]; // assuming you want to delete the second paragraph
paragraph.removeFromParent();
}
Explanation: This function removes the second paragraph from the document’s body. The paragraph is identified by its index in the getParagraphs() array.
These questions and answers cover various functionalities of the DocumentApp class in Google Apps Script, demonstrating how to manipulate and format Google Docs documents programmatically.