回答編集履歴

3

コード全文を追加

2022/08/25 10:47

投稿

退会済みユーザー
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ```js
6
6
  let stylesheets = [];
7
- $('link[rel="stylesheet"]').each((index, element) {
7
+ $('link[rel="stylesheet"]').each((index, element) => {
8
8
  stylesheets.push(element.attribs.href)
9
9
  });
10
10
  ```
@@ -22,4 +22,141 @@
22
22
  stylesheets.map((e,i) => obj['link'+i] = e); // 追加
23
23
  r.props = Object.assign({},obj);
24
24
  ```
25
+ ----------
26
+ # 直した後の全文
27
+ ```js
28
+ function checker(){
29
+ /*
30
+ ========================================
31
+ 初期設定
32
+ ========================================
33
+ */
34
+ const sheet = SpreadsheetApp.getActiveSheet(); // 末尾「,」を「;」に修正
35
+ startRow = 2,//情報を置くセルの一番左上の列番号(A = 1...)
36
+ startCol = 6,//情報を置くセルの一番左上の行番号(1 = 1...)
37
+ URLListRow = startRow - 1,//URLリストの配置した列番号
38
+ authInfo = {
39
+ userName : '',//basic認証を行う場合のユーザーネーム
40
+ password : ''//basic認証を行う場合のパスワード
41
+ }
42
+ /**
43
+ * 取得するメタ情報
44
+ * outName => 取得する情報の名前 好きに設定(空は不可)
45
+ * name => name or property属性を設定
46
+ * prop => 上記nameで設定した属性の値を設定
47
+ metaInfo = [
48
+ {
49
+ outName:'keywords',
50
+ name:'name',
51
+ prop:'keywords',
52
+ },
53
+ {
54
+ outName:'desc',
55
+ name:'name',
56
+ prop:'description',
57
+ },
58
+ {
59
+ outName:'og_title',
60
+ name:'property',
61
+ prop:'og:title',
62
+ },
63
+ {
64
+ outName:'og_desc',
65
+ name:'property',
66
+ prop:'og:description',
67
+ },
68
+ {
69
+ outName:'og_url',
70
+ name:'property',
71
+ prop:'og:url',
72
+ },
73
+ ]
74
+ */
75
+
76
+ /*
77
+ ========================================
78
+ 以下 script
79
+ ========================================
80
+ */
81
+
82
+ // clear cell
83
+ for (let i = startRow; i <= sheet.getLastColumn(); i++){
84
+ const dataRange = sheet.getRange(startCol,i,sheet.getLastRow());
85
+ dataRange.clearContent();
86
+ }
87
+
88
+ // create url data
89
+ const urlData = sheet.getRange(startCol,URLListRow,sheet.getLastRow() - 1).getValues();
90
+ let ary = '';
91
+ for (var a = 0; a<urlData.length;a++){
92
+ if(urlData[a][0]){
93
+ ary = ary + String(urlData[a][0]) + ','
94
+ }
95
+ }
96
+ ary = ary.split(',');
97
+ ary.pop();
98
+
99
+
100
+ // header option
101
+ const options = {
102
+ method: "GET",
103
+ headers: {"Authorization" : "Basic " + Utilities.base64Encode(authInfo.userName + ":" + authInfo.password)},
104
+ "muteHttpExceptions" : true,
105
+ "validateHttpsCertificates" : false,
106
+ "followRedirects" : false
107
+ }
108
+ let result = [];
109
+
110
+ // get html data
111
+ for(var i = 0; i < ary.length; i++){
112
+ try {
113
+ let obj = Object.assign({});
114
+ let r = Object.assign({});
115
+ let resultURL = ary[i];
116
+ const res = UrlFetchApp.fetch(resultURL,options);
117
+ const content = res.getContentText()
118
+ const $ = Cheerio.load(content);
119
+ obj.title = $('head title').text();
120
+ obj.canonical = $('link[rel="canonical"]').attr('href'); // 末尾「,」を「;」に修正
121
+ obj.stylesheet = $('link[rel="stylesheet"]').attr('href'); // 末尾「,」を「;」に修正
25
122
 
123
+ let stylesheets = []; // 追加
124
+ $('link[rel="stylesheet"]').each((index, element) => { // 追加
125
+ stylesheets.push(element.attribs.href) // 追加
126
+ }); // 追加
127
+ console.log('stylesheets = ' + stylesheets); // 追加
128
+
129
+ /* metaInfo.forEach((v)=>{
130
+ const reg = new RegExp(v.prop,'g');
131
+ if(content.match(reg)){
132
+ obj[v.outName] = $(`meta[${v.name}="${v.prop}"]`).attr("content");
133
+ }else{
134
+ obj[v.outName] = false;
135
+ }
136
+ }) */
137
+
138
+ stylesheets.map((e,i) => obj['link'+i] = e); // 追加
139
+ r.props = Object.assign({},obj);
140
+
141
+ result.push(r);
142
+
143
+ obj = null;
144
+ r = null;
145
+ //sleep 1秒
146
+ Utilities.sleep(1000);
147
+ } catch(e) {
148
+ // 例外エラー処理
149
+ console.log('Error:'+e);
150
+ }
151
+ }
152
+
153
+ // draw cell
154
+ result.forEach((d,r)=>{
155
+ Object.keys(d.props).forEach((k,i)=>{
156
+ const row = startCol + r;
157
+ const col = startRow + i;
158
+ sheet.getRange(row, col).setValue(d.props[k]);
159
+ })
160
+ })
161
+ }
162
+ ```

2

 

2022/08/24 13:57

投稿

退会済みユーザー
test CHANGED
@@ -1,4 +1,4 @@
1
- > 各ページに読み込んでいるcssのpath(ページによって数が違う)を取得
1
+ 各ページに読み込んでいるcssのpath(ページによって数が違う)を取得
2
2
  // get html data
3
3
  のループの中で
4
4
 
@@ -11,3 +11,15 @@
11
11
  としてみてはいかがでしょうか。
12
12
  (配列stylesheets に cssの複数リンクを格納)
13
13
 
14
+ ② 列方向にリンクを追加
15
+ (①が行われていることを前提として)
16
+ この部分を
17
+ ```js
18
+ r.props = Object.assign({},obj);
19
+ ```
20
+ ↓ このように変えます。
21
+ ```js
22
+ stylesheets.map((e,i) => obj['link'+i] = e); // 追加
23
+ r.props = Object.assign({},obj);
24
+ ```
25
+

1

 

2022/08/24 13:46

投稿

退会済みユーザー
test CHANGED
@@ -1,3 +1,4 @@
1
+ > 各ページに読み込んでいるcssのpath(ページによって数が違う)を取得
1
2
  // get html data
2
3
  のループの中で
3
4