仅在Google Apps脚本中是新文件时才解压缩文件

问题描述 投票:0回答:1

我在Google云端硬盘上有一个文件夹(我们将其称为源文件夹),该文件夹会不时更新为新的zip文件(底层文件为PDF)。我正在尝试使用Google Apps脚本仅解压缩新的zip文件并将基础PDF放置在另一个文件夹中(我们将其称为目标文件夹)。

我目前正在使用以下代码在基于时间的触发器上运行,将文件解压缩到源文件夹中。我当前的代码无法区分新旧zip文件,因此我在目标文件夹中积累了大量重复项。 (我在WeirdGeek上找到了此代码:https://www.weirdgeek.com/2019/10/unzip-files-using-google-apps-script/

function Unzip() {
  //Add folder ID to select the folder where zipped files are placed
  var SourceFolder = DriveApp.getFolderById("1KbyB2vTUfbwYdzBEyIwzTliXKjATbW8A")
  //Add folder ID to save the where unzipped files to be placed
  var DestinationFolder = DriveApp.getFolderById("1Z-iVlcROe5kVX8IkBlV9a98WKlvlfp3U")
  //Select the Zip files from source folder using the Mimetype of ZIP
  var ZIPFiles = SourceFolder.getFilesByType(MimeType.ZIP)

  //Loop over all the Zip files
  while (ZIPFiles.hasNext()){
   // Get the blob of all the zip files one by one
    var fileBlob = ZIPFiles.next().getBlob();
   //Use the Utilities Class to unzip the blob
    var unZippedfile = Utilities.unzip(fileBlob);
   //Unzip the file and save it on destination folder
    var newDriveFile = DestinationFolder.createFile(unZippedfile[0]);
    }
}

起初,我想对该功能添加某种基于时间的限制,但是由于源文件夹正在与sFTP站点同步(使用MultCloud),所以我不想朝这个方向发展。

我还发现以下代码用于对保存新电子表格进行“替换”限制,但无法弄清楚如何将此代码与我的代码集成。 (代码来自Tainake用户)

function saveAsSpreadsheet() {
  var folderId = "0B8xnkPYxGFbUMktOWm14TVA3Yjg";
  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFilesByName(getFilename());
  if (files.hasNext()) {
    files.next().setTrashed(true);
  }
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  DriveApp.getFileById(sheet.getId()).makeCopy(getFilename(), folder);
}

关于解决此问题的任何想法,将不胜感激!我是一个完全菜鸟,所以如果这是一个愚蠢的问题,我先向您道歉。

编辑:我不知道如何仅解压缩源文件夹中的“新”文件,因此,我的新代码移至删除目标文件夹中的所有文件,然后解压缩源文件夹中的所有文件。代码如下:



function Unzip() {
  //Add folder ID to select the folder where zipped files are placed
  var SourceFolder = DriveApp.getFolderById("1KbyB2vTUfbwYdzBEyIwzTliXKjATbW8A")
  //Add folder ID to save the where unzipped files to be placed
  var DestinationFolder = DriveApp.getFolderById("1Z-iVlcROe5kVX8IkBlV9a98WKlvlfp3U")

 //Delete files from the destination folder
  //Get the files in the destination folder
    var files = DestinationFolder.getFiles();

    //Loop through the files in the destination folder
    while(files.hasNext()){

      //Get the individual file in the destination folder to process
      var file = files.next(); 

      //Trash that file
        file.setTrashed(true);
      }


  //Select the Zip files from source folder using the Mimetype of ZIP
  var ZIPFiles = SourceFolder.getFilesByType(MimeType.ZIP)

  //Loop over all the Zip files
  while (ZIPFiles.hasNext()){
   // Get the blob of all the zip files one by one
    var fileBlob = ZIPFiles.next().getBlob();
   //Use the Utilities Class to unzip the blob
    var unZippedfile = Utilities.unzip(fileBlob);
   //Unzip the file and save it on destination folder
    var newDriveFile = DestinationFolder.createFile(unZippedfile[0]);
    }
}

我可以看到这可能不是解决此问题的最佳方法,但这使我能够让MultCloud将zip文件同步到我的Google云端硬盘中,然后使我可以通过运行时间的功能将这些文件解压缩时间。任何人都有一个更好的主意,如何在不删除并重新创建所有文件的情况下完成同一件事?

google-apps-script google-drive-api delete-file unzip
1个回答
0
投票
但是,更可靠的方法是将上一次成功执行的时间存储在Script Property中,因此您始终可以处理自上次成功执行以来创建的所有文件。

请注意,此代码将在第一次运行时处理该文件夹中当前的所有文件,之后将仅处理自上次运行以来创建的文件。

var ZIPFiles = SourceFolder.getFilesByType(MimeType.ZIP); var now = new Date(); //get current time after you fetch the file list from Drive. //Get script properties and check for stored "last_execution_time" var properties = PropertiesService.getScriptProperties(); var cutoff_datetime = properties.getProperty('last_execution_time'); //if we have last execution date, stored as a string, convert it to a Date object. if(cutoff_datetime) cutoff_datetime = new Date(cutoff_datetime); //Loop over all the Zip files while (ZIPFiles.hasNext()){ var file = ZIPFiles.next(); //if no stored last execution, or file is newer than last execution, process the file. if(!cutoff_datetime || file.getDateCreated() > cutoff_datetime){ var fileBlob = file.getBlob(); //Use the Utilities Class to unzip the blob var unZippedfile = Utilities.unzip(fileBlob); //Unzip the file and save it on destination folder var newDriveFile = DestinationFolder.createFile(unZippedfile[0]); } } //store "now" as last execution time as a string, to be referenced on next run. properties.setProperty('last_execution_time',now.toString());

© www.soinside.com 2019 - 2024. All rights reserved.