I want to retrieve a table from the URL of https://s.cafef.vn/screener.aspx#data using VBA. This task is difficult because the table contains JSON data embedded in an html file, but it was so kind of Tanaike, an GAS expert who helped me to create a custom function for Google Apps Scripts. (IMPORTHTML() doesn't work in this webpage structure) The function looks like:
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const html = res.getContentText().match(/var jsonData \=([\S\s\w]+\}\])/);
if (!html) return "No tables. Please confirm URL again.";
const table = JSON.parse(html[1].replace(/\n/g, ""));
const header = ["", "FullName", "Symbol", "CenterName", "ChangePrice", "VonHoa", "ChangeVolume", "EPS", "PE", "Beta", "Price"];
return table.reduce((ar, e, i) => {
const temp = header.map(f => f == "" ? i + 1 : e[f]);
ar.push(temp);
return ar;
}, [header]); }
This function works perfect in Google Sheets environment, but my goal now is to convert it into VBA, or in other words, writing a VBA module which can get the table at https://s.cafef.vn/screener.aspx#data.
Many thanks for any help or suggestions
Cao Doremi