Created
May 24, 2023 15:46
-
-
Save deepakness/0245fb9b0aea298f72691cf0535d642d to your computer and use it in GitHub Desktop.
Extracts Titles/H1 from URLs right inside Google Sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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