使用 JavaScript 透過 Google Sheets API 存取 Google 試算表

前置作業

首先要取得一個存取 Google Sheets API 的金鑰。步驟如下:

  1. 前往 Google Cloud
  2. 在控制台輸入「Google Sheets API」,並啟用。
  3. 點選「IAM 與管理」頁籤,點選「服務帳戶」頁籤,建立一個服務帳戶。
  4. 點選建立好的服務帳戶,點選「金鑰」頁籤,建立一個 JSON 格式的金鑰。
  5. 建立一個試算表,與服務帳戶共用。

建立專案

建立專案。

1
2
mkdir google-spreadsheet-example
cd google-spreadsheet-example

初始化專案。

1
npm init

安裝依賴套件。

1
npm i google-spreadsheet

新增 index.js 檔。

1
2
3
4
5
6
7
8
9
10
11
12
const { GoogleSpreadsheet } = require('google-spreadsheet');

(async () => {
const spreadsheetId = ''; // required
const sheetID = '0'; // required
const doc = new GoogleSpreadsheet(spreadsheetId);
await doc.useServiceAccountAuth(require('./credentials.json')); // required
await doc.loadInfo();
const sheet = doc.sheetsById[sheetID];
const rows = await sheet.getRows();
console.log(rows);
})();

執行程式。

1
node index.js

程式碼

參考資料