-1

I need help making this script that would pull the prices off a website and log it into spreadsheets with a date and timestamp, this was:

function pullPrices() {
var page = UrlFetchApp.fetch('http://www.apbcc.com').getContentText();
var number = page.match(/Soya (X Nagpur): ([0-9,]+)/)[1];
SpreadsheetApp.getActive().getSheetByName('Sheet1').appendRow([new Date(), number]);
}

Basically in the webpage they list the prices for maize and soya and I wanted to log it every 5minutes

The output should look like 1/1/2013 11.53 12.50 ( for maize nd soya respectively) However, the script is throwing error upon execution. Could someone please throw some light.


Thanks Harold. Appreciate your prompt response, However the code works fine if it is fed with the text, but if it has to fetch the html content using UrlFetchApp.fetch('http://www.apbcc.com').getContentText(); it doesn't work. Also, in the content that is fetched, the prices are in the next line of the parameters as shown below

<div style="font-size:14px;">
Soya (X Nagpur):&nbsp;
32.20</br>

Maize (WGL):&nbsp;
11.50</br>

Rice Bran Oil (Hyd):&nbsp;
51</br>

Maize (NZB):&nbsp;
11.30</br>

DCP:&nbsp;
29</br>

As on 03/02/2014:&nbsp;
.</br>

Any suggestions ??

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • the web site of the script apbcc.com contain a malware. (chromium said so) – Harold Jan 31 '14 at 13:49
  • Thanks Harold, didn't realize that the site had some problem. However, I have tried to pull the content of the site and copied below is the dump.Using this dump could you please help me build the regex to extract the prices for Maize (WGL)
    Soya (X Nagpur):  32.20 Maize (WGL):  11.55 Rice Bran Oil (Hyd):  51 Maize (NZB):  11.20 DCP:  29 As on 01/02/2014:  .
    – user3256756 Feb 02 '14 at 07:45

1 Answers1

0

try that:

var reg =  /<[^<]+?>/g
var text = '<div style="font-size:14px;"> Soya (X Nagpur):&nbsp; 32.20</br> Maize (WGL):&nbsp; 11.55</br> Rice Bran Oil (Hyd):&nbsp; 51</br> Maize (NZB):&nbsp; 11.20</br> DCP:&nbsp; 29</br> As on 01/02/2014:&nbsp; .</br>';

function testReg(){
  var priceList ={};
  var prices = text.split(reg);
  for(var i in prices){
   var val = prices[i].split(":&nbsp; "); 
   priceList[val[0]]=val[1];
  }
  Logger.log(JSON.stringify(priceList));
  Logger.log("maize: "+priceList[" Maize (WGL)"]);
  Logger.log("soya: "+priceList[" Soya (X Nagpur)"]);
  SpreadsheetApp.getActive().getSheetByName('Sheet1').appendRow([new Date(), priceList[" Soya (X Nagpur)"],priceList[" Maize (WGL)"]]);
}
Harold
  • 3,297
  • 1
  • 18
  • 26