2

I am trying to parse HTML element by class on Google Sites, my code is:

function doGet(){

  var html = UrlFetchApp.fetch ('http://indicadoresdeldia.cl/').getContentText();
  var doc = XmlService.parse(html);
  var html = doc.getRootElement();
  var menu = getElementsByClassName(html, 'span3 utm')[0];
  var output = XmlService.getRawFormat().format(menu);
  return HtmlService.createHtmlOutput(output);

}

Ween i run the code appear the nexte error message ReferenceError: "getElementsByClassName" is not defined.

i am trying to deploy the example for the next page: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html

Any ideas?

THanks in advance for your help.

1

2 Answers 2

1

According to that site, you should directly copy those functions to your project (source code available there) and then call them. That would alleviate each and every one of your problems.

Sign up to request clarification or add additional context in comments.

Comments

0

Source: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html

function getElementsByClassName(element, classToFind) {  
  var data = [];
  var descendants = element.getDescendants();
  descendants.push(element);  
  for(i in descendants) {
    var elt = descendants[i].asElement();
    if(elt != null) {
      var classes = elt.getAttribute('class');
      if(classes != null) {
        classes = classes.getValue();
        if(classes == classToFind) data.push(elt);
        else {
          classes = classes.split(' ');
          for(j in classes) {
            if(classes[j] == classToFind) {
              data.push(elt);
              break;
            }
          }
        }
      }
    }
  }
  return data;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.