HTML_PAGE Function for Google Sheets

The custom function HTML_PAGE for Google Sheets returns the HTML code of a web page (or the page response code in case of error).

Simply copy and paste the code of the HTML_PAGE function into the script editor to be able to use it later (for more details, check out the Add a Custom Function to Google Sheets page).


Usage Example

The HTML_PAGE function retrieves here the HTML code of the site index:

=HTML_PAGE("https://www.sheets-pratique.com")

Preview:

google sheets function html code website page

Another example with a minimalist web page (which could be that of an API) containing a single word:

=HTML_PAGE("https://www.excel-pratique.com/s/information/internet")

Preview:

google sheets function url api site html page

Google Apps Script Code of the Function

The code of the function to copy-paste into the script editor:

function HTML_PAGE(url) {
  
  // Source: https://www.sheets-pratique.com/en/codes/html-page-function
  
  // Request
  const request = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  
  // Response code
  const code = request.getResponseCode().toString();
  
  // Returns the HTML code of the page (or the response code in case of error)
  return code[0] == 2 ? request.getContentText() : 'Error ' + code;
}

Version Limited to 50,000 Characters

To avoid getting an error in case of exceeding the 50,000 character limit of a cell, you can use this second version of the HTML_PAGE function:

function HTML_PAGE(url) {
  
  // Source: https://www.sheets-pratique.com/en/codes/html-page-function
  
  // Request
  const request = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  
  // Response code
  const code = request.getResponseCode().toString();
  
  // Returns the HTML code of the page (or the response code in case of error)
  return code[0] == 2 ? request.getContentText().slice(0, 50000) : 'Error ' + code;
}