首页 > 解决方案 > 使用 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

标签: node.jsgoogle-drive-api

解决方案


  • 您想使用 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。

参考:

如果我误解了您的问题并且这不是您想要的结果,我深表歉意。


推荐阅读