使用 Google Sheets 作為免費資料庫
發佈於 September 15, 2023
申請憑證
-
建立專案 (Google Developers Console連結)
-
開啟API服務
-
開啟服務
-
開啟KEY
-
在雲端中建立新的檔案,並開啟分享新增權限。
使用方式
-
安裝套件
npm install google-spreadsheet
-
撰寫程式碼
// https://docs.google.com/spreadsheets/d/{表單id}/edit#gid=0 const { GoogleSpreadsheet } = require('google-spreadsheet'); const doc = new GoogleSpreadsheet('1a3zxgrlg9Ll1MGte7L_TjOiCj-rAUv1Q76E4sXoFPjo'); async function init(){ const creds = require('./credentials.json'); await doc.useServiceAccountAuth(creds); await doc.loadInfo() const sheet = doc.sheetsByIndex[0]; const larryRow = await sheet.addRow({ time: 'ttt', name: 'ttt' , male: 'ttt' , comment: 'ttt' }); const ff = await doc.addSheet({ headerValues: ['name', 'email'] }); // const rows = await sheet.getRows(); // console.log(rows[0]); } init()