javascript Chrome 扩展写入 Google 电子表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20450438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:32:09  来源:igfitidea点击:

Chrome Extension Writing to Google Spreadsheet

javascriptgoogle-chrome-extensiongoogle-spreadsheet-api

提问by Albert

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.

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

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.

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

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?

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

THanks.

谢谢。

回答by Ohhh

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/

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

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

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

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?

在编写 javascript 时,您需要编写创建 XML 字符串以与协议 API 接口的函数。解析响应非常简单。我已经包含了我使用过的代码片段。您还可以在此处使用 JQuery 查看我对相关问题的回答。JQuery .ajax POST 到电子表格 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
}

回答by vkadam

I think you are having same question which I had some months ago. I was looking for some library to do same but couldn't found any so I end up with creating one called gsloader. I am using this library in this jiraProgressTrackerchrome extension. Chrome extension is under development but gsloader library is ready to use.

我想你和我几个月前有同样的问题。我正在寻找一些库来做同样的事情,但找不到任何库,所以我最终创建了一个名为gsloader 的库。我在这个jiraProgressTrackerchrome 扩展中使用了这个库。Chrome 扩展程序正在开发中,但 gsloader 库已准备好使用。

Here is what you need to do.

这是您需要做的。

  1. Create a google cloud project under this, https://cloud.google.com/console#/project. Be patient, it will take some time.
  2. Under "Registered Apps", do not delete the "Service Account - Project".
  3. Under "Registered Apps", register a new app, choose platform web application.
  4. Under "APIs", select "Drive API".
  5. In newly created app, paste your chrome application url(like chrome-extension://) for "web origin"
  6. Copy "client id" from OAuth 2.0 Client ID from app created in step 3
  7. Add gsloaderlibrary, into you html page. It needs require.jsand js-loggerand jQuery. If you can't use requirejs, please let me know I will try to create library by removing requirejs dependency, though it may take more time for me to do it.
  8. Following is some code snippet to go with.
    // Do Authorization
    var clientId = "<your client id>";
    GSLoader.setClientId(clientId);

    // Load existing spreadsheet
    GSLoader.loadSpreadsheet("spreadsheet id");

    // Create spreadsheet
    GSLoader.createSpreadsheet("spreadsheet id")

    There are enough methods and objects available to work with, rather than mentioning all here I will try to make documentation available.

  1. 下创建一个谷歌云项目,https://cloud.google.com/console#/project。请耐心等待,这需要一些时间。
  2. 在“Registered Apps”下,不要删除“Service Account - Project”
  3. 在“Registered Apps”下,注册一个新的应用程序,选择平台网络应用程序。
  4. 在“API”下,选择“Drive API”。
  5. 在新创建的应用程序中,粘贴您的 chrome 应用程序 url(如 chrome-extension://)作为“web origin”
  6. 从步骤 3 中创建的应用程序中的 OAuth 2.0 客户端 ID 复制“客户端 ID”
  7. 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")

    有足够的方法和对象可供使用,而不是在这里提及所有,我将尝试提供文档。

Please let me know, how does it works with you overall.

请让我知道,它对您的整体效果如何。