我正在使用google node api。我正在尝试执行以下操作:
- 阅读 Google Drive 电子表格的内容
- 解释一些数据(我使用node-csv将 csv 转换为对象)
- 然后上传包含新内容的文件
我有可以完成所有这些工作的代码,我会在一分钟内分享它。
问题是当我在谷歌驱动器中创建它时,代码只能从谷歌驱动器读取电子表格文件,当我更新文件时它无法从 API 读取(在谷歌驱动器本身中工作)。所以上面列出的步骤有效,然后当我再次运行它时,更新的文件是“未找到”。请注意,我根本没有更改文件的 id。
这是一些代码,drive.files
正在csv
使用Promises Bluebird 的 promisifyAll()
函数进行处理,该函数将Async
promise 函数添加到库中。
function getDriveFile(drive, id){
return drive.files.getAsync({ fileId: id })
.spread(function(data, response){
console.log(data)
return data.exportLinks['text/csv']
}).then(function(csvDownload){
return request({
"method":"GET",
"url": csvDownload,
"qs":{
"exportFormat": "csv",
"key": id,
"gid": 0
},
"headers":{
"Authorization": "Bearer " + drive._options.auth.credentials.access_token
}
}).spread(function(response, body){
return body
})
})
}
function driveGetAndParse(drive, fileId, fileName){
return getDriveFile(drive, fileId).then(function(file){
if(file.match("<!DOCTYPE html>")) throw new Error(fileName + " is not found")
debug("fetched google drive %s doc", fileName)
return csv.parseAsync(file, {columns: true})
})
}
driveGetAndParse(drive, docId, "My super special doc"),
// one function that updates the file if there's and ID else inserts
function upload(drive, item){
if(item.title) debug("uploading file '%s' to google drive", item.title)
var options = {
addParents: uloadparent,
newRevision: false,
convert: true,
media: {
mimeType: 'text/csv',
body: item.content,
}
}
if(item.id) options.fileId = item.id
if(item.title) options.resourcetitle = item.title
if(options.fileId){
return drive.files.updateAsync(options)
.spread(function(data, response){
return data
})
}else{
return drive.files.insertAsync(options)
.spread(function(data, response){
return data
})
}
}
这两个文件在管理员中的唯一真正区别是上传的文档没有额外的单元格,它只是包含已上传内容的单元格。
如果您认为这是权限问题,对吗?适用于谷歌驱动器,但不适用于 API。我也是这么想的。
需要明确的是,我可以使用drive.files.get
I just can't download the file itself using获取文件信息data.exportLinks['text/csv']
。
我在上传之前和之后检索了文件元数据,可读的可下载文件和不可下载的文件。这是这两个响应之间的差异。
我不确定这是我上传的方式有问题,还是我下载的方式有问题。
有一个下载链接,当我登录谷歌驱动器并将其放到浏览器中时,我可以下载该文件就好了。
尝试:
- 我只是尝试添加
options.media.mimeType = "application/vnd.google-apps.spreadsheet"
更新,认为将现有文档设置为text/csv
. - 尝试了同样的事情,
options.convert = false
认为它正在转换一个已经存在的文档。
更新:
如果我使用谷歌驱动器恢复文件,我可以再次读取文件。进步?文件内容/API 本身一定有问题吗?
代码更改:
我拆分insert
/update
函数并让它们在upload
.
function insert(drive, item){
debug("inserting google drive file")
return drive.files.insertAsync({
addParents: uloadparent,
newRevision: false,
convert: true,
media: {
mimeType: 'text/csv',
body: item.content,
}
}).spread(function(data, response){
return data
})
}
function update(drive, item){
debug("updating google drive file")
return drive.files.updateAsync({
fileId: item.id,
addParents: uloadparent,
newRevision: false,
convert: true,
media: {
mimeType: 'text/csv',
body: item.content,
}
}).spread(function(data, response){
return data
})
}
function upload(drive, item){
if(item.title) debug("uploading %s", item.title)
if(item.id) return update(drive, item)
return insert(drive, item)
}