Unable to update values in google spreadsheets from nodejs using the cell based endpoints

China☆狼群 提交于 2019-12-11 20:56:35

问题


I have a google spreadsheet which is both published and available for anyone to edit. I'm able to retrieve the cell-based results, but when I try to update the cell I am unable to do so. I have also tried using the authenticated result but still no luck. Not sure how to proceed here, Any ideas what I am doing wrong.

var https = require('https');
var opt = {
        host: "spreadsheets.google.com",
        path: "/feeds/cells/key/od6/public/full/R2C4",
        method:"PUT"
    };

var request = https.request(opt, function(res){
        res.setEncoding('utf8');
        console.log('the header os '+ JSON.stringify(res.headers));
        var XMLoutput='';
        res.on('data',function(chunk){
            XMLoutput+=chunk;
        });
        res.on('end',function(){
        console.log('the data is  '+ XMLoutput);
        });
    });
    request.on('error',function(e){
        console.log('the error is '+ e);
    });
    request.write('<entry xmlns="http://www.w3.org/2005/Atom"    xmlns:gs="http://schemas.google.com/spreadsheets/2006">'+
                    '<id>https://spreadsheets.google.com/feeds/cells/key/od6/public/full/R2C4</id>'+
                    '<link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/key/od6/public/full/R2C4"/>'+
                    '<gs:cell row="2" col="4" inputValue="someValue"/>'+
                 '</entry>');
    request.end();

回答1:


I finally got it working; I had to use the Authorization key correctly. Hope the below code helps!

var https = require('https');
var opt = {
        host: "spreadsheets.google.com",
        path: "/feeds/cells/key/od6/private/basic/R2C4",
        method:"PUT",
        headers : {"content-type":"application/atom+xml","Authorization":"GoogleLogin Auth=actualAuthkey","GData-Version": "3.0","If-Match": "*"}
    };

var request = https.request(opt, function(res){
        res.setEncoding('utf8');
        console.log('the header os '+ JSON.stringify(res.headers));
        var XMLoutput='';
        res.on('data',function(chunk){
            XMLoutput+=chunk;
        });
        res.on('end',function(){
        console.log('the data is  '+ XMLoutput);
        });
    });
    request.on('error',function(e){
        console.log('the errir is '+ e);
    });
    request.write('<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/od6/private/basic/R2C4"/>'+
                    '<gs:cell row="2" col="4" inputValue="xyz"/>'+
                 '</entry>');
    request.end();


来源:https://stackoverflow.com/questions/26105535/unable-to-update-values-in-google-spreadsheets-from-nodejs-using-the-cell-based

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!