【问题标题】:Edit cell of Google Spreadhseet via API通过 API 编辑 Google 电子表格的单元格
【发布时间】:2015-01-29 08:53:35
【问题描述】:

我正在构建一个 Chrome 扩展程序,它应该编辑 Google 电子表格中的一个单元格。我设法阅读了工作表内容,但无法编辑单元格。目前我的错误是“400(错误请求)”。知道我在这里做错了什么吗?

我已经浏览了 Google Sheets API 文档和此处发布的其他问题,但找不到任何解决方案。

这是我用来获取工作表内容的代码(有效):

function loadSpreadsheet(token) {

    var y = new XMLHttpRequest();
    y.open('GET', 'https://spreadsheets.google.com/feeds/list/1NE8g7kyk-Z6Sci9_w6mdY2KV8542a3TJd9lIbucHZiU/default/private/values?access_token=' + token);
    y.onload = function() {
        console.log(y.response);
    };
    y.send();

}

这是我尝试编辑(PUT)一个新单元格的代码(给我“400 - 错误请求”):

function add_title(message, token) {

    url = 'https://spreadsheets.google.com/feeds/cells/1NE8g7kyk-Z6Sci9_w6mdY2KV8542a3TJd9lIbucHZiU/default/private/full/cell?access_token=' + token;

    function constructAtomXML(foo){
        var atom = ["<?xml version='1.0' encoding='UTF-8'?>",
            '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">',
            '<id>https://spreadsheets.google.com/feeds/cells/1NE8g7kyk-Z6Sci9_w6mdY2KV8542a3TJd9lIbucHZiU/default/private/full/R2C4</id>',
            '<link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/1NE8g7kyk-Z6Sci9_w6mdY2KV8542a3TJd9lIbucHZiU/default/private/full/R2C4"/>',
            '<gs:cell row="2" col="4" inputValue="',foo,'"/>',
            '</entry>'].join('');
        return atom;
    };

    var params = constructAtomXML(message);

    var z = new XMLHttpRequest();
    z.open("PUT", url);
    z.setRequestHeader("Content-type", "application/atom+xml");
    z.setRequestHeader("GData-Version", "3.0");
    z.send(params);

}

【问题讨论】:

  • "params" 对象应该是一个字符串。您只需要传递一个要发送到服务器的值。您是否检查了“var params = constructorAtomXML(message);”中的消息参数类型?还要检查这一行应该是 '' ,foo,'',
  • @gui47 我检查了“params”,它返回一个字符串。我还删除了该行的可变部分以使其成为 '' 但这也不起作用。还有其他想法吗?
  • Update values in google spreadsheets from nodejs - 看起来他们将 OAuth 密钥放在标题中。
  • @eddyparkinson 我尝试通过 'z.setRequestHeader("Authorization", "GoogleLogin Auth=" + token)' 添加令牌,但仍然收到 400 错误。
  • @theGreatDanton 知道什么是黄吗?我假设您在链接问题中发布的代码有效。

标签: ajax google-chrome-extension xmlhttprequest google-spreadsheet-api


【解决方案1】:

在 cmets 的帮助下,我终于得到了它:我必须将 OAuthIf-Match 添加到请求的标头中。

最终的代码应该是这样的:

function add_title(token, message) {

    url = 'https://spreadsheets.google.com/feeds/cells/1NE8g7kyk-Z6Sci9_w6mdY2KV8542a3TJd9lIbucHZiU/default/private/basic/R2C4?access_token=' + token;

    function constructAtomXML(foo){
        var atom = ["<?xml version='1.0' encoding='UTF-8'?>",
            '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">',
            '<id>https://spreadsheets.google.com/feeds/cells/key/private/basic/R2C4</id>',
            '<link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/key/default/private/basic/R2C4"/>',
            '<gs:cell row="2" col="4" inputValue="' + foo + '"/>',
            '</entry>'].join('');
        return atom;
    };

    var params = constructAtomXML(message);

    var z = new XMLHttpRequest();
    z.open("PUT", url);
    z.setRequestHeader("Content-type", "application/atom+xml");
    z.setRequestHeader("GData-Version", "3.0");
    z.setRequestHeader("Authorization", "GoogleLogin auth=" + token);
    z.setRequestHeader("If-Match", "*");
    z.send(params);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-01
    • 2020-03-08
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多