如何使用Google表格API + Javascript阅读电子表格 [英] How to read a spreadsheet using Google Sheets API + Javascript

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

问题描述

使用 Drive REST API

更新Google电子表格有一个很好的解决方案, a>在这个问题中。



它会覆盖当前的内容。我想阅读当前内容,修改它们,然后更新。

如何在当前的电子表格中阅读?

解决方案

(假设你的凭证是有序的并且你被授权了):


$ b $ function getFileInfo(fileId,callback){
try {
gapi.client.drive.files.get({
fileId:fileId,
fields:appProperties,capabilities,contentHints,createdTime,description,displayedTrashed,fileExtension,folderColorRgb,fullFileExtension,headRevisionId, iconLink,ID,imageMediaMetadata,isAppAuthorized,善良,lastModifyingUser,md5Checksum,mime类型,modifiedByMeTime,modifiedTime,名称,originalFilename,ownedByMe,车主,父母,权限,属性,quotaBytesUsed,共享,sharedWithMeTime,sharingUser,大小,空间,主演,thumbnailLink,垃圾邮件,版本,videoMediaMetadata,viewingByMe,viewingByMeTime,查看者可以拷贝内容,webContentLink,webViewLink,writersCanShare,
})。then(function(response){
info = JSON.parse(r esponse.body);
callback(info);
))
} catch(err){
console.log('Something bad happened:'+ err);
}
}

它返回所有已知的元数据字段(截至目前为止)。您需要将列表修剪为您需要的列表。但它不会返回实际的电子表格内容。为此,您需要使用导出 api:

 
函数getSpreadsheet(fileId,callback){
var spreadsheet;
try {
gapi.client.drive.files.export({
fileId:fileId,
mimeType:'text / csv'
})。then(function (response){
spreadsheet = Papa.parse(response.body).data;
callback(spreadsheet);
})
} catch(err){
console.log('发生了什么坏事:'+ err);
}
}

数据以csv字符串形式返回。我们使用 Papa.parse 将它变成2D数组。


There's a nice solution to updating Google spreadsheets using the Drive REST API in this question.

It overwrites the current contents. I'd like to read in the current contents, modify them, then update.

How do I read in the current spreadsheet?

解决方案

This works (assuming your credentials are in order and you are authorized):

function getFileInfo(fileId, callback) {
    try {
        gapi.client.drive.files.get({
            fileId: fileId,
            fields: "appProperties,capabilities,contentHints,createdTime,description,explicitlyTrashed,fileExtension,folderColorRgb,fullFileExtension,headRevisionId,iconLink,id,imageMediaMetadata,isAppAuthorized,kind,lastModifyingUser,md5Checksum,mimeType,modifiedByMeTime,modifiedTime,name,originalFilename,ownedByMe,owners,parents,permissions,properties,quotaBytesUsed,shared,sharedWithMeTime,sharingUser,size,spaces,starred,thumbnailLink,trashed,version,videoMediaMetadata,viewedByMe,viewedByMeTime,viewersCanCopyContent,webContentLink,webViewLink,writersCanShare",
        }).then(function(response) {
            info = JSON.parse(response.body);
            callback(info);
        })
    } catch (err) {
        console.log('Something bad happened: ' + err);
    }
}

It returns all the known metadata fields (as of this time). You'll want to trim back the list to those you need. But it does not return the actual spreadsheet contents. For that, you'll need to use the export api:

function getSpreadsheet(fileId, callback) {
    var spreadsheet;
    try {
        gapi.client.drive.files.export({
            fileId: fileId,
            mimeType: 'text/csv'
        }).then(function(response) {
            spreadsheet = Papa.parse(response.body).data;
            callback(spreadsheet);
        })
    } catch (err) {
        console.log('Something bad happened: ' + err);
    }
}

The data is return as a csv string. We use Papa.parse to turn it into a 2D array.

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

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