Google Apps Script that removes a line from a Google Docs document if it contains only a certain value

  1. Open the Google Docs Document: Open the document where you want to apply this script.
  2. Open the Script Editor:
    • Go to Extensions > Apps Script.
    • This will open a new tab with the Google Apps Script editor.
  3. Write the Script:
    • In the script editor, you can write a function to search for the specific line and remove it if it matches your criteria.

Here’s an example script that you can use as a starting point. This script will look for lines that contain only a specific value (e.g., “REMOVE_ME”) and remove them:

function removeSpecificLines() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragraphs = body.getParagraphs();

  var valueToRemove = "REMOVE_ME"; // Replace with the value you're looking for

  for (var i = paragraphs.length - 1; i >= 0; i--) {
    var text = paragraphs[i].getText().trim();
    if (text === valueToRemove) {
      body.removeChild(paragraphs[i]);
    }
  }
}
  1. Save and Run the Script:
    • Click the disk icon or File > Save to save your script.
    • Give your project a name.
    • Run the function removeSpecificLines by clicking the play button.
  2. Authorize the Script:
    • The first time you run the script, you’ll need to authorize it.
    • Follow the prompts to allow your script to access your Google Doc.
  3. Using the Script:
    • Every time you want to remove lines containing the specific value, run the removeSpecificLines function in the script editor.
  4. Automatic Trigger (Optional):
    • If you want this script to run automatically, you can set up a trigger in the script editor.
    • Go to Edit > Current project's triggers.
    • Click Add Trigger, select the removeSpecificLines function, choose the event source (like From calendar), and set the type of trigger (like Time-driven).

Remember to replace "REMOVE_ME" in the script with the actual value you want to remove. This script checks each paragraph in the document; if the paragraph’s text exactly matches the specified value, it will be removed.

Search for and remove lines containing several different values

To modify the script so that it can search for and remove lines containing several different values, you can update the script to check against an array of values. Here’s how you can do it:

  1. Define an array with the values you want to search for and remove.
  2. Iterate over each paragraph in the document.
  3. For each paragraph, check if its text matches any of the values in the array.
  4. If a match is found, remove that paragraph.

Here’s the updated script:

function removeMultipleSpecificLines() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragraphs = body.getParagraphs();

  var valuesToRemove = ["VALUE_1", "VALUE_2", "VALUE_3"]; // Replace with the values you're looking for

  for (var i = paragraphs.length - 1; i >= 0; i--) {
    var text = paragraphs[i].getText().trim();
    if (valuesToRemove.includes(text)) {
      body.removeChild(paragraphs[i]);
    }
  }
}

In this script, replace "VALUE_1", "VALUE_2", and "VALUE_3" with the actual values you want to search for and remove. You can add as many values as you need to the valuesToRemove array.

The rest of the steps to save, run, and authorize the script remain the same as described in the previous response.

This script will now remove any line that exactly matches any of the values specified in the valuesToRemove array. Keep in mind that this script treats the text comparison as case-sensitive and will not remove lines that contain additional text or formatting beyond the specified values.