How to remove whitespace in the front of page elements in Google Docs with Apps Script Easy Clean up

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:

  1. function removeLeadingWhitespace() {: This line defines a JavaScript function named removeLeadingWhitespace. This function will be used to remove leading whitespace from text elements in the document.
  2. var body = DocumentApp.getActiveDocument().getBody();: This line retrieves the body of the currently active Google Docs document using DocumentApp.getActiveDocument(). The body variable will be used to access the content of the document.
  3. var elements = body.getParagraphs();: This line fetches all the paragraph elements (text elements) within the document’s body and stores them in the elements variable.
  4. Logger.log(elements.length);: This line logs the number of text elements found in the document using the Logger.log function. This is for debugging purposes and will display the count of text elements in the document in the Apps Script logs.
  5. for (var i = 0; i < elements.length; i++) {: This line starts a for loop that iterates through each text element in the elements array.
  6. var element = elements[i];: Inside the loop, it retrieves the current text element and assigns it to the element variable.
  7. var text = element.getText();: It retrieves the text content of the current element and stores it in the text variable.
  8. if (text) {: This conditional statement checks if the text variable is not empty. If the element contains text (not just whitespace), it proceeds with the following operations:
  9. var newText = text.replace(/^\s+/g, '');: This line removes leading whitespace or blank space from the text using the replace 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 the newText variable.
  10. if (newText !== "") {: This conditional statement checks if newText is not an empty string.
  11. element.setText(newText);: If newText is not empty, it sets the newText as the text content of the current element, effectively removing the leading whitespace.
  12. 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.