node.js - 使用 google drive.files.copy 时设置新文件名
问题描述
我正在使用谷歌驱动器 apiv3 将电子表格复制到谷歌驱动器上的文件夹中。文档说明“名称”属性应该用于设置新工作表的标题。我正在使用它,但新创建的工作表名为“{titleOfSheetToCopy} 的副本”。
要求:
app.get('/new', function (req, res)
{
var drive = google.drive({version:'v3', auth: jwt_client});
var copyRequest = {
name: 'im a copy',
fileId: 'idOfSheetToClone',
parents: ['idOfDestinationFolder'],
}
drive.files.copy(copyRequest, function(err, response){
if(err){
console.log(err);
res.send('error');
return;
}
res.send(response);
});
});
回复:
{
"config": {
"url": "https://www.googleapis.com/drive/v3/files/-----/copy?name=im%20a%20copy&parents=---",
"method": "POST",
"headers": {
"Accept-Encoding": "gzip",
"User-Agent": "google-api-nodejs-client/0.7.2 (gzip)",
"Authorization": "---",
"Accept": "application/json"
},
"params": {
"name": "im a copy",
"parents": [
"---"
]
},
"responseType": "json"
},
"data": {
"kind": "drive#file",
"id": "---",
"name": "Copy of _template",
"mimeType": "application/vnd.google-apps.spreadsheet"
},
"headers": {
"alt-svc": "quic=\":443\"; ma=2592000; v=\"46,43\",h3-Q050=\":443\"; ma=2592000,h3-Q049=\":443\"; ma=2592000,h3-Q048=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000",
"cache-control": "no-cache, no-store, max-age=0, must-revalidate",
"connection": "close",
"content-encoding": "gzip",
"content-type": "application/json; charset=UTF-8",
"date": "Mon, 25 Nov 2019 05:37:01 GMT",
"expires": "Mon, 01 Jan 1990 00:00:00 GMT",
"pragma": "no-cache",
"server": "GSE",
"transfer-encoding": "chunked",
"vary": "Origin, X-Origin",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-xss-protection": "1; mode=block"
},
"status": 200,
"statusText": "OK"
}
可以看到 response.data.name 显示的是默认命名,而不是“我是副本”。
任何方向将不胜感激。谢谢
https://developers.google.com/drive/api/v3/reference/files/copy
解决方案
- 您想使用 Files: copy in Drive API v3 的方法复制文件。
- 您想使用 googleapis 和 Node.js 来实现这一点。
- 您已经能够使用 Drive API。
如果我的理解是正确的,这个答案怎么样?
修改后的脚本:
在本次修改中,copyRequest
进行drive.files.copy()
了如下修改。
var copyRequest = { // Modified
name: "im a copy",
parents: ["idOfDestinationFolder"]
};
drive.files.copy(
{ // Modified
fileId: "idOfSheetToClone",
requestBody: copyRequest // or resource: copyRequest
},
function(err, response) {
if (err) {
console.log(err);
res.send("error");
return;
}
res.send(response);
}
);
笔记:
- 如果出现错误,请使用最新版本的googleapis。
参考:
如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
推荐阅读
- python - 张量流错误。TypeError:张量对象仅在启用急切执行时才可迭代。要迭代此张量,请使用 tf.map_fn
- php - 检查 MySQL 查询是否具有布尔值并显示 ✓ 或 ✖
- pointers - 在汇编中,使用“mov/lea”时是否总是需要指定“DWORD/WORD/BYTE...”?
- postgresql - Postgres 的循环依赖
- bash - 如何避免在 bash 参数扩展中出现“找不到命令”?
- office365 - 如何以编程方式找到 Office 365 发行版?
- c# - Azure Active Directory 为 API 调用返回 203
- php - Rest API:保存一个值以供合理数量的并发用户以后使用
- php - 在 PHP + PostgreSQL 的查询中使用变量和表名的正确方法是什么?
- cassandra - DataStax Bulk Loader - 更改 Cassandra 中的日期格式