我有一個在目標檔案夾中查找并將檔案解壓縮到新檔案夾的腳本。只要 zip 只包含一個檔案,一切都完美無缺;不再,我似乎只解壓縮了第一個檔案。
我很確定導致這種情況的行是選擇檔案陣列中的第一條記錄,但是我不夠熟練,無法弄清楚如何解決這個問題。任何幫助將不勝感激
[var newDriveFile = DestinationFolder.createFile(unZippedfile[0]);]
//Unzips all ZIP files in the folder
function Unzip() {
//Add folder ID to select the folder where zipped files are placed
var SourceFolder = DriveApp.getFolderById("14vRNV3FZJicplxeurycsfBHmETrUQBex")
//Add folder ID to save the where unzipped files to be placed
var DestinationFolder = DriveApp.getFolderById("1OJXkPrUqcqapsv366oCV_ZsvaECstKTn")
//Select the Zip files from source folder using the Mimetype of ZIP
var ZIPFiles = SourceFolder.getFilesByType(MimeType.ZIP)
//var ZIPFiles = SourceFolder.getFilesByName('MTTR.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]); //I think this line is where my issue is
}
Logger.log("Finished Unzipping files")
}
uj5u.com熱心網友回復:
正如 Rodrigo 在評論中提到的,您需要在所有檔案上回圈它。unZippedfile.length是 zip 檔案中的檔案數。在createFile方法周圍使用回圈。
代碼:
// loop to all zip files
while (ZIPFiles.hasNext()){
var fileBlob = ZIPFiles.next().getBlob();
var unZippedfile = Utilities.unzip(fileBlob);
// loop to all items inside the current zip file
for (i=0; i<unZippedfile.length; i ) {
var newDriveFile = DestinationFolder.createFile(unZippedfile[i]);
}
}
輸出:

筆記:
- 上面的示例輸出顯示了相同目錄中的解壓縮檔案,因為我使用相同的目錄 ID 實體化了源和目標。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/358824.html
標籤:谷歌应用程序脚本
