Got it! So your updated requirements are:
- The input is a single website URL, located at cell A2.
- From this URL, extract all image URLs and put them in row 1 starting at column A.
- Extract all corresponding image alt texts and put them in row 3 starting at column A, aligned with their image URLs.
- Row 2 can be left blank or untouched.
Here is the modified Apps Script to do exactly that:
function extractImagesFromSingleUrl() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Get the URL from cell A2
const url = sheet.getRange("A2").getValue();
if (!url || url.trim() === "") {
SpreadsheetApp.getUi().alert("Please enter a website URL in cell A2.");
return;
}
try {
const response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
if (response.getResponseCode() !== 200) {
SpreadsheetApp.getUi().alert("Failed to fetch the URL. Status code: " + response.getResponseCode());
return;
}
const html = response.getContentText();
// Extract all <img ...> tags
const imgTagRegex = /<img\b[^>]*>/gi;
const imgTags = html.match(imgTagRegex);
// If no images found, clear row 1 and 3 and alert
if (!imgTags || imgTags.length === 0) {
// Clear row 1 starting from column A (let's clear 100 columns to be safe)
sheet.getRange(1, 1, 1, 100).clearContent();
sheet.getRange(3, 1, 1, 100).clearContent();
SpreadsheetApp.getUi().alert("No images found on the page.");
return;
}
let imgUrls = [];
let altTexts = [];
for (let tag of imgTags) {
// Extract src attribute
let srcMatch = tag.match(/\bsrc\s*=\s*(['"])(.*?)\1/i);
let src = srcMatch ? srcMatch[2] : "";
// Extract alt attribute
let altMatch = tag.match(/\balt\s*=\s*(['"])(.*?)\1/i);
let alt = altMatch ? altMatch[2] : "";
if (src) {
// Convert relative URLs to absolute URLs
try {
let absoluteUrl = new URL(src, url).href;
imgUrls.push(absoluteUrl);
} catch(e) {
// If URL resolution fails, keep src as-is
imgUrls.push(src);
}
altTexts.push(alt);
}
}
if (imgUrls.length === 0) {
// No valid image src found
sheet.getRange(1, 1, 1, 100).clearContent();
sheet.getRange(3, 1, 1, 100).clearContent();
SpreadsheetApp.getUi().alert("No valid images with src found on the page.");
return;
}
// Clear row 1 and 3 before writing (clear 100 columns to be safe)
sheet.getRange(1, 1, 1, 100).clearContent();
sheet.getRange(3, 1, 1, 100).clearContent();
// Set image URLs in row 1
sheet.getRange(1, 1, 1, imgUrls.length).setValues([imgUrls]);
// Set alt texts in row 3
sheet.getRange(3, 1, 1, altTexts.length).setValues([altTexts]);
SpreadsheetApp.getUi().alert("Image URLs and alt texts extracted successfully!");
} catch (error) {
SpreadsheetApp.getUi().alert("Error fetching or processing the URL:\n" + error.message);
}
}
Steps to use this script:
- Paste the website URL into cell A2 of your Google Sheet.
- Open the script editor:
- Paste the above code into the script editor.
- Save the project.
- Run the
extractImagesFromSingleUrl function.
- Authorize permissions if prompted.
- The script will extract images from the URL in A2 and populate:
- Row 1 (starting at A1): image URLs
- Row 3 (starting at A1): alt texts (matched by column)
- Row 2 is not touched and left for other purposes if needed.
If you want me to add anything else (e.g., clear row 2 as well, or add error handling differently), just let me know!