当使用Apps Script将文件从一个文件夹复制到另一个文件夹时,任何"Apps Script"复制的文件最终位于MyDrive中而不是指定的文件夹中-为什么? [英] When copying files using Apps Script from one folder to another any "Apps Script" files being copied end up in MyDrive not the specified folder - why?

查看:66
本文介绍了当使用Apps Script将文件从一个文件夹复制到另一个文件夹时,任何"Apps Script"复制的文件最终位于MyDrive中而不是指定的文件夹中-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此Apps脚本制作某些文件的备份副本:

I use this Apps Script to make backup copies of some files:

var sourceFolderId = "xxxxxx";
var sourceFolder = DriveApp.getFolderById(sourceFolderId);
var latestFiles = sourceFolder.getFiles();
var backupFolderId = "yyyyyy";
var backupFolder = DriveApp.getFolderById(backupFolderId);

while(latestFiles.hasNext()) {
 var file = latestFiles.next();
 file.makeCopy(backupFolder);
}

它工作正常-将所有文件从源文件夹复制到备份文件夹-除非复制的任何文件是"Google Apps脚本"文件,否则不会将其复制到备份文件夹,而是将其复制到我的云端硬盘".

It works fine - copying all files from the source folder to the backup folder- except that if any file being copied is a "Google Apps script" file it doesn't copy it to the backup folder, it copies it to "My Drive".

然后,我必须将其从我的云端硬盘"移动到所需的文件夹.我可以使用脚本来执行此操作,但是我无法弄清楚为什么它首先要执行此操作.

I then have to move it from "My Drive" to the required folder. I can do this with a script but I can't work out why it is doing this in the first place.

我是所有文件和文件夹的所有者.

I am the owner of all the files and folders.

有什么想法吗?

谢谢

推荐答案

在我的环境中,我也与您确认了同样的情况.仅Google Apps脚本无法复制到备份文件夹.我认为这可能是一个错误.因此,我想到了这种情况的解决方法.我测试的模式如下.

Also in my environment, I confirmed the same situation with you. Only Google Apps Scripts cannot be copied to the backup folder. I think that this may be a bug. So I thought of the workaround for this situation. The patterns I tested are as follows.

  1. 试图使用 addFile() removeFile()将复制的GAS文件从我的云端硬盘"移到备份文件夹.
    • 可以添加备份文件夹的父ID.但是无法删除我的云端硬盘"的父ID.
      • 失败
  1. Tried to move the copied GAS files from "My Drive" to the backup folder using addFile() and removeFile().
    • A parent ID of backup folder can be added. But the parent ID of "My Drive" cannot be removed.
      • Failure
  • 将复制的GAS文件创建到我的云端硬盘".这与 makeCopy()相同.
    • 失败
    • 可以将父ID从我的云端硬盘"更改为备份文件夹.
      • 成功

      修改后的脚本:

      在此修改后的脚本中,使用 Drive.Files.update 将Google Apps脚本文件从我的云端硬盘"移动到备份文件夹.

      Modified script :

      In this modified script, Google Apps Script files are moved from "My Drive" to backup folder using Drive.Files.update.

      var sourceFolderId = "xxxxxx";
      var sourceFolder = DriveApp.getFolderById(sourceFolderId);
      var latestFiles = sourceFolder.getFiles();
      var backupFolderId = "yyyyyy";
      var backupFolder = DriveApp.getFolderById(backupFolderId);
      
      while(latestFiles.hasNext()) {
        var file = latestFiles.next();
        var res = file.makeCopy(backupFolder); // Modified
        if (file.getMimeType() == MimeType.GOOGLE_APPS_SCRIPT) { // Added
          Drive.Files.update({"parents": [{"id": backupFolderId}]}, res.getId()); // Added
        }
      }
      

      注意:

      要使用此修改后的脚本,请按以下步骤在高级Google服务和API控制台中启用Drive API.

      Note :

      In order to use this modified script, please enable Drive API at Advanced Google Services and API console as follows.

      • 在脚本编辑器上
        • 资源->高级Google服务
        • 打开Drive API v2
        • 在脚本编辑器上
          • 资源-> Cloud Platform项目
          • View API控制台
          • 在入门"中,单击启用API"并获取凭据(例如密钥).
          • 在左侧,单击库.
          • 在搜索API和服务,输入驱动器".然后点击Drive API.
          • 单击启用"按钮.
          • 如果已启用API,请不要关闭.

          如果我误解了你的问题,对不起.

          If I misunderstand your question, I'm sorry.

          这篇关于当使用Apps Script将文件从一个文件夹复制到另一个文件夹时,任何"Apps Script"复制的文件最终位于MyDrive中而不是指定的文件夹中-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆