Skip to content
Create a gist now

Instantly share code, notes, and snippets.

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
// 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");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.