Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Drive files MD5 checksum to Google Sheets and exporting the Sheets data to text file #271

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ width="96px"/>
### Drive
- [Manage Google Drive files and folders](drive/quickstart)
- [View Google Drive activity](drive/activity)
- [Get MD5-checksum of all files and sub-directories](drive/md5-checksum)

<img
src="https://www.gstatic.com/images/branding/product/2x/forms_96dp.png"
Expand Down
58 changes: 58 additions & 0 deletions drive/md5-checksum/ExportHashValuesToText.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Export the hash values stored in Google Sheets to Text file
*/

// add the function to Google Sheets menu
function onOpen() {
var thisSS = SpreadsheetApp.getActiveSpreadsheet(); // get active Spreadsheet
var myFs = [
{name: "Export hash (.txt)", functionName: "exportHashToText"}, // Option to export Hash and File location
{name: "Export filename (.txt)", functionName: "exportFilenameToText"}, // Option to export only file location
];
thisSS.addMenu("Extra Custom Tools", myFs);
}

// get Data in sorted form from the sheet
function getData(){
var thisSS = SpreadsheetApp.getActiveSpreadsheet(); // get active Spreadsheet
var currSheet = thisSS.getActiveSheet(); // get the active sheet name
var range = currSheet.getRange("A:B"); // select columns containing the hash value and path
range.sort(2); // Sorts by the values in the second column (B)
return currSheet.getDataRange().getValues(); // return the values in the sheet
}

// Save data to a text file
function saveToTextFile(text){
var myName = Browser.inputBox("The file named here will be appeared in your Docs list.", Browser.Buttons.OK_CANCEL);
if (myName == "cancel"){
} else {
DriveApp.createFile(myName + ".txt", text);
}
}

// Get Hash values and file path from the sheet
function exportHashToText(){
var data = getData(); // get Data in sorted form from the sheet function

var text = data.map(function (row) {
if(row[0]==""){
hash_val='--------------------------------';
}else{
hash_val=row[0];
}
return hash_val + "\t" + row[1];
}).join('\n'); // join the values of data

saveToTextFile(text); // Save data to a text file function
}

// Get file path from the sheet
function exportFilenameToText(){
var data = getData(); // get Data in sorted form from the sheet function

var text = data.map(function (row) {
return row[1];
}).join('\n'); // join the values of data

saveToTextFile(text); // Save data to a text file function
}
39 changes: 39 additions & 0 deletions drive/md5-checksum/GoogleDriveHashToSheets.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Get the hash values of files in Google Drive and store it in Google Sheets
*/

function driveHash() {
var folderId="<folderID>"; // Null for root Directory or set Folder ID: https://drive.google.com/drive/folders/<folderID>?usp=sharing
var sheetName="Sheet1"; // Name of the sheet
var sheetId="<sheetID>" // Google Sheets ID: https://docs.google.com/spreadsheets/d/<sheetID>/edit#gid=0
var rootPattern=".";
var pathPattern="\\" // "\\" for "\" and "/" for "/"
if(folderId!=""){
var folder=DriveApp.getFolderById(folderId); // retrive folder by folder ID if not Null
}
var ss = SpreadsheetApp.openById(sheetId); // retrive Spreadsheet by Spreadsheet ID
SpreadsheetApp.setActiveSpreadsheet(ss); // get active Spreadsheet
var sheet = ss.getSheetByName(sheetName); // get Sheet by Sheet name in the Spreadsheet
sheet.clear(); // clear all data
listFolders(DriveApp,sheet,folder,rootPattern,pathPattern);
}

// recursive function to get hash values of all files and subdirectories
function listFolders(dApp,sheet,folder,path,pathPattern) {
folder = folder || dApp.getRootFolder(); // get the folder passed in arguments else start from root Directory
var files = folder.getFiles(); // get files in present folder

while ( files.hasNext() ) {
file=files.next()
md5_val=Drive.Files.get(file.getId())['md5Checksum']; // retrive MD5 hash from the file properties calculated by Google Drive
full_path=path+pathPattern+file.getName(); // retrive the path to the file
sheet.appendRow([md5_val, full_path]); // store the full path and hash value in Google Sheets
// Logger.log(md5_val + "\t" + full_path);
}

var subfolders = folder.getFolders(); // retrive subfolders to iterate over nested folders and files
while (subfolders.hasNext()) {
subfolder=subfolders.next();
listFolders(dApp,sheet,subfolder,path+pathPattern+subfolder.getName(),pathPattern); // recursive function to get hash values of all files and subdirectories
}
}