12
14

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 5 years have passed since last update.

node-google-spreadsheetを使う(超簡易的なテスト)

Last updated at Posted at 2017-02-22

##参考ページ

Google Apps API Keyの取得
http://www.yoheim.net/blog.php?q=20160411

##取得するシート
https://docs.google.com/spreadsheets/d/1Z2fHUIDDwX_sR6Az3wdfp4wvZNim-0wNzaTzWghchic/edit?usp=sharing

こんな感じ↓
SnapCrab_NoName_2017-1-20_14-3-51_No-00.png
※限定共有

##使用したパッケージ

google-spreadsheet
https://www.npmjs.com/package/google-spreadsheet

##Google Apps API Keyの取得

上記リンクを参考に取得しました。

サービスアカウントキーの作成から「App Engin default service account」を選択
最終的に「{Google APIsのプロジェクト名}-{hash}.json」という.jsonファイルが取得できます

##サンプル

index.js
var GoogleSpreadsheet = require('google-spreadsheet');

var test_sheet = new GoogleSpreadsheet('1Z2fHUIDDwX_sR6Az3wdfp4wvZNim-0wNzaTzWghchic');
var credentials = require('./{Google APIsのプロジェクト名}-{hash}.json');

var sheet;
test_sheet.useServiceAccountAuth(credentials, function(err){
    test_sheet.getInfo(function(err, data){
        sheet = data; //あとから使えるように外部スコープに保存
        for(var i in sheet.worksheets) {
            if(sheet.worksheets[i].title === 'sheet1') {
                sheet.worksheets[i].getRows(function(err, rows) {
                    for(var i in rows) {
                        console.log(rows[i].id);
                        console.log(rows[i].name);
                        console.log(rows[i].age);
                    }
                });
            }
        }
    });

});

出力
F:\Work\node-spreadsheet>node index.js
0
tanaka
18
1
ishii
20
2
kato
45
3
kitamura
64
4
hosono
3
5
noda
50
12
14
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
12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?