├── LICENSE
├── README.md
└── parser.js
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 tadaken3
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # HTML/XML parser for Google Apps Script
2 |
3 | parser is HTML/XML parser for Google Apps Script.
4 |
5 | ## Install
6 |
7 | Script ID :`1Jrnqmfa6dNvBTzIgTeilzdo6zk0aUUhcXwLlQEbtkhaRR-fi5eAf4tBJ`
8 |
9 | ## Example
10 | ```javascript
11 | var src = ''
12 | + ' Anime Japan Expo'
13 | + ' '
14 | + ' Do you like Anime?'
15 | + ' '
16 | + '';
17 |
18 | function parseXML() {
19 | var doc = XmlService.parse(src),
20 | xml = doc.getRootElement(),
21 | title = parser.getElementById(xml, 'doc-title');
22 | Logger.log(title.getValue());
23 | }
24 |
25 |
26 | function parseXMLByClassName() {
27 | var doc = XmlService.parse(src),
28 | xml = doc.getRootElement(),
29 | paragraph = parser.getElementsByClassName(xml, 'paragraph');
30 | Logger.log(paragraph[0].getValue());
31 | }
32 |
33 | function parseXMLByTagName() {
34 |
35 | var doc = XmlService.parse(src),
36 | xml = doc.getRootElement(),
37 | title = parser.getElementsByTagName(xml, 'title');
38 | Logger.log(title[0].getValue());
39 | }
40 | ```
41 |
42 | ## Contribution
43 |
44 | 1. Fork ([https://github.com/tadaken3/html-parser-gas](https://github.com/tadaken3/html-parser-gas))
45 | 2. Create a feature branch
46 | 3. Commit your changes
47 | 4. Rebase your local changes against the master branch
48 | 5. Create new Pull Request
49 |
50 | ## Licence
51 |
52 | [MIT](https://github.com/tadaken3/html-parser-gas/blob/master/LICENSE)
53 |
54 | ## Author
55 |
56 | [tadaken3](https://github.com/tadaken3)
57 |
--------------------------------------------------------------------------------
/parser.js:
--------------------------------------------------------------------------------
1 | /**
2 | * @param {string} element 検索する要素を指定します
3 | * @param {string} idToFind idを指定します
4 | * @return {string} element 対象の要素を返します
5 | */
6 |
7 | function getElementById(element, idToFind) {
8 | var descendants = element.getDescendants();
9 | for (var i in descendants) {
10 | var elem = descendants[i].asElement();
11 | if ( elem != null) {
12 | var id = elem.getAttribute('id');
13 | if ( id != null && id.getValue() == idToFind) return elem;
14 | }
15 | }
16 | }
17 |
18 |
19 | /**
20 | * @param {string} element 検索する要素を指定します
21 | * @param {string} classToFind クラス名を指定します
22 | * @return {string} element 対象の要素を返します
23 | */
24 |
25 | function getElementsByClassName(element, classToFind) {
26 | var data = [], descendants = element.getDescendants();
27 | descendants.push(element);
28 | for (var i in descendants) {
29 | var elem = descendants[i].asElement();
30 | if (elem != null) {
31 | var classes = elem.getAttribute('class');
32 | if (classes != null) {
33 | classes = classes.getValue();
34 | if (classes == classToFind) {
35 | data.push(elem);
36 | } else {
37 | classes = classes.split(' ');
38 | for (var j in classes) {
39 | if (classes[j] == classToFind) {
40 | data.push(elem);
41 | break;
42 | }
43 | }
44 | }
45 | }
46 | }
47 | }
48 | return data;
49 | }
50 |
51 | /**
52 | * @param {string} element 検索する要素を指定します
53 | * @param {string} tagName タグを指定します
54 | * @return {string} element 対象の要素を返します
55 | */
56 |
57 | function getElementsByTagName(element, tagName) {
58 | var data = [], descendants = element.getDescendants();
59 | for(var i in descendants) {
60 | var elem = descendants[i].asElement();
61 | if ( elem != null && elem.getName() == tagName) data.push(elem);
62 | }
63 | return data;
64 | }
65 |
66 |
67 | /**
68 | 以下テストコードです。
69 | */
70 |
71 | var testData =
72 | ''
73 | + ' Anime Japan Expo'
74 | + ' '
75 | + ' Do you like Anime?'
76 | + ' '
77 | + '';
78 |
79 | function testParseXMLById_() {
80 | var doc = XmlService.parse(testData),
81 | xml = doc.getRootElement(),
82 | title = getElementById(xml, 'doc-title');
83 | if (title === "Anime Japan Expo"){
84 | Logger.log("getById is OK");
85 | }else{
86 | Logger.log("getById is BAD");
87 | }
88 | }
89 |
90 | function testParseByClassName_() {
91 | var doc = XmlService.parse(testData),
92 | xml = doc.getRootElement(),
93 | paragraph = getElementsByClassName(xml, 'paragraph');
94 | paragraph = paragraph[0].getValue();
95 | if (paragraph === "Do you like Anime?"){
96 | Logger.log("getByClassName is OK");
97 | }else{
98 | Logger.log("getByClassName is BAD");
99 | }
100 |
101 | }
102 |
103 | function testParseXMLByTagName_() {
104 | var doc = XmlService.parse(testData),
105 | xml = doc.getRootElement(),
106 | title = getElementsByTagName(xml, 'title');
107 | title = title[0].getValue();
108 | if (title === "Anime Japan Expo"){
109 | Logger.log("getById is OK");
110 | }else{
111 | Logger.log("getById is BAD");
112 | }
113 | }
114 |
--------------------------------------------------------------------------------