Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google Drive-共享驱动器-在向共享驱动器添加任何新文件时自动复制的脚本_Google Apps Script_Google Drive Api_Google Drive Shared Drive - Fatal编程技术网

Google apps script Google Drive-共享驱动器-在向共享驱动器添加任何新文件时自动复制的脚本

Google apps script Google Drive-共享驱动器-在向共享驱动器添加任何新文件时自动复制的脚本,google-apps-script,google-drive-api,google-drive-shared-drive,Google Apps Script,Google Drive Api,Google Drive Shared Drive,我希望使用一个脚本,它可以复制添加到Google Drive->Shared Drive->(驱动器名)的任何新文件 理想情况下,我希望将此副本移动到另一个共享驱动器,而不是在名称之前插入标题“copy of”。。。但我不确定是否可以。(旁注:我无法让它工作……可能是因为另一个共享驱动器归另一个共享驱动器所有?) 我的一个问题是,我不明白如何将复制功能限制为仅限于新添加的文件和特定日期后添加的文件 我所有的测试都得到了文件夹的完整副本。我似乎不能把它限制在新文件中。此外,我似乎无法避免在每个新文

我希望使用一个脚本,它可以复制添加到Google Drive->Shared Drive->(驱动器名)的任何新文件

理想情况下,我希望将此副本移动到另一个共享驱动器,而不是在名称之前插入标题
“copy of”
。。。但我不确定是否可以。(旁注:我无法让它工作……可能是因为另一个共享驱动器归另一个共享驱动器所有?)

我的一个问题是,我不明白如何将复制功能限制为仅限于新添加的文件和特定日期后添加的文件

我所有的测试都得到了文件夹的完整副本。我似乎不能把它限制在新文件中。此外,我似乎无法避免在每个新文件名的开头添加“Copy of”

我能做什么:我可以(是的)将任何给定文件夹的全部内容(所有日期)复制到我在根目录(非共享)google drive中创建的文件夹(我将其命名为备份)


要在上次运行脚本后检查创建的文件,可以使用属性服务[1]以毫秒为单位保存日期,并将其与文件创建日期进行比较,您可以对每个文件[2]使用
getCreated()
函数获取文件创建日期。要使用自定义名称复制文件,请使用
makeCopy
函数[3]。要完全删除文件(而不是垃圾箱),请使用删除请求[4]和驱动器高级服务(您必须激活它)[5]

[1]

[2]

[3]

[4]


[5]

不需要为自己是编程新手而道歉。你的Q位置很好。就我个人而言,我觉得谷歌的应用程序脚本文档既麻烦又混乱。。。
function moveFiles(sourceFileId, targetFolderId) { 
    var file = DriveApp.getFileById(sourceFileId); 
    file.getParents().next().removeFile(file); 
    DriveApp.getFolderById(targetFolderId).addFile(file); 
}
function moveFiles(sourceDriveId, targetFolderId) { 
  var scriptProperties = PropertiesService.getScriptProperties();
  var lastUpdate = scriptProperties.getProperty('lastUpdate');
  if(lastUpdate == null) {
    scriptProperties.setProperty('lastUpdate', new Date().getTime());
    return;
  }

  var targetFolder = DriveApp.getFolderById(targetFolderId);
  var sourceDrive = DriveApp.getFolderById(sourceDriveId);
  var files = sourceDrive.getFiles();

  while(files.hasNext()){
    var file = files.next();
    if(file.getDateCreated().getTime() > lastUpdate) {
      file.makeCopy(file.getName(), targetFolder); 
      Drive.Files.remove(file.getId(), {supportsAllDrives: true});
    }
  }  
  scriptProperties.setProperty('lastUpdate', new Date().getTime());  
}

function test() {
  moveFiles("[SOURCE-DRIVE-ID]", "[TARGET-FOLDER-ID]");
}