Google的試算表是一個很好存放資料的地方,許多人收集訂單資訊或是收集問卷資料,都會使用Google表單來做,而Google表單的資料也可以存進Google的試算表,如果手邊沒有資料庫可儲存資料,利用現成的Google試算表倒是一個不錯的方案。LineBot和使用者的對話,如果也能善用試算表,將會有許多的應用可以產生。但LineBot要能和Google試算表的API溝通,便要先經過許多的認證,取得Google的憑證及權杖,申請的過程有點繁雜,步驟如下:
1、登入google後進入啟用的網站,https://console.developers.google.com/start/api?id=sheets.googleapis.com
安裝完這二個模組後,package.json應該會長成這個樣子
{ "name": "bottest", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node ." }, "author": "", "license": "ISC", "dependencies": { "express": "^4.15.3", "google-auth-library": "^0.10.0", "googleapis": "^19.0.0", "linebot": "^1.3.0" }}var fs = require('fs');var readline = require('readline');var google = require('googleapis');var googleAuth = require('google-auth-library');// If modifying these scopes, delete your previously saved credentials// at ~./sheetsapi.jsonvar SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];var TOKEN_DIR = './';var TOKEN_PATH = TOKEN_DIR + 'sheetsapi.json';// Load client secrets from a local file.fs.readFile('client_secret.json', function processClientSecrets(err, content) { if (err) { console.log('Error loading client secret file: ' + err); return; } // Authorize a client with the loaded credentials, then call the // Google Sheets API. authorize(JSON.parse(content), listMajors);});/** * Create an OAuth2 client with the given credentials, and then execute the * given callback function. * * @param {Object} credentials The authorization client credentials. * @param {function} callback The callback to call with the authorized client. */function authorize(credentials, callback) { var clientSecret = credentials.installed.client_secret; var clientId = credentials.installed.client_id; var redirectUrl = credentials.installed.redirect_uris[0]; var auth = new googleAuth(); var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); // Check if we have previously stored a token. fs.readFile(TOKEN_PATH, function(err, token) { if (err) { getNewToken(oauth2Client, callback); } else { oauth2Client.credentials = JSON.parse(token); callback(oauth2Client); } });}/** * Get and store new token after prompting for user authorization, and then * execute the given callback with the authorized OAuth2 client. * * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for. * @param {getEventsCallback} callback The callback to call with the authorized * client. */function getNewToken(oauth2Client, callback) { var authUrl = oauth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPES }); console.log('Authorize this app by visiting this url: ', authUrl); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Enter the code from that page here: ', function(code) { rl.close(); oauth2Client.getToken(code, function(err, token) { if (err) { console.log('Error while trying to retrieve access token', err); return; } oauth2Client.credentials = token; storeToken(token); callback(oauth2Client); }); });}/** * Store token to disk be used in later program executions. * * @param {Object} token The token to store to disk. */function storeToken(token) { try { fs.mkdirSync(TOKEN_DIR); } catch (err) { if (err.code != 'EEXIST') { throw err; } } fs.writeFile(TOKEN_PATH, JSON.stringify(token)); console.log('Token stored to ' + TOKEN_PATH);}/** * Print the names and majors of students in a sample spreadsheet: * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit */function listMajors(auth) { var sheets = google.sheets('v4'); sheets.spreadsheets.values.get({ auth: auth, spreadsheetId: '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms', range: 'Class Data!A2:E', }, function(err, response) { if (err) { console.log('The API returned an error: ' + err); return; } var rows = response.values; if (rows.length == 0) { console.log('No data found.'); } else { console.log('Name, Major:'); for (var i = 0; i < rows.length; i++) { var row = rows[i]; // Print columns A and E, which correspond to indices 0 and 4. console.log('%s, %s', row[0], row[4]); } } });}附註:日後如果還要管理自己的Google API憑證,請由https://console.developers.google.com/登入後,便可以看到這次申請的憑證