2012-01-20 57 views
0

我想通過JavaScript/JQuery在谷歌電子表格中添加一個新行。我不知道如何做到這一點,但這裏是我的嘗試:JQuery .ajax POST到Spreadsheets API?

 var myJSONObject = {"entry": [ 
     "gsx:name": name, 
     "gsx:email": email, 
     "gsx:phone": phone, 
     "gsx:guests": guests 
     ]} 

     var json_uri = "https://spreadsheets.google.com/feeds/list/" + KEY + "/" + id + "/" + SECURE + "/values?alt=json" 

     $.ajax({ 
     type: "POST", 
     url: json_uri, 
     //contentType: 'application/json', 
     data: myJSONobject 
     dataType: 'json', 
     success: function(data){ 
      alert("row added!"); 
     }, 
     error: function(){ 
      alert("error"); 
     } 
    }); 

,但我不斷收到錯誤:(

回答

0

我不知道如何使用jQuery的AJAX API,但你可以嘗試構建一個XML條目下面,如圖所示設置你的參數。

也許別人可以顯示如何使用jQuery的API做呢?

gdocs.constructSpreadAtomXml_ = function(name, email, phone, guests) {  
    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">', 
       '<gsx:name>',name,'</gsx:title>', 
       '<gsx:email>',email,'</gsx:url>', 
       '<gsx:phone>',phone,'</gsx:summary>', 
       '<gsx:guests>',guests,'</gsx:tags>', 
       '</entry>'].join(''); 
    return atom; 
}; 

...

var params = { 
    'method': 'POST', 
    'headers': { 
     'GData-Version': '3.0', 
     'Content-Type': 'application/atom+xml' 
    }, 

    'body': gdocs.constructSpreadAtomXml_(name, email, phone, guests) 
}; 

    var worksheetId = 'od6'; //The first worksheet. 

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

    //sends the params to the background page to get delivered to gDocs 
    oauth.sendSignedRequest(url, handleSuccess, params);