回答編集履歴

2

2021/06/25 09:35

投稿

退会済みユーザー
test CHANGED
@@ -24,7 +24,9 @@
24
24
 
25
25
  どのような結果を得たいのかによりますが、たとえばスコアだけを出すなら下記のようなコードになるかと思います。
26
26
 
27
+
28
+
27
- parsedResult.lighthouseResult.categories.performance.score
29
+ (スコア=下記コードの「parsedResult.lighthouseResult.categories.performance.score」の部分)
28
30
 
29
31
 
30
32
 

1

追加

2021/06/25 09:35

投稿

退会済みユーザー
test CHANGED
@@ -2,9 +2,13 @@
2
2
 
3
3
  &local= → &local**e**=
4
4
 
5
+
6
+
7
+ (追記:urlはv5にする)
8
+
5
9
  ```diff
6
10
 
7
- var url = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=' + url
11
+ var url = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' + url
8
12
 
9
13
  -  + '&key=' + APIkey + '&local=' + locale + '&strategy=' + vStrategy;
10
14
 
@@ -13,3 +17,123 @@
13
17
  +  + '&key=' + APIkey + '&locale=' + locale + '&strategy=' + vStrategy;
14
18
 
15
19
  ```
20
+
21
+
22
+
23
+ 追記:
24
+
25
+ どのような結果を得たいのかによりますが、たとえばスコアだけを出すなら下記のようなコードになるかと思います。
26
+
27
+ parsedResult.lighthouseResult.categories.performance.score
28
+
29
+
30
+
31
+ 返ってくるデータ(レスポンス)の構造はhttps://developers.google.com/speed/docs/insights/v5/reference/pagespeedapi/runpagespeed?hl=ja#response
32
+
33
+ を見てください。
34
+
35
+
36
+
37
+ ```
38
+
39
+ function pageSpeed(vStrategy, vResultOutputCol) {
40
+
41
+ var APIkey = 'APIキー'
42
+
43
+ //シートを扱う準備
44
+
45
+ var myActiveSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
46
+
47
+ var myActiveSheet = myActiveSpreadSheet.getSheetByName('speed');//計測結果を出力するシート名
48
+
49
+ var cStartRow = 3;//計測対象url一覧の開始行
50
+
51
+ var cUrlCol = 3;//計測対象url一覧の列
52
+
53
+
54
+
55
+ var locale = locale || 'ja_JP'; // default is 'en'
56
+
57
+
58
+
59
+ var i = cStartRow;
60
+
61
+
62
+
63
+ while(true){
64
+
65
+ var url = myActiveSheet.getRange(i,cUrlCol).getValue();//URLを取得
66
+
67
+
68
+
69
+ if(url.indexOf('http') == -1){//対象文字列が含まれていない場合
70
+
71
+ if(url === ''){//文字列が空の場合
72
+
73
+ break;//ループ終了
74
+
75
+ }else{
76
+
77
+ i = i + 1;
78
+
79
+ continue;//次のループ
80
+
81
+ }
82
+
83
+ }
84
+
85
+ // v5にする。
86
+
87
+ var url = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' + url +
88
+
89
+ '&key=' + APIkey + '&locale=' + locale + '&strategy=' + vStrategy;
90
+
91
+
92
+
93
+ try {
94
+
95
+ var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true });
96
+
97
+ } catch (err) {
98
+
99
+ Logger.log(err);
100
+
101
+ return(err);
102
+
103
+ }
104
+
105
+ //Logger.log(response.getContentText())
106
+
107
+ var parsedResult = Utilities.jsonParse(response.getContentText());
108
+
109
+
110
+
111
+ Logger.log(parsedResult.lighthouseResult.categories.performance.score);//一応ログを残す
112
+
113
+
114
+
115
+ myActiveSheet.getRange(i,vResultOutputCol).setValue(parsedResult.lighthouseResult.categories.performance.score);//書き込み
116
+
117
+ i = i + 1;
118
+
119
+ }
120
+
121
+ }
122
+
123
+
124
+
125
+ function pageSpeed_PC(){
126
+
127
+ pageSpeed('desktop', 4);
128
+
129
+ }
130
+
131
+
132
+
133
+ function pageSpeed_SP(){
134
+
135
+ pageSpeed('mobile', 5);
136
+
137
+ }
138
+
139
+ ```