public
Last active

This is the full script solution for Stack Overflow Q14518410, "a google apps script to move or restore google drive's files from the trash" http://stackoverflow.com/questions/14518410/a-google-apps-script-to-move-or-restore-google-drives-files-from-the-trash

  • Download Gist
GDrive Trash Recovery.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
// See http://stackoverflow.com/questions/14518410/a-google-apps-script-to-move-or-restore-google-drives-files-from-the-trash
 
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the moveFilesFromTrash() function specified below.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Untrash All",
functionName : "moveFilesFromTrash"
}];
sheet.addMenu("Recover Trashed Files", entries);
};
 
/**
* Return string representation of a Document Type.
*
* @param {FileType} type Enum as defined in https://developers.google.com/apps-script/class_docslist_filetype.
* @return {String} String matching the provided type. "UNKNOWN" if type was not known.
*/
function docTypeToText_(type) {
var typeText = "";
switch (type) {
case DocsList.FileType.DOCUMENT:
typeText = "DOCUMENT";
break;
case DocsList.FileType.DRAWING:
typeText = "DRAWING";
break;
case DocsList.FileType.PRESENTATION:
typeText = "PRESENTATION";
break;
case DocsList.FileType.SPREADSHEET:
typeText = "SPREADSHEET";
break;
case DocsList.FileType.OTHER:
typeText = "OTHER";
break;
default:
typeText = "UNKNOWN";
break;
}
return typeText;
}
 
/**
* Return a comma-separated list of user IDs, from an array of Class User.
*
* @param {array} users Array of Class User.
* @param {boolean} skipOwner If true, do not include file owner in results
*
* @returns {string} Comma-sparated list of user IDs.
*/
function userNames( users, skipOwner ) {
var retval = "";
var startAt = skipOwner ? 1 : 0; // Owner listed first in editors & viewers lists
if (users.length <= startAt) {
retval = "none";
}
else {
for (var i=startAt; i<users.length; i++) {
if (i > startAt) retval += ", "
var userId = "";
try {
userId = users[i].getEmail()//.getUserLoginId()
} catch(e)
{
// Possible permission problem
userId = "unknown";
}
retval += userId;
}
}
return retval
}
 
/**
* Return an object containing relevant information about the given file.
* See https://developers.google.com/apps-script/class_file.
*
* @param {File or Folder} file Google Drive File or Folder object
*
* @return {Object} Interesting file attributes.
* <pre>{
* id: unique id (ID) of the folder or file
* name: the name of the File
* size: the size of the File
* type: The type of the file
* created: date that the File was created
* description: the description of the File
* owner: user id of the owner of the File
* otherViewers: list of user ids of viewers of the File, w/o owner
* otherEditors: list of user ids of editors of the File, w/o owner
* }</pre>
*/
function getFileInfo (file,fileType) {
var fileInfo = {
id: file.getId(),
name: file.getName(),
size: file.getSize(),
type: (fileType == "file") ? docTypeToText_(file.getFileType()) : "folder",
created: file.getDateCreated(),
description: file.getDescription(),
owner: userNames([file.getOwner()],false),
otherViewers: userNames(file.getViewers(),true),
otherEditors: userNames(file.getEditors(),true)
};
return fileInfo;
}
 
/**
* Untrash then move all files listed in spreadsheet.
*/
function moveFilesFromTrash() {
var fileList = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var targetFolder = DocsList.getFolder('cestisti'); // That's Italian for "basket", isn't it?
 
for (var i in fileList) {
var fileId = fileList[i][0];
var fileType = "file";
try {
var file = DocsList.getFileById(fileId);
} catch (e)
{
// Error getting the file. Is it a folder?
try {
fileType = "folder"
var file = DocsList.getFolderById(fileId);
}
catch (e)
{
Logger.log( "No File or Folder with id ("+fileId+")" );
}
}
// Untrash the file
file.setTrashed(false);
// Unlink any & all folders this file used to appear in
var oldFolders = file.getParents();
for (var j in oldFolders) {
file.removeFromFolder(oldFolders[j]);
}
// Move the recovered file to target directory.
file.addToFolder(targetFolder);
Logger.log(getFileInfo(file,fileType));
}
Browser.msgBox("Recovered " + i + " Files / Folders");
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.