如何使用Google表格API + Javascript更新电子表格 [英] How to update spreadsheet using Google Sheets API + Javascript

查看:139
本文介绍了如何使用Google表格API + Javascript更新电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型的JavaScript网站,可以从我的Google电子表格中获取数据,并将这些数据显示给最终用户。我也想最终更新这个电子表格(例如插入一些行),但我完全坚持它。



要从工作表检索数据,我使用此文档。它还包含有关更新电子表格的一些信息,但我不确定这是否有效。
我试过了:


  1. jQuery $ .post(...)按照文档中的描述创建一个POST请求,但没有成功)

  2. 使用创建请求邮递员应用程序 - 获取找不到页面响应

任何人都可以帮我解决这个问题吗? :'(


解决方案

使用 JavaScript ,您需要使用 Drive REST API



以下是更新电子表格的代码

  function updateFile(fileId,fileMetadata,fileData,callback){
const boundary ='------- 314159265358979323846';
const delimiter =\r\\ \\ n--+ boundary +\r\\\
;
const close_delim =\r\\\
--+ boundary + - ;

var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e){
var contentType = fileData.type ||'application / octet-stream ';
var base64Data = btoa(reader.result);
var multipartRequestBody =
del imiter +
'Content-Type:application / json\r\\\
\r\''+
JSON.stringify(fileMetadata)+
delimiter +
' Content-Type:'+ contentType +'\r\\\
'+
'Content-Transfer-Encoding:base64 \\\\''+
'\r\\\
'+
base64Data +
close_delim;

var request = gapi.client.request({$ b $'path':'/ upload / drive / v3 / files /'+ fileId,$ b $'method':'PATCH ',
'params':{$ b $'uploadType':'multipart',
'alt':'json'
},
'headers':{
'Content-Type':'multipart / mixed; boundary =''+ boundary +'''
},
'body':multipartRequestBody
});
if(!callback){
callback = function(file){
console.log(file)
};
}
request.execute(callback);




$ b您可以使用以下代码调用 updateFile 函数。

  var blob = new Blob(['Cell Text 1,Cell Text 2 '],{的contentType:' 文本/无格式}); 
updateFile(SHEET_ID,'',blob,function(){
alert(Updated document);
})


I have a small javascript site that gets data from my google spreadsheet and shows this data to the end user. I also want to eventually update this spreadsheet (e.g. insert some rows), but I'm totally stuck with it.

To retrieve data from the sheet I use this document. It also contains some information about updating spreadsheet, but I'm not sure if this works. I tried:

  1. jQuery $.post(...) to create a POST request as described in the document but without success)
  2. Create request using the Postman app - getting "Page not found" response

Can anyone help me with this problem? :'(

解决方案

For updating Google Spreadsheet using JavaScript, you need to use Drive REST API

Following is the code for updating spreadsheet

function updateFile(fileId, fileMetadata, fileData, callback) {
    const boundary = '-------314159265358979323846';
    const delimiter = "\r\n--" + boundary + "\r\n";
    const close_delim = "\r\n--" + boundary + "--";

    var reader = new FileReader();
    reader.readAsBinaryString(fileData);
    reader.onload = function(e) {
        var contentType = fileData.type || 'application/octet-stream';
        var base64Data = btoa(reader.result);
        var multipartRequestBody =
            delimiter +
            'Content-Type: application/json\r\n\r\n' +
            JSON.stringify(fileMetadata) +
            delimiter +
            'Content-Type: ' + contentType + '\r\n' +
            'Content-Transfer-Encoding: base64\r\n' +
            '\r\n' +
            base64Data +
            close_delim;

        var request = gapi.client.request({
            'path': '/upload/drive/v3/files/' + fileId,
            'method': 'PATCH',
            'params': {
                'uploadType': 'multipart',
                'alt': 'json'
            },
            'headers': {
                'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
            },
            'body': multipartRequestBody
        });
        if (!callback) {
            callback = function(file) {
                console.log(file)
            };
        }
        request.execute(callback);
    }
}

You can use following code to call updateFile function.

var blob = new Blob(['Cell Text 1,Cell Text 2'],{contentType:'text/plain'});
updateFile(SHEET_ID,'',blob,function(){
    alert("Updated document");
})

这篇关于如何使用Google表格API + Javascript更新电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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