仅在 Google 个 Apps 脚本中有新文件时才解压缩文件
Unzip File Only If New in Google Apps Scripts
我在 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),所以我不想朝那个方向发展。
我还发现以下代码用于对保存新电子表格设置 "replace" 限制,但无法弄清楚如何将其与我的代码集成。 (代码来自用户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);
}
任何关于如何解决这个问题的想法都将不胜感激!我是一个彻头彻尾的菜鸟,所以如果这是一个愚蠢的问题,我提前道歉。
编辑:我无法弄清楚如何只解压缩源文件夹中的 "new" 文件,因此我的新代码移动到垃圾文件夹中的所有文件,然后解压缩源文件夹中的所有文件文件夹。代码如下:
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 驱动器中,然后让我解压缩这些文件具有不时运行的功能。有谁知道如何在不删除并重新创建所有文件的情况下完成同样的事情?
编辑 2:
感谢 Cameron,这个问题得到了解答。我在下面粘贴我正在使用的完整代码,以供后代/其他新手使用,这样他们就不必将它们拼凑在一起:
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);
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());
}
您可以对 File 对象使用 getDateCreated() 函数来确定文件的创建时间。通过根据时间限制检查此值,您应该能够确定文件是否是新文件。如果您在两次执行之间至少间隔几个小时触发脚本,则可以使用硬编码的截止时间。因此,如果您每六小时触发一次脚本,则可以忽略过去 6 小时内未创建的任何文件,例如。
但是,更可靠的方法是将上次成功执行时间存储在 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());
我在 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),所以我不想朝那个方向发展。
我还发现以下代码用于对保存新电子表格设置 "replace" 限制,但无法弄清楚如何将其与我的代码集成。 (代码来自用户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);
}
任何关于如何解决这个问题的想法都将不胜感激!我是一个彻头彻尾的菜鸟,所以如果这是一个愚蠢的问题,我提前道歉。
编辑:我无法弄清楚如何只解压缩源文件夹中的 "new" 文件,因此我的新代码移动到垃圾文件夹中的所有文件,然后解压缩源文件夹中的所有文件文件夹。代码如下:
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 驱动器中,然后让我解压缩这些文件具有不时运行的功能。有谁知道如何在不删除并重新创建所有文件的情况下完成同样的事情?
编辑 2: 感谢 Cameron,这个问题得到了解答。我在下面粘贴我正在使用的完整代码,以供后代/其他新手使用,这样他们就不必将它们拼凑在一起:
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);
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());
}
您可以对 File 对象使用 getDateCreated() 函数来确定文件的创建时间。通过根据时间限制检查此值,您应该能够确定文件是否是新文件。如果您在两次执行之间至少间隔几个小时触发脚本,则可以使用硬编码的截止时间。因此,如果您每六小时触发一次脚本,则可以忽略过去 6 小时内未创建的任何文件,例如。
但是,更可靠的方法是将上次成功执行时间存储在 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());