我一直在做一些研究,但由于某种原因找不到一个很好的例子来说明这一点,我开始怀疑这是否可能。
我想要做的是让我的扩展程序在 Google 电子表格中写入数据,以便该表格用作数据库。
有人有任何我可以遵循的文档吗?考虑到电子表格 API 似乎不允许 JavaScript,这可能吗?
谢谢。
最佳答案
是的,这绝对是可能的。我通过 Javascript 广泛使用了电子表格 API。您需要使用此处记录的 API 协议(protocol)版本:https://developers.google.com/google-apps/spreadsheets/
这需要使用 OAuth2 发送签名请求(旧的身份验证协议(protocol)不再可靠。)因此我建议使用 OAuth2 库,例如 JSO。 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
}
关于javascript - Chrome 扩展程序写入 Google 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450438/