简体   繁体   English

如何使用 JavaScript API 将 Google Drive 文档从一个文件夹复制到另一个文件夹

[英]How to copy a google drive document from one folder to another using JavaScript API

I have two folders Folder1 and Folder2 in my Google drive.我的 Google 驱动器中有两个文件夹Folder1Folder2

I have created a google drive document doc1 in Folder1.我在 Folder1 中创建了一个谷歌驱动器文档doc1 Later i need to copy this document to Folder2.稍后我需要将此文档复制到Folder2。 How can i achieve this.我怎样才能做到这一点。

I tried the below link but its creating the copy on the same folder.我尝试了下面的链接,但它在同一个文件夹中创建了副本。

https://developers.google.com/drive/v2/reference/files/copy https://developers.google.com/drive/v2/reference/files/copy

/**
 * Copy an existing file.
 *
 * @param {String} originFileId ID of the origin file to copy.
 * @param {String} copyTitle Title of the copy.
 */
function copyFile(originFileId, copyTitle) {
  var body = {'title': copyTitle};
  var request = gapi.client.drive.files.copy({
    'fileId': originFileId,
    'resource': body
  });
  request.execute(function(resp) {
    console.log('Copy ID: ' + resp.id);
  });
}

As I have read on this blog , there is no direct method to move files from one folder to another in Google Drive.正如我在此博客上所读到的,没有直接的方法可以将文件从 Google Drive 中的一个文件夹移动到另一个文件夹。 Your option is to copy a file to another folder, set its name to the original file and then delete the original using the setTrashed(true) method of File.您的选择是将文件复制到另一个文件夹,将其名称设置为原始文件,然后使用 File 的setTrashed(true) 方法删除原始文件。

Be noted that this method will fail if the file/s have been uploaded by another user while the script is running under a different user.请注意,如果脚本在其他用户下运行时文件已由其他用户上传,则此方法将失败。

Here is a sample snippet:这是一个示例片段:

function copyFile(originFileId, destinationFileId) {

  var files = originFileId.getFiles();

  while (files.hasNext()) {

    var file = files.next();
    file.makeCopy(target).setName(file.getName());
    file.setTrashed(true);

  }

Hope this helps!希望这可以帮助! :) :)

Hackaround in 2020. Go to Google Colab and use ! cp path1 path2 2020 年的 Hackaround。转到 Google Colab 并使用! cp path1 path2 ! cp path1 path2

You can even use -r option for directories.您甚至可以对目录使用 -r 选项。 Pretty neat!漂亮整齐! Though I should warn you this method is slow if your folder contains a lot of files or contains large files.尽管我应该警告您,如果您的文件夹包含大量文件或包含大文件,则此方法会很慢。

Edit: typo in Colab编辑:Colab 中的错字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Google Drive 复制文件夹以使用 JS 在 Google Drive 中创建新文件夹 - How to copy a folder from Google Drive to create a new folder in Google drive using JS 使用javascript或jquery将文件从一个文件夹复制到另一个文件夹 - copy file from one folder to another folder using javascript or jquery 如何使用 Google Drive API Javascript 列出特定文件夹中的更改 - How to list changes in a specific folder using Google Drive API Javascript 使用JavaScript API在Google驱动器中创建文件夹 - Create Folder in google drive using JavaScript API 如何使用php将文件从Google驱动器复制到另一台服务器 - How to copy file from google drive to another server using php 如何使用 javascript 在 Google Drive 上创建文件夹 - How to create a folder on Google Drive using javascript 如何在谷歌驱动器 api 中复制文件夹? - How I can copy folder in google drive api? 如何使用js将文件从一个文件夹复制到另一个文件夹 - How to copy the file from one folder to another using js 如何使用用于 excel 插件的 excel javascript api 将一列从一个工作表复制到另一个工作表 - How to copy a column from one worksheet to another using the excel javascript api for an excel addin 如何将文件或文件夹移动到谷歌驱动器 api 中的另一个文件夹? - How can i move the file or folder to another folder in google drive api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM