Google Drive - App Scripts - How to delete a file from the trash - google-drive-api

I am writing a google drive apps script to poll my google trash (using the DriveApp.getTrashedFiles method).
However, although this correctly lists the files, I can find no way to actually delete the file.
I want to be able to delete files every hour or day using a trigger and not have to rely on the 30-day file deletion method, as I find my drive fills up before then.
Can anyone advise how I can use App Scripts to delete a file I've identified in the trash or to simply empty the trash? It used to be supported but seems it's changed !!
Thanks

Related

How to use GAS functions in a spreadsheet created by a service account in a team drive?

In my company we use a team drive, where we store a template spreadsheet which our API server copies a lot of times. The spreadsheet contains a few custom Google App Script functions which should be copied as well.
Right now when the API copies the spreadsheet, the scripts stop working in the copy. According to this issue https://issuetracker.google.com/issues/36762799 , service accounts can't execute scripts. However, in a Team Drive there are no owners. I even checked the permissions with Permissions.List from the Drive API and I can see that everyone is an organizer and the API does not allow me to specify another user as an owner, but the scripts still won't work (Error: Please try saving the project again). On the other hand, if I go and copy the generated sheet by hand, scripts execute just fine.
I tried moving everything to an add-on, however to my displeasure, it appears that service account-owned files can't execute add-on scripts as well (if the file is created by the bot, custom function names simply fail to resolve).
Does anyone know of a way to deal with this situation? Is there a more elegant solution besides using an actual account for the copying if that's even possible to do programmatically?

Creating trigger in google script to save file to drive

I'm trying to modify this script so that once a day it runs exporting the result to my drive and overwriting the file that is there. I'm completely new to google scripts so this might be a very basic request. To clarify, i know how to set a tim trigger. I'm looking for how i edit the script so that it writes then overwrites the file to my drive on the timetrigger

How do I restore revoked permissions on a google-apps-script?

I mistakenly revoked permissions on a google-apps-script that ran on a time trigger on a google spreadsheet from here: https://myaccount.google.com/permissions . I now receive an email that the script fails to run. Going to the spreadsheet and editing the script fails to bring up the script, presumably because of the lack of permissions, but no error is shown, just a blank script editor. I am able to see the trigger to the script (here: https://script.google.com/macros/triggers?id=some_id ), but I am not able to get to the script.
How can I restore or reinstate the permissions to this script?
Or how can I get the code to this script? Where is it stored?
If you simply revoked permissions it will not remove the actual script/coding. So it may not be attached to the spreadsheet you are opening. Perhaps you are opening the wrong spreadsheet, or it is a standalone- If you type type:script into your google drive search it will show all scripts that are not bound to a spreadsheet.
As far as restoring you will just want to run it, once you find it.
I was successful. I'm not sure what did it but: previously I was not able to view the script, looking at the revision history doesn't allow me to get access to the menu and thus I was not able to get at older versions of the script. But making a copy of the spreadsheet then allowed me to view the script again, both in the original and copy of the spreadsheet. Running the script then requests for permission, restoring the permissions!
I really don't know if the lack of access to the script was a temporary condition or required that I make a copy of the spreadsheet, but this was the sequence of events that worked for me.
Thank you #OblongMedulla for making the suggestions.
You can simply go to your Google Profile - Manage Your Google Account - Security - Third Party Apps and revoke the permissions. If you want to use the app again, you'll have to run the script again from appscript and grant the permissions.

Prevent deletion of files in Google Drive

I am trying to prevent deletion of all files uploaded to specific folder in Google drive. The idea is several people will be able to upload files to this drive, but they should never be able to delete it.
I thought of putting a Google apps script to achieve this, like letting the script run every x interval and getTrashedFiles and set isTrashed to false. But what if the user is able to delete it from trash too?
Is there a foolproof way?
Or is Google Drive not the right answer at all?
Any other service can help me which is simple for people uploading the files?
TIA
its possible but the way you are doing it isnt practical or tamperproof.
instead write a little apps script webapp that allows uploading files to the given folder where you are owner and only writter.
publish it to run as you with permissions to anyone (or your domain). they will be able to add files and view but not change or remove them (and you will be the owner of all uploaded files).
to support uploading files already in drive (move them to the folder) is also possible but with more code.

I need a script to monitor a specific Google Drive folder whenever there is a change and notify me by email

I have been trying all day and I found this:
http://www.jellybend.com/2012/12/19/monitor-google-drive-folders-with-google-apps-script/
The attached script worked only partially for me. It doesn't respond to change to the subfolders even there are files inside the subfolders (eg. rename/delete the subfolder). It also seems to have errors if I delete and re-add the same file to the folder again, it just doesn't email me for the newly added "old file".
I also found this:
https://developers.google.com/drive/v2/reference/changes/list#examples
but unfortunately I am not really sure what those parameters are and I am just inexperienced in writing something like that.
Any help will be greatly appreciated! Thanks!
The file monitoring code at that link, is an Apps Script bound to a Sheet. An Apps Script can be bound to a Sheet, Doc, Form or Site. An Apps Script can also be a stand alone application. So, any code you may want to write, does not need to be in a spreadsheet.
An Apps Script can be set up to have a Time-driven event trigger.
There is also a Script Service to build Clock Triggers.
ClockTriggerBuilder Class
Using Time Driven Event Trigger, or a Clock Trigger you could use the getSize() method to return the amount of disk space used by the item:
Class - Folder - getSize Method
// This example logs the first file's size in bytes
// Note: This can also be used on a folder to get the size of its contents
var file = DocsList.getAllFiles[0];
Logger.log(file.getSize());
Of course, you would need to know what the original size of the folder or file was, and so you would need to store the current size somewhere. You could create a file for storing that information, or use the built in database that Apps Script has.
For storing your historical folder or file information you could use ScriptDB.
ScriptDB
Quote:
ScriptDB is a JavaScript object database for Google Apps Script. Each script project gets a database, which the script can use to save, update, and search JavaScript object data.
You could write historical file and folder info to a spreadsheet or document also.
Depending on who owns the file, and who is accessing the file, permissions would need to be granted, or you'd need to use oAuth2 to authenticate who has access to the file and folder information.
If you can't write all the code yourself, you could set up a shared Apps Script file, or find some other way to have people collaborate on the project.
I've been researching this on my own for some time and came up with a script bound to a spreadsheet as well. Hope it can help someone, as it seems that a lot of people are looking for something similar. My script is monitoring a folder and sends notification to all the current viewers of the folder as soon as there is new file added. Trigger to run the script could be set depending on the needs of the user. In my case hourly worked just fine.
Spread Sheet with the script: https://docs.google.com/spreadsheets/d/1CzVADjUTT2d9Y5OGDOnv37mCaO49kPt4RmnyZgzjTKA/edit#gid=0
If you would like to try this script just make sure you are logged in with your google account when you open the link and you should be able to make a copy of the spreadsheet.
This Google Script will send an email notification to you or other email addresses when a file in a Google Drive folder has been added, renamed, changed, or modified. http://baumbach.com/google-script-2/