Search code examples
google-apps-scriptgoogle-drive-apionedrive

google apps script. copy files to one drive


I would have files stored in google drive and I would like to copy (or move) those files to my OneDrive. I have managed to connect OneDrive to google drive and I can successfully create folders and upload files by file ID. I am trying to copy (or move) multiple files at once from google drive to OneDrive though without any luck yet. I am not good at programming just doing everything by tutorials only but yet could not find any working one. Maybe anyone could guide me how to do it? For the final idea I would like to achieve that files from google drive would be copied even to separate folders by the part of file title (name and surname, if to be exact). Currently I have "developed" such script:

function moveFiles(source, dest_folder) {
  var source = DriveApp.getFolderById("1faFbaXJNziwIGyer2H2IPALQ8ahBfGMc");
  var prop = PropertiesService.getScriptProperties();
  var odapp = OnedriveApp.init(prop);
  var files = DriveApp.getFiles();

  while (files.hasNext()) {
    var file = files.next();  
    var dest_folder = 
    OnedriveApp.init(PropertiesService.getScriptProperties()).uploadFile(files, 
    "/samplefolder");
  }
}

Solution

  • I believe your current situation and your goal are as follows.

    • You are using OnedriveApp.
    • You have already been finished the authorization process. So you have already been had the access token and refresh token in the PropertiesServices.
    • You want to upload the files in a specific folder of Google Drive to a specific folder in OneDrive.
    • The number of files is 10 - 20.
    • The file size is small.

    From your following reply

    I have changed accordingly to: var files = file.getId(); but then I receive such error: TypeError: Cannot read property 'getId' of undefined moveFiles @ srcpt.gs:5 If I use DriveApp.getFiles() or source.getFiles(), I receive such error: Exception: Unexpected error while getting the method or property getFileById on object DriveApp. convToMicrosoft @ code.gs:629 OnedriveApp.uploadFile @ code.gs:460 Appreciated for any help.

    • About var files = file.getId(); and If I use DriveApp.getFiles() or source.getFiles(), I receive such error:, unfortunately, I cannot understand it.

    In this case, how about the following modification?

    Modified script:

    function myFunction() {
      var prop = PropertiesService.getScriptProperties();
      var odapp = OnedriveApp.init(prop);
    
      var source = DriveApp.getFolderById("###"); // Please put your folder ID.
      var files = source.getFiles();
      while (files.hasNext()) {
        var file = files.next();
        odapp.uploadFile(file.getId(), "/samplefolder/"); // Please set the destination folder name. In this case, the folder name in OneDrive.
      }
    }
    
    • When "/samplefolder" is used, an error occurs. Please enclose the folder name by / like "/samplefolder/". Please be careful about this.

    • When this script is run, the files in the specific folder of Google Drive are copied to the destination folder of /samplefolder/ in OneDrive.

    References: