通過 JavaScript 客戶端 API Auth 使用 Google 電子表格作為“已驗證”數據源的 Google 可視化 API (Google Visualization API using Google Spreadsheet as "Authenticated" Data Source via JavaScript Client API Auth)


問題描述

通過 JavaScript 客戶端 API Auth 使用 Google 電子表格作為“已驗證”數據源的 Google 可視化 API (Google Visualization API using Google Spreadsheet as "Authenticated" Data Source via JavaScript Client API Auth)

The Google Visualization API documentation states the easiest way to use a Google Spreadsheet as a Data Source is to publish it. And under normal circumstances that works great. But I am dynamically creating the Google Spreadsheet using the Drive API. So, even when I am authorized, then create a spreadsheet, I cannot immediately use that spreadsheed as a data source. So I need one of two solutions:

1) Programatically "Publish" the Google Spreadsheet via the Drive API?

2) Authenticate the Query to the Google Visualization API?

The code looks like this:

var url = 'https://spreadsheets.google.com/tq?key=' + idOfSpreadsheet + '&headers=1&pub=1';
var query = new google.visualization.Query(url);
query.send(drawTable);

You would think I could append the auth token to the url varible, but I can't find any documentation on that.  This would be my first choice.

Otherwise, I could publish the spreadsheet but I can't find how to do that from JavaScript. Currently, I have to open the newly created spreadsheet in the Google Drive UI and manually publish it. I step I would not like to have to explain to users how to accomplish with each dynamically created spreadsheet.

Cheers James


參考解法

方法 1:

As you mentioned you could try to append the oauth 2 access token to the url.

After creating your google spreadsheet, you can query its metadata by doing an authorized:

GET https://www.googleapis.com/drive/v2/files/<file_key>

See documentation.

In the ile's metadata you will get the list of possible export formats:

...
"exportLinks": {
  "application/pdf": "https://docs.google.com/...&exportFormat=pdf",
  "application/x-vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/...exportFormat=ods",
  "application/vnd.ms-excel": "https://docs.google.com/...&exportFormat=xls"
},
...

CSV is not listed because it will only allow you to download the first sheet but it is supported so you could use the following URL:

https://docs.google.com/feeds/download/spreadsheets/Export?key=<file_key>&exportFormat=csv

You have to do an authorized request though so you wil need to append your auth token like this:

https://docs.google.com/feeds/download/spreadsheets/Export?key=<file_key>&exportFormat=csv&access_token=<auth_token>

Be aware that the OAuth 2.0 access tokens are only valid for 1h so you need to refresh them eventually.

(by user1541413Nicolas Garnier)

參考文件

  1. Google Visualization API using Google Spreadsheet as "Authenticated" Data Source via JavaScript Client API Auth (CC BY-SA 3.0/4.0)

#google-visualization #google-sheets #google-drive-api






相關問題

帶有坐標的谷歌地理圖表? (Google geocharts with coordinates?)

通過 JavaScript 客戶端 API Auth 使用 Google 電子表格作為“已驗證”數據源的 Google 可視化 API (Google Visualization API using Google Spreadsheet as "Authenticated" Data Source via JavaScript Client API Auth)

在 PHP 中壓縮 JSON 字符串並在 Javascript 中解壓縮以進行 Google API 的數據庫查詢 (Compress JSON string in PHP and decompress in Javascript for Database query for Google API)

用於創建 innerHTML 的 JavaScript 循環(Google 表格) (JavaScript loop to create innerHTML (Google Sheet))

相當於 GViz setVisibleChartRange 的 Dygraphs? (Dygraphs equivalent for GViz setVisibleChartRange?)

避免 Google 圖表中 X 軸的小數點 (Avoid decimal points in X-axis in Google charts)

如何從 Google 的 Visualization API 生成的條形圖中刪除 x 軸? (How do you remove the x-axis from a bar chart produced by Google's Visualization API?)

谷歌圖表 API 加載 (Google Chart API loading)

為 Google Charts Api 編寫自定義格式化程序 (Write a custom formatter for Google Charts Api)

IE7中的谷歌圖表 (Google Chart's in IE7)

谷歌圖表 - 直方圖:刪除項目的水平劃分 (google charts - histograms: remove horizontal division of items)

與 jquery 一起使用時,Google 圖表上的 Vaxis 不顯示 (Vaxis on Google chart not showing when used with jquery)







留言討論