Last active
January 27, 2023 01:47
-
-
Save takahashilabo/02342d1a1e83e9f830d24df96aa7f123 to your computer and use it in GitHub Desktop.
Google ClassroomのClassroomフォルダから最新提出ファイルのみ取り出すスクリプト(ChatGPTの生成コードを改変しただけ)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//使い方: | |
// 1. Google Driveのトップにスクリプトを新規作成し以下のコピペする。 | |
// 2. 抽出したいClassroomの課題フォルダのFOLDER_IDを取得して以下にコピペする | |
// 3. あとは1で作成したスクリプトを実行する(コード上の実行ボタンを押すだけ)。 | |
// 4. Google DriveのトップにLatest Submissionsというフォルダができるのでその中に提出された最新ファイルだけがコピーされる。 | |
function extractLatestSubmissions() { | |
// Define the folder containing the submissions | |
var folder = DriveApp.getFolderById("FOLDER_ID"); | |
// Get all the files in the folder | |
var files = folder.getFiles(); | |
// Create an empty object to store file IDs and their corresponding modification dates | |
var fileTime = {}; | |
var fileIds = {}; | |
// Loop through the files in the folder | |
while (files.hasNext()) { | |
var file = files.next(); | |
var fileId = file.getId(); | |
var modificationTime = file.getLastUpdated(); | |
var key = file.getOwner().getEmail() + file.getName(); | |
// Check if the file ID already exists in the fileData object | |
if (fileTime.hasOwnProperty(key)) { | |
// If it does, check if the modification time is more recent | |
if (modificationTime > fileTime[key]) { | |
// If it is, update the fileData object with the new modification time | |
fileTime[key] = modificationTime; | |
fileIds[key] = fileId; | |
} | |
} else { | |
// If the file ID does not exist in the fileData object, add it with the current modification time | |
fileTime[key] = modificationTime; | |
fileIds[key] = fileId; | |
} | |
} | |
// Get the IDs of the latest versions of the files | |
var latestFileIds = Object.values(fileIds); | |
// Create a new folder to store the latest versions of the files | |
var latestFolder = DriveApp.createFolder("Latest Submissions"); | |
// Loop through the latest file IDs | |
for (var i = 0; i < latestFileIds.length; i++) { | |
var file = DriveApp.getFileById(latestFileIds[i]); | |
// Move the file to the latest folder | |
//file.makeCopy(latestFolder); //Copy | |
file.makeCopy(file.getOwner().getEmail() + file.getName(), latestFolder); //Copy and Chage name | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add the email address of the file owner to the file name