Write the google sheet app script code for fetching the the meta title in 3rd column and meta description in 4th column
Assistant
User
Reads URLs from column B
Assistant
Got it! Below is the updated script that reads URLs from column B (2nd column) instead of column A, and writes meta title to column C (3rd column) and meta description to column D (4th column), starting from row 2:
javascript
Certainly! Here’s a Google Apps Script that fetches the meta title and meta description of URLs listed in the first column of your sheet, and writes the meta title in the 3rd column (Column C) and the meta description in the 4th column (Column D).
How it works:
Reads URLs from column A (starting from row 2, assuming row 1 has headers).
Fetches the page content using UrlFetchApp.
Extracts meta title and description using regex.
Writes results back to columns C and D.
Script Code:
javascript
function fetchMetaTitleAndDescription() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const lastRow = sheet.getLastRow();
// Read URLs from column A (starting from row 2)
const urlRange = sheet.getRange(2, 1, lastRow - 1, 1);
const urls = urlRange.getValues();
// Arrays to store meta titles and descriptions
const titles = [];
const descriptions = [];
for (let i = 0; i < urls.length; i++) {
let url = urls[i][0];
if (url) {
try {
// Fetch the content of the URL with a timeout of 5 seconds
const response = UrlFetchApp.fetch(url, {muteHttpExceptions: true, timeout: 5000});
const html = response.getContentText();
// Regex to get <title>content</title>
let titleMatch = html.match(/<title[^>]*>([^<]+)<\/title>/i);
let title = titleMatch ? titleMatch[1].trim() : "";
// Regex to get <meta name="description" content="..."> or <meta property="og:description" content="...">
let descMatch = html.match(/<meta\s+(?:name=["']description["']|property=["']og:description["'])\s+content=["']([^"']+)["']/i);
let description = descMatch ? descMatch[1].trim() : "";
titles.push([title]);
descriptions.push([description]);
} catch (e) {
// In case of error (fetch fail, etc), leave blank or add error msg
titles.push(["Error fetching"]);
descriptions.push(["Error fetching"]);
}
} else {
// If no URL, add blank cells
titles.push([""]);
descriptions.push([""]);
}
}
// Write the results back to columns C (3rd) and D (4th), starting row 2
sheet.getRange(2, 3, titles.length, 1).setValues(titles);
sheet.getRange(2, 4, descriptions.length, 1).setValues(descriptions);
}
How to use:
Open your Google Sheet.
Click on Extensions > Apps Script.
Delete any existing code and paste the above code.
Save the script.
Go back to your sheet and reload the page.
Run the function fetchMetaTitleAndDescription from the Apps Script editor or create a custom menu/button.
Grant the necessary permissions the first time you run it.
If you want, I can also help you add a custom menu item for ease of use. Just let me know!
function fetchMetaTitleAndDescription() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const lastRow = sheet.getLastRow();
// Read URLs from column B (2nd column), rows 2 to lastRow
const urlRange = sheet.getRange(2, 2, lastRow - 1, 1);
const urls = urlRange.getValues();
const titles = [];
const descriptions = [];
for (let i = 0; i < urls.length; i++) {
let url = urls[i][0];
if (url) {
try {
const response = UrlFetchApp.fetch(url, {muteHttpExceptions: true, timeout: 5000});
const html = response.getContentText();
let titleMatch = html.match(/<title[^>]*>([^<]+)<\/title>/i);
let title = titleMatch ? titleMatch[1].trim() : "";
let descMatch = html.match(/<meta\s+(?:name=["']description["']|property=["']og:description["'])\s+content=["']([^"']+)["']/i);
let description = descMatch ? descMatch[1].trim() : "";
titles.push([title]);
descriptions.push([description]);
} catch (e) {
titles.push(["Error fetching"]);
descriptions.push(["Error fetching"]);
}
} else {
titles.push([""]);
descriptions.push([""]);
}
}
// Write meta title to column C and meta description to column D, starting row 2
sheet.getRange(2, 3, titles.length, 1).setValues(titles);
sheet.getRange(2, 4, descriptions.length, 1).setValues(descriptions);
}