Chrome 扩展写入 Google 电子表格 [英] Chrome Extension Writing to Google Spreadsheet

查看:44
本文介绍了Chrome 扩展写入 Google 电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一些研究,但由于某种原因无法在任何地方找到一个很好的例子来展示这一点,我开始怀疑这是否可能.

I've been doing some research and for some reason can't find a good example showing this anywhere, and I am starting to wonder if it's even possible.

我想要做的是让我的扩展程序在 Google 电子表格中写入数据,以便将表格用作数据库.

What's I'm looking to do is to have my extension write data within a Google Spreadsheet, so that the sheet is being used as a database.

有没有人有任何我可以遵循的文档?考虑到电子表格 API 似乎不允许使用 JavaScript,这可能吗?

Does anyone have any documentation that I could follow through? Considering that the Spreadsheet API doesn't seem to allow JavaScript, is that even possible?

谢谢.

推荐答案

是的,这绝对是可能的.我已经使用 Javascript 广泛使用了电子表格 API.您需要使用此处记录的 API 协议版本:https://developers.google.com/google-apps/spreadsheets/

Yes, it is definitely possible. I have used the Spreadsheet API extensively using Javascript. You'll need to use the Protocol version of the API as documented here: https://developers.google.com/google-apps/spreadsheets/

这需要使用 OAuth2 发送签名请求(旧的身份验证协议不再可靠.)所以我建议使用像 JSO 这样的 OAuth2 库.https://github.com/andreassolberg/jso

This requires sending signed requests using OAuth2 (the older auth protocols aren't really reliable anymore.) so I suggest using an OAuth2 library like JSO. https://github.com/andreassolberg/jso

在编写 JavaScript 时,您需要编写函数来创建 XML 字符串以与协议 API 交互.解析响应非常简单.我已经包含了我使用过的代码片段.您还可以在此处使用 JQuery 查看我对相关问题的回答.JQuery .ajax POST 到电子表格 API?

When writing your javascript you'll need to write functions that create an XML string to interface with the Protocol API. Parsing the responses is pretty straight forward. I've included a snippet of the code I've used. You can also see my answer to a related question using JQuery here. 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
',
          '<gsx:columnTitle>',foo,'</gsx:columnTitle>',//'--END_OF_PART
',
          '</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
}

这篇关于Chrome 扩展写入 Google 电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆