1

May be this question already asked, but that won't solve my problem.

I try to save data's into google spreadsheet using google app script. But it shows Exceeded memory limit error.

following my code:

//new
function getNewTitle() {
    var url = "https://www.reddit.com/r/DigitalMarketing.rss?limit=100&after=0";
    var fromText = '</updated><title>';
    var toText = '</title>';
    var content = UrlFetchApp.fetch(url).getContentText();

    var scraped = Parser.data(content).from(fromText).to(toText).iterate();
    return scraped;
}

function getNewContent() {
    var url = "https://www.reddit.com/r/DigitalMarketing.rss?limit=10&after=0";
    var content = UrlFetchApp.fetch(url).getContentText();

    var document = XmlService.parse(content);
    var root = document.getRootElement();
    var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
    Logger.log(atom);

    var fromText = '<content type="html">&lt;!-- SC_OFF --&gt;&lt;div class=&quot;md&quot;&gt;&lt;p&gt;';
    var toText = '</div>';


    var scraped = Parser.data(content).from(fromText).to(toText).iterate();

    return scraped;
}

function getNewLink() {
    var url = "https://www.reddit.com/r/DigitalMarketing.rss?limit=10&after=0";
    var fromText = '<link href="';
    var toText = '" /><updated>';
    var content = UrlFetchApp.fetch(url).getContentText();

    var scraped = Parser.data(content).from(fromText).to(toText).iterate();

    return scraped;
}

function SAVE_DATA() {
  var sheet  = SpreadsheetApp.openById('1No3m_FnhyxIaxj2zSlbHrg8HLBJULGQ2bda65hpKlyY').getSheetByName('sample'); 
 var content   = getNewContent();
  var title   = getNewTitle();
  var link   = getNewLink();
  Logger.log(title[1]);
  for(var i =0; i < title.length; i++) { 
    sheet.appendRow([ 'Reddit','wordpress', title[i], link[i], content[i]]);
  }
}
//new

In my above code am tried to save the data from url.

But i get Exceeded memory limit error.

In my Log i got this message

[18-07-21 05:33:29:719 PDT] [Namespace: prefix "" is mapped to URI "http://www.w3.org/2005/Atom"]

Please help me to fix this error...!

Thanks in advance.

Ramesh S
  • 841
  • 3
  • 15
  • 35
  • Where is this `Parser` – TheMaster Jul 21 '18 at 11:56
  • @I'-'I Actually i added `parser` in `Resources -> Library ` – Ramesh S Jul 21 '18 at 12:03
  • Sorry this is the 1st time am using `google app script`, i don't know how to solve this issue – Ramesh S Jul 21 '18 at 12:05
  • Have to see what's going on inside `Parser` too. But,For starters, create a output array and Use `setValues()` instead of `appendRow()`. Also share your logs and execution transcripts. Was there any line written on the spreadsheet? – TheMaster Jul 21 '18 at 12:09
  • @I'-'I inside Parser i got this https://ibb.co/iVggTd – Ramesh S Jul 21 '18 at 12:13
  • You didn't answer the rest of my questions. Especially [Edit] to include logs and execution transcripts,so that others might also help. Also know that there are [quotas](https://developers.google.com/apps-script/guides/services/quotas#current_limitations): Specifically URLFetch has 50MB/call. Try to debug and include all debugging details in the question. – TheMaster Jul 21 '18 at 12:28
  • Sounds like you need to work on a smaller link. – tehhowch Jul 21 '18 at 12:31
  • in my log i got this [18-07-21 05:21:04:644 PDT] [Namespace: prefix "" is mapped to URI "http://www.w3.org/2005/Atom"] – Ramesh S Jul 21 '18 at 12:32
  • @I'-'I can i share my screen ? – Ramesh S Jul 21 '18 at 12:34
  • @I'-'I question updated ..! – Ramesh S Jul 21 '18 at 12:36
  • Yes post your screen in the question. Execution transcript too(should be in view). Post complete logs. – TheMaster Jul 21 '18 at 12:52
  • 1
    Have you tried reducing limit to `5` from `100` – TheMaster Jul 21 '18 at 12:53
  • @I'-'I I have tried with reducing limit to 5 from 100. But unfortunately its not working.. Shall I share my screen. – Ramesh S Jul 21 '18 at 13:10
  • 1
    @I'-'I Parser is a GAS library. You can see this information at [here](https://www.kutil.org/2016/01/easy-data-scrapping-with-google-apps.html). – Tanaike Jul 22 '18 at 00:20

1 Answers1

2

I think that the reason of the error is that </div> of var toText = '</div>'; is not included in content retrieved from https://www.reddit.com/r/DigitalMarketing.rss?limit=10&after=0. So how about this modification?

Modification points :

  • </div> of var toText = '</div>'; is not included in content. So in this modification, I used </content>. Because you are using '<content type="html">&lt;!-- SC_OFF --&gt;&lt;div class=&quot;md&quot;&gt;&lt;p&gt;' for fromText.
  • setValues() instead of appendRow() is used for putting the values.
    • You can see the difference of the cost between setValues() and appendRow() at here.

Modified script :

1. For getNewContent()

Please modify from

From :
var toText = '</div>';
To :
var toText = '</content>';

2. For SAVE_DATA()

Please modify as follows.

function SAVE_DATA() {
  var sheet  = SpreadsheetApp.openById('1No3m_FnhyxIaxj2zSlbHrg8HLBJULGQ2bda65hpKlyY').getSheetByName('sample');
  var content = getNewContent();
  var title   = getNewTitle();
  var link   = getNewLink();
  var values = title.map(function(e, i){return [e, link[i], content[i]]});
  sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}

Note :

  • In this modification, I used var toText = '</content>'; for getNewContent(). If you want to retrieve other range of the site, please modify this.
  • About the URL, limit=100 for the title is set. But limit=10 is set for the link and content. So when the values are retrieved and put them to Spreadsheet, link and content become undefined from 11 row.
    • If you have already known this, please ignore this.

Reference :

If I misunderstand your question, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165