BigMike
BigMike

Reputation: 1093

Google Sheet Cell to execute Google search

I have a Google sheet with a list of companies (Column A) and I'd like to know if both options below can be executed by way of a function within google sheets:

1) Find domain name and put it in Column B

2) Run a 'site:domain.com [search term]' google search and return '1' if successful or '0' if no results.

Edit: Sample Google Sheet.

enter image description here

Any thoughts? Thank you!

Upvotes: 0

Views: 2029

Answers (1)

Aerials
Aerials

Reputation: 4419

You can create a bound Apps script to your sheet. And make a custom function that you can use as a formula in your sheet.

Using the UrlFetchApp.fetch() function you can get the results from any website by making a request to its URL such as https://www.google.com.

Keep in mind, the function will return HTML code, that you will have to parse to extract the string you want from the HTML content returned. for this you can use more regex.

function searchResults(input) {
  var url = "https://duckduckgo.com/?q=what+is+the+website+of+company+"+encodeURIComponent(input);
  var options = {
    'muteHttpExceptions' : true
  };
  var searchResults = UrlFetchApp.fetch(url, options);
  var titleExp = /<h2 class=\"result__title\">([\s\S]*?)<\/h2>/gi;
  var titleResults = searchResults.getContentText().match(titleExp);
  // return the first match
  return titleResults[0];
}


Recommended reading:

Upvotes: 1

Related Questions