Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Google Sheets Script to Remove Image Links Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To remove image links from a Google Sheet using a script, you must use Google Apps Script to iterate through your cells and replace any string containing an image URL pattern with an empty string or a specific value. This is the most efficient method when dealing with thousands of rows where manual find-and-replace is impractical or when links are embedded in a way that standard filters cannot catch. This process allows you to sanitize your data, remove clutter from imported datasets, or prepare a sheet for clean printing by stripping out long, unsightly URLs that point to image hosting services.

What You'll Need

  • A Google account with access to Google Sheets.
  • A spreadsheet containing the image links you wish to remove.
  • Basic familiarity with navigating the Google Sheets menu system.
  • A stable internet connection to save and run the script in the cloud.

Step-by-Step

  1. Back Up Your Data: Before running any script that modifies cell contents, create a duplicate of your sheet. Go to File > Make a copy. Scripts cannot be "undone" with the standard Ctrl+Z command once they have completed their execution across a large range of cells.
  2. Open the Apps Script Editor: In your active Google Sheet, click on the Extensions menu and select Apps Script. This will open a new browser tab with the script editor interface. Note that menu labels may differ slightly depending on your region or the version of Google Workspace you are using.
  3. Clear the Default Code: You will see a default function called myFunction(). Delete all the existing code in the editor window to ensure there are no conflicts with the new script.
  4. Insert the Removal Script: Copy and paste the following code into the editor:
    function removeImageLinks() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var range = sheet.getDataRange();
      var values = range.getValues();
      
      // Regular expression to identify common image file extensions
      var imageRegex = /https?:\/\/[^\s]*\.(jpg|jpeg|png|gif|bmp|webp|svg)(\?[^\s]*)?/gi;
    
      for (var row = 0; row < values.length; row++) {
        for (var col = 0; col < values[row].length; col++) {
          var cellValue = values[row][col];
          if (typeof cellValue === 'string') {
            // Replace the image link with an empty string
            values[row][col] = cellValue.replace(imageRegex, '');
          }
        }
      }
      range.setValues(values);
    }
  5. Save the Project: Click the disk icon (Save project) in the toolbar or press Ctrl+S. Give your project a descriptive name, such as "Image Link Remover," so you can find it later.
  6. Run the Script: In the toolbar, ensure that removeImageLinks is selected in the dropdown menu next to the "Debug" button, then click the Run button.
  7. Authorize Permissions: A dialog box will appear stating that "Authorization is required." Click Continue. Select your Google account. You may see a warning that "Google hasn't verified this app" because you just wrote it. Click Advanced, then click "Go to Image Link Remover (unsafe)" at the bottom. Finally, click Allow to grant the script permission to edit your spreadsheets.
  8. Verify the Results: Return to your Google Sheet tab. You should see that all strings matching common image URL patterns have been removed from the active sheet.

Common Mistakes

One of the most frequent errors is attempting to run the script on a sheet that is currently filtered. While the script will still process the data, the visual representation in the sheet may be confusing, leading the user to believe the script didn't work. Always clear your filters before running a data-cleaning script to see the full scope of the changes.

Another common mistake is forgetting to handle the data type. Google Apps Script reads cell values as a variety of types, including numbers, booleans, and strings. If a script attempts to run a .replace() method on a cell that contains a pure number or a date, the script will crash with a "TypeError: cellValue.replace is not a function" error. The provided script avoids this by checking if (typeof cellValue === 'string') before attempting the replacement.

Users also frequently confuse "image links" (text URLs) with "embedded images" (images placed inside a cell using the =IMAGE() function). A script designed to remove text links will not remove a formula. To remove =IMAGE() formulas, you would need a different regex pattern that targets the formula string itself rather than the URL pattern.

Tips

To make the script more flexible, you can modify the imageRegex variable. If your image links do not end in standard extensions like .jpg or .png (for example, if they are dynamic API links), you can change the regex to look for specific keywords in the URL, such as "/images/" or "/media/".

If you have a massive dataset (over 10,000 rows), you might encounter a "Script timeout" error because Google Apps Script has a maximum execution time. To solve this, instead of using sheet.getDataRange(), you can process the sheet in smaller chunks by defining specific row ranges (e.g., rows 1-2000, then 2001-4000) and running the script multiple times.

For those who want to keep the text around the link but just remove the link itself, the .replace(imageRegex, '') part of the code is key. .

Troubleshooting

If the script runs but no links are removed, first check the format of your URLs. The provided regular expression looks for http or https followed by a file extension. If your links start with www. without the protocol, the regex will not find them. To fix this, update the regex to /(https?:\/\/)?[^\s]*\.(jpg|jpeg|png|gif|bmp|webp|svg)/gi.

If you receive a "Permission Denied" error, ensure you are the owner of the spreadsheet or have editor permissions. If you are using a corporate Google Workspace account, your administrator may have disabled the ability to run Apps Scripts. In this case, you will need to contact your IT department to request access to the Apps Script environment.

If the script is running extremely slowly, check if you have a large number of complex formulas in your sheet. Every time range.setValues(values) is called, Google Sheets triggers a recalculation of all formulas. To speed this up, consider converting your formulas to static values (Copy > Paste Special > Values only) before running the cleanup script.

Frequently Asked Questions

Will this script delete the actual images from my drive?

No. This script only removes the text strings (the URLs) from the cells in your spreadsheet. It does not have the permissions or the code to delete files from Google Drive or any external image hosting server.

Can I use this to remove links to websites that aren't images?

Yes, but you must modify the regular expression. The current script specifically targets image extensions (.jpg, .png, etc.). To remove any URL, you would use a more general regex like /https?:\/\/[^\s]+/gi.

Does this work on the Google Sheets mobile app?

You cannot write or trigger Apps Scripts directly from the mobile app. However, if you have already set up the script and created a custom menu button or a trigger, it may run, but the initial setup and execution must be done via a desktop browser.

How do I stop the script if it's taking too long?

In the Apps Script editor, there is an "Execution log" at the bottom. When a script is running, a "Stop" button appears in the toolbar. Clicking this will immediately terminate the process, though any changes already written to the sheet will remain.

Can I target only one specific column instead of the whole sheet?

Yes. Instead of using sheet.getDataRange(), you can use sheet.getRange("B:B") (replacing B with your desired column). This prevents the script from scanning unnecessary data and reduces the risk of accidentally removing links in columns where they should be kept.