错误文件的数据移动

Data movement of wrong files

我写了一个函数来将文件从一个文件夹移动到另一个文件夹。它有效,但问题是当打开目标文件夹时,我看到我的 google 脚本和一些不在源文件夹中的其他文档。

这是代码


function moveFiles() {
var currentMonth = Utilities.formatDate(new Date(), "GMT+6", "MMMM");
const source_folder = DriveApp.getFolderById('XXXXXXX');
const files = source_folder.getFiles();
const dest_folder = DriveApp.getFolderById('XXXXXX').createFolder(currentMonth + " test text");
while (files.hasNext()) {
const file = files.next();
dest_folder.addFile(file);
source_folder.removeFile(file);
}
}



提前致谢

我相信你的目标如下。

  • 您想使用 Google Apps 脚本将文件移动到创建的文件夹。

  • 从下面的情况来看,

    it works but the issue that when opening the destination folder I see my google script and some other documents that aren't in the source folder.

    • 虽然您可能想将特定 mimeType 的文件移动到该文件夹​​。
  • 在目前阶段,我认为 Class 个文件中的 moveTo(destination) 个可以用于将文件移动到文件夹中。

修改后的脚本 1:

在此修改中,使用 getFilesByType 检索文件。

function moveFiles() {
  var currentMonth = Utilities.formatDate(new Date(), "GMT+6", "MMMM");
  const source_folder = DriveApp.getFolderById('XXXXXXX');
  const files = source_folder.getFilesByType(MimeType.GOOGLE_SHEETS);
  const dest_folder = DriveApp.getFolderById('XXXXXXX').createFolder(currentMonth + " test text");
  while (files.hasNext()) {
    files.next().moveTo(dest_folder);
  }
}
  • 在此修改后的脚本中,Google 电子表格文件被移动。
  • 当您要修改mimeType时,请根据您的情况修改MimeType.GOOGLE_SHEETS

修改脚本 2:

在此修改中,使用 getFiles 检索文件并在循环中检查 mimeTypes。

function moveFiles() {
  var currentMonth = Utilities.formatDate(new Date(), "GMT+6", "MMMM");
  const source_folder = DriveApp.getFolderById('XXXXXXX');
  const files = source_folder.getFiles();
  const dest_folder = DriveApp.getFolderById('XXXXXXX').createFolder(currentMonth + " test text");
  const exclusiveMimeTypes = [MimeType.GOOGLE_APPS_SCRIPT, MimeType.GOOGLE_DOCS];
  while (files.hasNext()) {
    const file = files.next();
    if (!exclusiveMimeTypes.includes(file.getMimeType())) {
      file.moveTo(dest_folder);
    }
  }
}
  • 在此示例脚本中,从 when opening the destination folder I see my google script and some other documents that aren't in the source folder 开始,移动了 Google Apps 脚本文件和 Google 文档文件以外的文件。
  • 如需添加更多专属mimeType,请修改以上脚本

参考文献: