1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【コピペ】Qiitaのview数を取得して合計し、スプレッドシートに反映させる方法

Posted at

はじめに

筆者は 大学生限定 プログラミングコミュニティ 『GeekSalon』で活動している者です!

完成イメージ

image.png

実装方法

情報を取得したいスプレッドシートからスクリプトを開き以下をコピペしましょう!

function qiita(){
  const COL_TITLE  = 1;
  const COL_PV     = 2;
  var sheet = SpreadsheetApp.getActiveSheet();
  
  sheet.getRange(1, COL_TITLE).setValue("タイトル");
  sheet.getRange(1, COL_PV).setValue("ビュー");

  const QIITA_TOKEN  = 'あなたのtoken';
  const API_ENDPOINT = 'https://qiita.com/api/v2';
  const API_MY_ITEMS = '/authenticated_user/items';

  var headers = {'Authorization' : 'Bearer ' + QIITA_TOKEN};
  var params = {'headers' : headers};

  var paramstr = "?per_page=100&page=1";
  var res = UrlFetchApp.fetch(API_ENDPOINT + API_MY_ITEMS + paramstr, params);
  var json = JSON.parse(res.getContentText());
  json.forEach(function(item, i){
    sheet.getRange(i + 2, COL_TITLE).setValue(item["title"]);
  });

  const API_ITEM_DTL = '/items';

  json.forEach(function(item, i){
    var resdtl = UrlFetchApp.fetch(API_ENDPOINT + API_ITEM_DTL + "/" + item["id"], params);
    var jsondtl = JSON.parse(resdtl.getContentText());
    sheet.getRange(i + 2, COL_PV).setValue(jsondtl["page_views_count"]);
  });

}
 

Tokenの取得方法

スクリーンショット 2022-07-16 16.25.36.png
スクリーンショット 2022-07-16 16.26.00.png
スクリーンショット 2022-07-16 16.26.39.png

最後に

いかがでしたでしょうか?
不具合等ありましたらご連絡ください!
詳細の解説はまた時間がある時にやろうと思います!今回はとりあえずコピペで使えるところまで!
それではご覧いただきありがとうございました!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?