function removeLeadingWhitespace() {
var body = DocumentApp.getActiveDocument().getBody();
var elements = body.getParagraphs();
Logger.log(elements.length);
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var text = element.getText();
// Check if the element contains text before attempting to remove leading whitespace
if (text) {
// Remove leading whitespace or blank space
var newText = text.replace(/^\s+/g, '');
// Check if newText is not empty before setting it as the element's text
if (newText !== "") {
element.setText(newText);
}
}
}
}
This code is a Google Apps Script that is designed to remove leading whitespace or blank space from text elements within a Google Docs document. Let’s break it down step by step:
function removeLeadingWhitespace() {
: This line defines a JavaScript function namedremoveLeadingWhitespace
. This function will be used to remove leading whitespace from text elements in the document.var body = DocumentApp.getActiveDocument().getBody();
: This line retrieves the body of the currently active Google Docs document usingDocumentApp.getActiveDocument()
. Thebody
variable will be used to access the content of the document.var elements = body.getParagraphs();
: This line fetches all the paragraph elements (text elements) within the document’s body and stores them in theelements
variable.Logger.log(elements.length);
: This line logs the number of text elements found in the document using theLogger.log
function. This is for debugging purposes and will display the count of text elements in the document in the Apps Script logs.for (var i = 0; i < elements.length; i++) {
: This line starts afor
loop that iterates through each text element in theelements
array.var element = elements[i];
: Inside the loop, it retrieves the current text element and assigns it to theelement
variable.var text = element.getText();
: It retrieves the text content of the current element and stores it in thetext
variable.if (text) {
: This conditional statement checks if thetext
variable is not empty. If the element contains text (not just whitespace), it proceeds with the following operations:var newText = text.replace(/^\s+/g, '');
: This line removes leading whitespace or blank space from thetext
using thereplace
method. The regular expression/^\s+/g
matches one or more whitespace characters (\s
) at the beginning of the string (^
indicates the start of the string). It replaces them with an empty string, effectively removing them. The result is stored in thenewText
variable.if (newText !== "") {
: This conditional statement checks ifnewText
is not an empty string.element.setText(newText);
: IfnewText
is not empty, it sets thenewText
as the text content of the current element, effectively removing the leading whitespace.- The loop continues to the next element until all elements in the document have been processed.
In summary, this script goes through all text elements in a Google Docs document’s body, removes leading whitespace from each element if it exists, and updates the element’s text content without leading whitespace. It’s useful for cleaning up documents where unwanted leading spaces might have been accidentally added.