存取Google試算表

收集到的資料可以丟到Google試算表

前置作業,先到Google API控制台啟用Google Drive API,並建立金鑰,下載存成 client_secret.json

將Google試算表設為共用,並加入共同編輯者email (email在client_secret.json裡)

程式部分就修改試算表連結,金鑰檔需上傳

var GoogleSpreadsheet = require('google-spreadsheet');
var creds = require('./client_secret.json');
 
// Create a document object using the ID of the spreadsheet - obtained from its URL.
var doc = new GoogleSpreadsheet('1YBblQZaEJ90eyRhH7hjZaVGkqH1TXhwq9z9LerUzXRY');

// Authenticate with the Google Spreadsheets API.
doc.useServiceAccountAuth(creds, function (err) {
  // Get all of the rows from the spreadsheet.
  
  doc.getRows(1, function (err, rows) {
    //console.log(rows);
    //console.log(rows.length);
    
    //刪除
    //rows[0].del() // this is asynchronous
    
    //新增
    doc.addRow(1, { 物件: '法國香榭', 坪數: '18.72坪', 總價: '390萬', 格局: '1房1廳1衛', 日期: '2017/07/20' }, function(err) {
      if(err) {
        console.log(err);
      }
    });    
    
  });
});