Google Driveï¼ä»¥ä¸GDï¼ã§ãã¡ã¤ã«ãä½ã£ãããæ´æ°ããã¨ãã«ãã¡ã¼ã«ã§ãã¡ãã¡éç¥ããã®ãé¢åã¨ãããä½ã£ãã
åãã¦Google Apps Scriptã使ã£ããã©ãjavascriptã©ã¤ã¯ã§ãGoogleã®APIãç°¡åã«ä½¿ãã¦ããµã¼ãã¼ä»£ãã¡ã³ããã³ã¹ãããããªãã®ã¯ããªã便å©ã
yamap55.hatenablog.com
ãã¡ããåèã«ããã¦ããã ãã¾ããã
Githubã«ãç½®ãã¾ãã
GitHub - uyamazak/gdrive_update_notifier: Google Driveで更新があった際にメール通知を送るGoogle Apps Script。
ãã©ã«ããæä¸é¨ã¾ã§æ¢ãããã«ãå帰é¢æ°ãä½ã£ãã®ãããã°ã£ãã¨ããã
使ãæ¹
GDä¸ã§è¨é²ç¨ã®ã¹ãã¬ããã·ã¼ããä½ãã
ãã¼ã«âã¹ã¯ãªããã¨ãã£ã¿ã§ãä¸è¨å 容ãå¼µãä»ãã
//対象ã¨ããGoogleDriveãã©ã«ãã®IDããã©ã¦ã¶ã§ã¢ã¯ã»ã¹ãã¦URLè¦ãã°åãã var TARGET_FOLDER_ID = ""; //æ´æ°æ¥æãè¨é²ããã®ã¹ãã¬ããã·ã¼ãã®IDããã©ã¦ã¶ã§ã¢ã¯ã»ã¹ãã¦URLè¦ãã°åãã var UPDATE_SHEET_ID = ""; //ã¹ãã¬ããã·ã¼ãã®ã·ã¼ãåï¼ä¸ã«è¡¨ç¤ºãããã¿ãã®ãã¤ï¼ var UPDATE_SHEET_NAME = "ã·ã¼ã1"; var SEND_MAIL_ADDRESS = ["[email protected]","[email protected]"] function updateCheck() { var targetFolder = DriveApp.getFolderById(TARGET_FOLDER_ID); var folders = targetFolder.getFolders(); var files = targetFolder.getFiles(); //ãã©ã«ãå ãå帰çã«æ¢ç´¢ãã¦ãã¹ã¦ã®ãã¡ã¤ã«IDãé åã«ãã¦è¿ã function getAllFilesId(targetFolder){ var filesIdList = []; var files = targetFolder.getFiles(); while(files.hasNext()){ filesIdList.push(files.next().getId()); } var child_folders = targetFolder.getFolders(); while(child_folders.hasNext()){ var child_folder = child_folders.next(); //Logger.log( 'child_folder :' + child_folder ); //Logger.log('getAllFilesId(child_folder):'+ getAllFilesId(child_folder)); filesIdList = filesIdList.concat( getAllFilesId(child_folder) ); } return filesIdList; } //Logger.log('getAllFilesId(targetFolder):' + getAllFilesId(targetFolder)); var allFilesId = getAllFilesId(targetFolder); var lastUpdateMap = {}; //Logger.log(folders) allFilesId.forEach( function( value, i ){ var file =DriveApp.getFileById( value ); lastUpdateMap[file.getName()] = {lastUpdate : file.getLastUpdated(), fileId: file.getId()}; } ); // ã¹ãã¬ããã·ã¼ãã«è¨è¼ããã¦ãããã©ã«ãåã¨æ´æ°æ¥æãåå¾ã var spreadsheet = SpreadsheetApp.openById(UPDATE_SHEET_ID); var sheet = spreadsheet.getSheetByName(UPDATE_SHEET_NAME); //Logger.log(sheet) var data = sheet.getDataRange().getValues(); //Logger.log('data: ' + data) // åå¾ãããã¼ã¿ãMapã«å¤æã var sheetData = {}; for (var i = 0; i < data.length; i++) { sheetData[data[i][0]] = {name : data[i][0], lastUpdate : data[i][1], rowNo : i + 1}; } // å®éã®ãã©ã«ãã¨ã¹ãã¬ããã·ã¼ãæ å ±ãæ¯è¼ã var updateFolderMap = []; for (key in lastUpdateMap) { if( UPDATE_SHEET_ID == lastUpdateMap[key].fileId ){ continue; } if(key in sheetData) { // ãã©ã«ãåãã·ã¼ãã«åå¨ããå ´åã if(lastUpdateMap[key].lastUpdate > sheetData[key].lastUpdate) { // ãã©ã«ããæ´æ°ããã¦ããå ´åã sheet.getRange(sheetData[key].rowNo, 2).setValue(lastUpdateMap[key].lastUpdate); sheet.getRange(sheetData[key].rowNo, 3).setValue(lastUpdateMap[key].fileId); updateFolderMap.push({filename:key, lastUpdate:lastUpdateMap[key].lastUpdate, fileId:lastUpdateMap[key].fileId}); } } else { // ãã©ã«ãåãã·ã¼ãã«åå¨ããªãå ´åã var newRow = sheet.getLastRow() + 1; sheet.getRange(newRow, 1).setValue(key); sheet.getRange(newRow, 2).setValue(lastUpdateMap[key].lastUpdate); sheet.getRange(newRow, 3).setValue(lastUpdateMap[key].fileId); updateFolderMap.push({filename:key, lastUpdate:lastUpdateMap[key].lastUpdate, fileId:lastUpdateMap[key].fileId}); } } //Logger.log('updateFolderMap:' + updateFolderMap) // æ°è¦åã³æ´æ°ãããæ å ±ãã¡ã¼ã«éä¿¡ã var updateText = ""; for( key in updateFolderMap ){ item = updateFolderMap[key]; updateText += item.filename + 'ãæ´æ°æ¥æï¼' + Utilities.formatDate(item.lastUpdate, "JST", "yyyy-MM-dd HH:mm:ss") + '\n' + DriveApp.getFileById(item.fileId).getUrl() + "\n\n" } if (updateFolderMap.length != 0) { SEND_MAIL_ADDRESS.forEach(function(o,i) { MailApp.sendEmail(SEND_MAIL_ADDRESS[i],targetFolder.getName() + "æ´æ°é£çµ¡éç¥", "ã" + targetFolder.getName() + "ããæ´æ°ããã¾ããã\n\n"+ updateText ); }); } }
URLãè¦ã¦ãè¨é²ç¨ã¹ãã¬ããã·ã¼ãã®IDãUPDATE_SHEET_IDã«å
¥ãã
https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxxxx/edit#gid=0
ç£è¦å¯¾è±¡ã¨ããGDã®ãã©ã«ããéãã¦ãURLããIDãTARGET_FOLDER_IDã«ã³ãã
https://drive.google.com/drive/folders/xxxxxxxxxxxxxxxxxxxx
ã·ã¼ãåãããã£ã¦ãªããã°ãã·ã¼ã1ãã®ã¾ã¾ãå¤æ´ãããUPDATE_SHEET_NAMEãå¤ãã
SEND_MAIL_ADDRESS ã«éä¿¡å ã¡ã¼ã«ã¢ãã¬ã¹ã追å
ã¹ã¯ãªããã¨ãã£ã¿ã§ä¿åãã¦ãå®è¡ï¼ä¸è§å½¢ã®ãã¤ï¼ãã¯ãªãã¯ãã¦ãã¡ã¼ã«ãæ¥ãã確èªããã
ååã¯ãã¹ã¦ã®ãã¡ã¤ã«ã«å¯¾ãã¦éç¥ãããã¯ãã
éä¿¡å ã¯æä½ãã¦ããã¢ã«ã¦ã³ãã®Gmailã¨ãªããéä¿¡æ¸ã¿ãã©ã«ãã«ã¡ã¼ã«ãå ¥ã£ã¦ãã¾ãã®ã¯æ³¨æã
æåã§åããã¦ã¿ã¦ãåé¡ããªããã°ãæè¨ãã¼ã¯ãã¯ãªãã¯ãã¦ãããªã¬ã¼ãè¨å®ããã
15åã¨ã30åç¨åº¦ãããããã