Skip to content

Instantly share code, notes, and snippets.

@deepakness
Created May 24, 2023 15:46
Show Gist options
  • Save deepakness/0245fb9b0aea298f72691cf0535d642d to your computer and use it in GitHub Desktop.
Save deepakness/0245fb9b0aea298f72691cf0535d642d to your computer and use it in GitHub Desktop.
Extracts Titles/H1 from URLs right inside Google Sheets
function getTitleOrH1(url) {
var response = UrlFetchApp.fetch(url,
{
method: "get",
muteHttpExceptions: true
});
var html = response.getContentText();
var titleMatch = html.match("<title>(.*?)</title>");
var h1Match = html.match("<h1[^>]*>(.*?)</h1>");
if (titleMatch) {
return titleMatch[1];
} else if (h1Match) {
return h1Match[1];
} else {
return "No title or H1 found";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment