努力載入中
Ruby 透過 Google API 串接 Excel (SpreadSheet)

Ruby 透過 Google API 串接 Excel (SpreadSheet)

2024-01-17

文章目錄

本文最後更新日期 2024/1/17。

  • google API 憑證申請:參考 IT 邦幫忙 ,取得下載後的憑證 (json 檔案)
  • 雲端檔案如果是 xlsx 的話,必須先轉換為 google 試算表格式。 (參考以下截圖)
  • # Gemfile
    gem 'googleauth'
    gem 'google-apis-sheets_v4'
    
    client = Google::Apis::SheetsV4::SheetsService.new
    cred_file = '/tmp/secret.json'
    authorizer = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: File.open(cred_file), scope: [Google::Apis::SheetsV4::AUTH_SPREADSHEETS])
    client.authorization = authorizer
    
    1. cred_file 為準備工作所下載的憑證檔案
    2. scope 可參考官方資料 (搜尋 "AUTH_")