Google Apps脚本将文件移动到其他文件夹时,云端硬盘文件的修改日期会发生变化 [英] The modification date of Drive files change when Google Apps Script move a file to other folder

查看:212
本文介绍了Google Apps脚本将文件移动到其他文件夹时,云端硬盘文件的修改日期会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var idOriginFolder ='ABCDEFG12345abcdefg'; 
var originFolder = DriveApp.getFolderById(idOriginFolder);
var destinationFolder = DriveApp.createFolder('New Folder');
var searchString =''+ idOriginFolder +'in parents'
var foundFiles = DriveApp.searchFiles(searchString);
while(foundFiles.hasNext()){
var file = foundFiles.next();
destinationFolder.addFile(file);
originFolder.removeFile(file);
}

文件移动正确,但每个移动文件的修改日期为更改为脚本执行日期。你知道有什么办法可以避免这种情况吗?当我通过Google Drive的Web Interface移动文件时,不会发生这种情况。

解决方案

根据我的经验,文件的修改日期不会因使用Drive API v3而改变。在你的问题中,当使用DriveApp移动文件时,修改日期已更改。我认为DriveApp使用Drive API v2。所以我调查了这一点,因为我对这种情况感兴趣。

对于Drive API v2


  • 发现当使用 drive.files.update drive.files.patch 移动文件时,修改日期已更改。



对于Drive API v3


  • 发现当文件使用 drive.files.update 移动,修改日期为 NOT 更改。


示例脚本:



使用Drive API v3的示例脚本如下。

  var idOriginFolder ='ABCDEFG12345abcdefg'; 
var destinationFolder = DriveApp.createFolder('New Folder')。getId();
var searchString =''+ idOriginFolder +'in parents'
var foundFiles = DriveApp.searchFiles(searchString);
var requests = [];
while(foundFiles.hasNext()){
var file = foundFiles.next();
requests.push({
url:https://www.googleapis.com/drive/v3/files/+ file.getId()+?addParents =+ destinationFolder +& amp ; removeParents =+ idOriginFolder,
method:patch,
headers:{Authorization:Bearer+ ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
});
}
var res = UrlFetchApp.fetchAll(requests);
Logger.log(res)



注意:




  • 从这些结果可以看出,通过Web Interface移动文件可能是由于Drive API v3造成的。

  • 这是一个简单的示例脚本。因此,如果您想移动大量文件,我建议使用批处理请求。



参考:




  • 文件: Drive API v2更新

  • 文件:适用于Drive API v2的修补程序

  • 文件:Drive API v3的更新

  • 批处理请求
    如果这对您没有用处,我很抱歉。

    I am moving file this way:

    var idOriginFolder = 'ABCDEFG12345abcdefg';
    var originFolder = DriveApp.getFolderById(idOriginFolder);
    var destinationFolder = DriveApp.createFolder('New Folder');
    var searchString = '"'+idOriginFolder+'" in parents'
    var foundFiles = DriveApp.searchFiles(searchString);
    while (foundFiles.hasNext()){
     var file = foundFiles.next();
     destinationFolder.addFile(file);
     originFolder.removeFile(file);
    }
    

    The files are moved correctly, but the modification date of every one moved file is changed to script execution date. Do you know any way to avoid this? When I move files throught of the Web Interface of Google Drive this not happen.

    解决方案

    In my experience, the modification date of files is not changed by moving using Drive API v3. In your question, when the files were moved using DriveApp, the modification date was changed. I think that DriveApp uses Drive API v2. So I investigated this, because I was interested in this situation.

    For Drive API v2
    • It was found that when the files were moved using drive.files.update and drive.files.patch, the modification date was changed.
    For Drive API v3
    • It was found that when the files were moved using drive.files.update, the modification date was NOT changed.

    Sample script :

    The sample script for using Drive API v3 is as follows.

    var idOriginFolder = 'ABCDEFG12345abcdefg';
    var destinationFolder = DriveApp.createFolder('New Folder').getId();
    var searchString = '"'+idOriginFolder+'" in parents'
    var foundFiles = DriveApp.searchFiles(searchString);
    var requests = [];
    while (foundFiles.hasNext()){
      var file = foundFiles.next();
      requests.push({
        url: "https://www.googleapis.com/drive/v3/files/" + file.getId() + "?addParents=" + destinationFolder + "&removeParents=" + idOriginFolder,
        method: "patch",
        headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
        muteHttpExceptions: true,
      });
    }
    var res = UrlFetchApp.fetchAll(requests);
    Logger.log(res)
    

    Note :

    • From these results, it is considered that moving files by Web Interface may be due to Drive API v3.
    • This is a simple sample script. So if you want to move a lot of files, I recommend to use the Batching Requests.

    Reference :

    If this was not useful for you, I'm sorry.

    这篇关于Google Apps脚本将文件移动到其他文件夹时,云端硬盘文件的修改日期会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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