Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set new file name when using google drive.files.copy

I'm using google drive apiv3 to copy a spreadsheet into a folder on google drive. The documentation states the "name" property should be used to set the title of the new sheet. I'm using this, but newly created sheets are named "Copy of {titleOfSheetToCopy}".

request:

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);
    });
});

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"
}

can see that response.data.name shows the default naming instead of "i'm a copy".

Any direction would be appreciated. Thanks

https://developers.google.com/drive/api/v3/reference/files/copy

like image 599
justin Avatar asked Jan 20 '26 03:01

justin


1 Answers

  • You want to copy a file using the method of Files: copy in Drive API v3.
  • You want to achieve this using googleapis with Node.js.
  • You have already been able to use Drive API.

If my understanding is correct, how about this answer?

Modified script:

In this modification, copyRequest and drive.files.copy() were modified as follows.

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);
  }
);

Note:

  • If an error occurs, please use the latest version of googleapis.

Reference:

  • Files: copy

If I misunderstood your question and this was not the result you want, I apologize.

like image 151
Tanaike Avatar answered Jan 22 '26 20:01

Tanaike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!