Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

参考ページ

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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away