0

I wonder how can I execute the code faster or bypass the runtime limit. Here is a sample. Among the other elements are rating and number of ratings, Coupon, Product Description. I've repeatedly used the loop over each element so I guess that's what slowed the script way down.

function priceScraper(){

  var apiKey= SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings").getRange(1,2).getValue();

  var scraperSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Scraper")
  var lrow=scraperSheet.getLastRow();

    for(var i= 2; i<=lrow; i++){
    var url = "http://api.scraperapi.com?api_key=" + apiKey + "&url=https://www.amazon.com/dp/"+scraperSheet.getRange(i, 1).getValue()

    var regEx = /<span aria-hidden="true".*<\/span>/gi
    //<span aria-hidden="true">$11.80</span>
      
    var getContent = UrlFetchApp.fetch(url).getContentText().trim();
    var price= getContent.match(regEx);
    price=price[0];
    price = price.replace('<span aria-hidden="true"><span class="a-price-symbol">', "").replace('<span aria-hidden="true">', "").replace('</span><span class="a-price-whole">', "").replace('<span class="a-price-decimal">', "").replace('</span></span><span class="a-price-fraction">',"").replace('</span></span></span>',"").replace("</span></span>", "")

    scraperSheet.getRange(i,2).setValue(price)
}

I know that the code can be shortened but I don't know how.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • The use of getRange in a loop is what is one of the things that is slowing your script. See the suggested originals. – Rubén Oct 07 '22 at 19:34
  • Unfortunately, i only got the first sentence of your answer. What is "suggested originals"? Thank you! – dumdumforgumgum Oct 07 '22 at 19:56
  • Below te question title there should be a notice box with links to two questions already answered. – Rubén Oct 07 '22 at 20:01
  • 1
    See [tag info page](https://stackoverflow.com/tags/google-apps-script/info) for official documentation, free resources and more details. – TheMaster Oct 07 '22 at 21:34
  • I would just do it in batches. But you could get the reads from an array and put the writes into another array and do them later. But the reality is that fetch is the problem so I'd probably just do it in batch and keep track of where you left off. – Cooper Oct 08 '22 at 06:25

0 Answers0