LoginSignup
0
0

More than 5 years have passed since last update.

nodeでgoogle spreadsheetから値を取得する。

Last updated at Posted at 2019-03-27

公式チュートリアルする

https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit#gid=0
ここの値を取得するテスト。

Step 1: Turn on the Google Sheets API

credentials.jsonをダウンロードしておく
自身の持っているgoogleアカウントの認証情報を取得

Step 2: Install the client library

ライブラリを追加する

npm install googleapis@27 --save

Step 3: Set up the sample

Sampleコードをコピペする
sheets/quickstart/index.js

Step 4: Run the sample

node .

途中で

Authorize this app by visiting this url: https://accounts.google.com/o/oauth2/v2/auth?acce....

と聞かれる。

そのURLをクリックすると、Quickstartアプリへ、Googleスプレットシート表示権限が与えられる。
どういうことじゃ?
許可をクリックすると、トークンが表示される。
トークンを入力すると、spreadsheetの値が表示される。

自身の作ったSpreadsheetを参照する

spreadsheet_idとシート名を自身のspreadsheetに変えれば、動作する。

Spreadsheetの値を更新する

hoge.ts
  var values = [["value1", "value2", "value3"]];
  var body = {
    values: values
  };
  var request = {
    spreadsheetId: "xxxxxxxxxxxx", 
    range: "シート1!B3:D3",
    valueInputOption: "USER_ENTERED",
    resource: body
  };
  const sheets = google.sheets({ version: "v4", auth: oAuth2Client });
  const res = await new Promise((resolve, reject) => {
    sheets.spreadsheets.values.update(request, (err, res) => {
      err ? reject(err) : resolve(res);
    });
  });
0
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
0
0