Solution of : =IMPORTHTML() Errors : Could not fetch url

While I'm Importing table from HTML with =Importhtml() built-in function (Formula) an web page I had error : Could not fetch url.







Here's a sample custom function to import any table from web page HTML.
Copy the code into the Tools > Script Editor.

The Code :

function importTable(URL,i) {
//Content
var resp=UrlFetchApp.fetch(URL).getContentText();
//Regex
var regT=/<table[^>]*>([\s\S]*?)<\/table[^>]*>/g;
var regR=/<tr[^>]*>([\s\S]*?)<\/tr[^>]*>/g;
var regC=/<t[^>]*>([\s\S]*?)<\/t[^>]*>/g;
//Get All Tables
var tables=resp.match(regT).map(function(val){
return val.replace(/<\/?table[^>]*>/g,'');
});
//Get Table in rows
var rows=tables[i].match(regR).map(function(val){
return val.replace(/<\/?tr[^>]*>/g,'');
});
//Fill Array
var arr=[];
for(var r in rows){
arr.push([]);
var row=rows[r].match(regC).map(function(val){
return val.replace(/<\/?t[^>]*>/g,'');
});
for(var c in row){
arr[arr.length-1].push(row[c].replace(/<\/?[^>]*>/g,''))
}
}
return arr;
}
view raw importTable.gs hosted with ❤ by GitHub

Sample :


Comments

  1. worked like a champ! thanks for sharing 5/2019

    ReplyDelete
  2. not working showing
    DNS error: http://undefined (line 4, file "Code")Dismiss

    ReplyDelete
    Replies
    1. Hi, Can you send me your code

      Delete
    2. Same problem plz help code is same which you peast

      Delete
  3. i want table from https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-10006&symbol=NIFTY&symbol=NIFTY&instrument=-&date=-&segmentLink=17&symbolCount=2&segmentLink=17
    please help me

    ReplyDelete

Post a Comment

Popular posts from this blog

Email a Custom Sheets as PDF in Attachment

Export and Email Specific Range in Google Sheets in CSV,PDF or XLSX