12

我一直在做一些研究,並由於某種原因無法找到一個很好的例子顯示這個地方,我開始懷疑是否有可能。Chrome擴展程序寫入谷歌電子表格

我在做什麼就是讓我的擴展程序在Google Spreadsheet中寫入數據,以便將表單用作數據庫。

有沒有人有任何我可以遵循的文檔?考慮到Spreadsheet API似乎不允許JavaScript,這甚至有可能嗎?

THanks。

+2

太寬?跆拳道,實際上只有1非hacky的方式(OAUTH,xml請求到他們的API) –

+2

這絕對不是太寬泛,我非常需要這個答案。 –

回答

5

我想你有幾個月前有過的同樣的問題。我正在尋找一些圖書館做同樣的事情,但找不到任何,所以我最終創建了一個名爲gsloader。我在這個jiraProgressTracker chrome擴展中使用這個庫。 Chrome擴展程序正在開發中,但gsloader庫已準備好使用。

這是你需要做的。

  1. thishttps://cloud.google.com/console#/project下創建一個google雲項目。要有耐心,這將需要一些時間。
  2. 在「註冊的應用程序」下,請勿刪除「服務帳戶 - 項目」
  3. 在「註冊的應用程序」下,註冊一個新的應用程序,選擇平臺Web應用程序。
  4. 在「API」下,選擇「Drive API」。
  5. 在新創建的程序,粘貼鉻應用程序URL(如鉻擴展://)從應用程序步驟3
  6. 添加創建「網絡起源」
  7. 複製「客戶端ID」從的OAuth 2.0客戶端ID gsloader庫,進入你的html頁面。它需要require.jsjs-logger和jQuery。如果你不能使用requirejs,請告訴我,我將嘗試通過刪除requirejs依賴項來創建庫,儘管它可能需要更多時間才能完成。
  8. 以下是一些代碼片段。
    //完成授權
    var clientId = "<your client id>";
    GSLoader.setClientId(clientId);

    //將現有的電子表格
    GSLoader.loadSpreadsheet("spreadsheet id");

    //創建電子表格
    GSLoader.createSpreadsheet("spreadsheet id")

    有足夠的方法和對象可以一起工作,而不是提所有這裏我會盡力使文檔可用。

請讓我知道,它是如何與您的整體工作。

7

是的,這絕對有可能。我已經廣泛使用Javascript使用Spreadsheet API。您需要使用以下協議版本的API:https://developers.google.com/google-apps/spreadsheets/

這需要使用OAuth2發送簽名請求(舊的認證協議不再可靠。)所以我建議使用像JSO一樣的OAuth2庫。 https://github.com/andreassolberg/jso

在編寫JavaScript時,您需要編寫函數來創建XML字符串以與Protocol API進行接口。解析響應非常簡單。我已經包含了我用過的代碼片段。你也可以在這裏看到我使用JQuery的相關問題的答案。 JQuery .ajax POST to Spreadsheets API?

function appendSpreadsheet(){ 

//Constructs the XML string to interface with the Spreadsheet API. 
//This function adds the value of the param foo to the cell in the first empty row in the column called 'columnTitle'. 
//The Spreadsheet API will return an error if there isn't a column with that title. 
function constructAtomXML(foo){ 
    var atom = ["<?xml version='1.0' encoding='UTF-8'?>", 
      '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">',//'--END_OF_PART\r\n', 
      '<gsx:columnTitle>',foo,'</gsx:columnTitle>',//'--END_OF_PART\r\n', 
      '</entry>'].join(''); 
    return atom; 
}; 

var params = { 
'method': 'POST', 
'headers': { 
    'GData-Version': '3.0', 
    'Content-Type': 'application/atom+xml' 
}, 
'body': constructAtomXML(foo) 
}; 

var docId //Get this from the spreadsheet URL or from the Google Drive API. 
var worksheetId = 'od6'; //The worksheet Id for the first sheet is 'od6' by default. 

url = 'https://spreadsheets.google.com/feeds/list/'+docId+'/'+worksheetId+'/private/full'; 

sendSignedRequest(url, handleSuccess, params); //Use your OAuth2 lib 
} 
+0

如何管理導入https://apis.google.com/js/client.js? – noogui