If you want to fetch the alt text of images whose image URLs are listed in your sheet (i.e., direct image URLs like https://example.com/image.jpg), the alt text is not embedded in the image file itself — alt text is part of the HTML <img> tag on some webpage that displays the image.
To fetch alt text for such image URLs, you generally need to:
- Identify the webpage(s) where those images appear (the page showing the
<img src="your-image-url" alt="some alt text">).
- Scrape the HTML of that webpage to find the
<img> tag with an src matching your image URL, then extract its alt.
Example Apps Script: Find alt text of images by searching the given URL's HTML for <img> tags matching the image URLs in your sheet
This script assumes:
- Column A has image URLs (direct image URLs).
- Column B will be populated with the alt text extracted from a corresponding "page URL" which you provide in Column C (the page where the image is embedded).
If you only have image URLs, no page URLs, it's impossible to get alt text because alt text is a property of the HTML page, not the image itself.
Here is a sample script:
function fetchAltTextsFromPages() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = sheet.getLastRow();
// Assuming:
// A2:A = image URLs
// C2:C = page URLs where the images appear
// B2:B = output alt text
var imageUrls = sheet.getRange("A2:A" + lastRow).getValues();
var pageUrls = sheet.getRange("C2:C" + lastRow).getValues();
for (var i = 0; i < imageUrls.length; i++) {
var imgUrl = imageUrls[i][0];
var pageUrl = pageUrls[i][0];
if (imgUrl && pageUrl) {
try {
var html = UrlFetchApp.fetch(pageUrl, {muteHttpExceptions: true}).getContentText();
var alt = getAltForImageUrl(html, imgUrl);
if (!alt) alt = "No alt found";
sheet.getRange(i + 2, 2).setValue(alt);
} catch (e) {
sheet.getRange(i + 2, 2).setValue("Error: " + e.message);
}
} else {
sheet.getRange(i + 2, 2).setValue("Missing URL");
}
}
}
function getAltForImageUrl(html, imageUrl) {
// Normalize imageUrl for matching by removing protocol, etc.
var normalizedImageUrl = imageUrl.replace(/^https?:/, '');
var imgTags = html.match(/<img [^>]*src=['"][^'"]+['"][^>]*>/gi);
if (!imgTags) return null;
for (var i = 0; i < imgTags.length; i++) {
var tag = imgTags[i];
// Extract src
var srcMatch = tag.match(/src=['"]([^'"]+)['"]/i);
if (!srcMatch) continue;
var src = srcMatch[1].replace(/^https?:/, '');
if (src === normalizedImageUrl || src.endsWith(imageUrl)) {
// Found matching image src - extract alt
var altMatch = tag.match(/alt=['"]([^'"]*)['"]/i);
return altMatch ? altMatch[1] : "";
}
}
return null;
}
How to use this script:
- Put your image URLs into Column A (starting A2).
- Put the webpage URLs (the pages where those images appear) into Column C (starting C2).
- Run
fetchAltTextsFromPages() in Apps Script Editor.
- The alt text of the matching image on the page will be filled into Column B.
Important:
- If you do not have webpage URLs where the images appear, unfortunately you cannot get alt text because image files do not carry the alt attribute.
- If the image appears multiple times on the page with different alt texts, this will retrieve the alt of the first matching
<img> tag.
- You may need to handle relative URLs and other HTML edge cases depending on your use case.
If you want a solution tailored to a specific scenario, feel free to share more details!