Google Apps Scriptでスクレイピングをし易くするために、フロントエンドの方にお馴染みのDOM機能の一部のようなプログラムを作成。
// XmlServiceを前提としたScrapingお助けProgram
/*
* Method: getElementById
* @param {XmlService.Element} element
* @param {string} idToFind
* @return {XmlService.Element}
* Description: if id does not exist, null will return.
*/
function getElementById(element, idToFind) {
var descendants = element.getDescendants();
for (i in descendants) {
var elmt = descendants[i].asElement();
if (elmt != null){
var id = elmt.getAttribute('id')
if (id != null) {
if (idToFind === id.getValue()){
return elmt;
}
}
}
}
}
/*
* Method: getElementsByClassName
* @param {XmlService.Element} element
* @param {string} classToFind
* @return {Array XmlService.Element[]}
* Description: if id does not exist, empty Array will return.
*/
function getElementsByClassName(element, classToFind){
var classes = [];
var descendants = element.getDescendants();
for (i in descendants) {
var elmt = descendants[i].asElement();
if (elmt != null){
var class = elmt.getAttribute('class');
if (class != null) {
if (classToFind === class.getValue()){
classes.push(elmt.asElement());
}
}
}
}
return classes;
}
/*
* Method: getElementsByTagName
* @param {XmlService.Element} element
* @param {string} tagToFind
* @return {Array XmlService.Element[]}
* Description: if id does not exist, empty Array will return.
*/
function getElementsByTagName(element, tagToFind){
var tags = []
var descendants = element.getDescendants();
for (i in descendants) {
var elmt = descendants[i].asElement();
if (elmt != null) {
var tag = elmt.getName();
if (tag != null) {
if (tagToFind === tag) {
tags.push(elmt);
}
}
}
}
return tags;
}