Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // hl_price function to extract share prices for OEIC / UT from Hargreaves Lansdown webpage
- //
- // Created because google IMPORTXML function wasn't loading
- //
- // See the following link for a problem description
- // https://support.google.com/docs/forum/AAAABuH1jm0YMhLzXEfwkI/?hl=en
- //
- // Code obtained from
- // https://stackoverflow.com/questions/39014766/to-exceed-the-importxml-limit-on-google-spreadsheet/48483734#48483734
- //
- // Also see following link for a slightly different approach using xml instead of regex to get the price
- // https://stackoverflow.com/questions/41253339/trouble-with-importxml-on-google-sheets/41275946#41275946
- //
- // todo perhaps need to publish the script so that it can be called from other google sheets?
- function importRegex(url, regexInput) {
- var output = '';
- var fetchedUrl = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
- if (fetchedUrl) {
- var html = fetchedUrl.getContentText();
- if (html.length && regexInput.length) {
- output = html.match(new RegExp(regexInput, 'i'))[1];
- }
- }
- // Grace period to not overload
- // Utilities.sleep(1000);
- return output;
- }
- function hl_price(url) {
- // regex regular expression to extract the bid price
- // HL price looks like this
- // <span class="price-label">Sell:</span><span class="bid price-divide" >30,056.71p</span>
- var bid_price_regex = '<span class="price-label">Sell:</span><span class="bid price-divide" >([0-9,]*\.[0-9]+[0-9]+)p</span>';
- // Grace period to not overload
- Utilities.sleep(100);
- return importRegex(url, bid_price_regex);
- }
- // test function - run with Run->Run function->run_test, then look at View->Logs to see output
- function run_test() {
- url = "https://www.hl.co.uk/funds/fund-discounts,-prices--and--factsheets/search-results/v/vanguard-global-small-cap-index-accumulation"
- Logger.log(url);
- price = hl_price(url);
- Logger.log(price);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement