Does anyone have any suggestions for how to remove a file from shared with me? I need to do this without actually revoking access, as the people are added via a contact group, so only removing 1 person will not work.
Things to note:
I know using this can find the file:
var files = DriveApp.searchFiles('sharedWithMe');
//then I set file = what I'm looking for.
However, this will not remove the file from shared with me:
DriveApp.removeFile(file);
I've also tried brute forcing by patching the metadata, except it was to no avail.
In addition, it is wrong to assume that shared with me is a folder, as when listing parent folders the file returns nothing.
Only DriveApp cannot completely remove files. So it is necessary to use Drive API. "removeFile()" is used for changing parent folder information. It cannot remove files. I prepared 2 samples for removing files. If you are owner of the file, you can remove it.
Move file to trash box and empty trash :
var file = DriveApp.getFilesByName("filename").next();
file.setTrashed(true)
Drive.Files.emptyTrash();
Remove directly file :
Drive.Files.remove("fileID");
It was confirmed that above script can remove file even if other users are using.
For using above script, it is necessary to enable Drive API at Advanced Google Services and Drive API at https://console.developers.google.com/ Developers Console Project.
Files without parent folder :
If you had used "removeFile()", files without the parent folder may be existing in your drive. You can confirm them following script. This can retrieve file name and file ID of files without the parent folder.
var result = [];
var files = DriveApp.getFiles();
while (files.hasNext()) {
file = files.next();
if (!file.getParents().hasNext()) {
result.push([file, file.getId()]);
}
}
Logger.log(result);
Related
I am trying to create a question bank on GitHub Pages.
I load a specific file using:
$(document).ready(function() {
var theQuestion = "./questionsBank/"+"question-4"+".html"; //path to load
$("#question").load(theQuestion); //loading the question
});
Now, there are many files in the questionsBank directory and I would like to have a script picking and loading a random one, but I don't know how to do in JS.
How do you retrieve all filenames in questionsBank directory?
I would like to do something like below but don't know how to read filenames in a directory into an array:
var questionFolder = './some directory';
var questionFiles = [];
questionFiles = readingFilesandPopulatingIntoArray(questionFolder); \\how to do this?
var folderSize = questionFiles.length ;
randomNumber = Math.floor(Math.random() * folderSize);
randomQuestion = questionFiles[randomNumber];
$("#question").load(randomQuestion);
As commented by #Lawrence Cherone, the only way of getting the contents of a remote directory is by having an index of it.
Of course Apache has it's own directory scanner, which if configured ok, can render an HTML page with its contents. Then you could fetch that index and loop over the file links in it.
But GitHub Pages does not generates such indexes, so you need to generate it by your own. To do so, you need to do it during the build/deploy process of your page (which we don't know). There, you can add a NodeJS script (or whatever other language you prefer to use, like a plain bash script) using, for example, node's fs dir.read() to get the files list in ./questionsBank/ directory and generating a file to save it somehow (for example, a JSON file containing an Array).
Finally, you can include it directly in your code during the build process by importing it somewhere, or fetching it as you'd fetch any other URL containing a JSON (or whatever other format you decided to use).
I'm having trouble with a WordPress plugin I've been working on. A JS file is loaded as a resource with each page/post opened, which in turn has a request to load the contents of an HTML file.
Being that the page/post directories change frequently, I'm having a difficult time making the jQuery dynamically pin down the location of the file (even though it's in the same location as the rest of the plugin resources).
An example:
jQuery('body').append('<section id="asub00LOAD"></section>');
var url = jQuery(location).attr('hostname');
var dir = url + '/wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';
jQuery('#asub00LOAD').load(dir);
That was placing the whole URL path to the file after the local install ("root.com/CHEETOS/" in this case):
After which, I did this, which works fine for the root directory only:
jQuery('body').append('<section id="asub00LOAD"></section>');
var dir = 'wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';
jQuery('#asub00LOAD').load(dir);
After you venture to another page, obviously the directory location is wrong again.
I tried to place some PHP into my JS file before so I could take advantage of the $plugins_url feature, but that became very convoluted and it's hard to track any errors without a PHP console to work from...
I hope someone here will have a solution for me!
The first example probably fails because there's no http:// (or //)
Use var url = "//" + location.hostname;
The second example should work everywhere if you use a root-relative path:
var dir = '/wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';
I am attempting to load a series of Images from a Shared Dropbox Folder like so:
function getSprite(raw) {
var sprt = new Image();
sprt.crossOrigin = '';
sprt.src = 'https://dl.dropboxusercontent.com/s/k1v7iv85vntx107/AABOD-CfE3A5sQo0RPPmRmmJa/ground1.png' + (raw ? '?raw=1' : '');
return sprt;
}
The folder is shared, and Dropbox says that 'People with Link can View'. I have tried to do the same with Google Drive, but I get a Cross Origin Error there.
EDIT: I just tried to share one of the files individually, and it worked. DO I have to now go through and do this for each file in the folder? I thought If I just share the folder I should have access to all its contents.
ERROR MESSAGE:
GET https://dl.dropboxusercontent.com/s/k1v7iv85vntx107/AABOD-CfE3A5sQo0RPPmRmmJa/characters/triggerman/up.png?raw=1 403 (Forbidden)
It looks like the original shared link you had was:
https://www.dropbox.com/sh/k1v7iv85vntx107/AABOD-CfE3A5sQo0RPPmRmmJa?dl=0
This is a shared link for a folder. Note that you can't just modify it directly to get shared links for individual files inside that folder though, which is what you appear to be trying in your question.
To get the individual files, you have a few options:
Manually get the shared links for each file via the Dropbox web site, as you mentioned.
Use the API to individually but programmatically get shared links for each file: https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings
Use the API to download the files in the original shared link by specifying the path inside the folder: https://www.dropbox.com/developers/documentation/http/documentation#sharing-get_shared_link_file This is likely closest to what you're looking for.
I don't think this has much to do with JavaScript. Go Incognito and take a look at it because all I can see is a 403 error from my browser.
I'm currently creating files using drive.makeCopy(Objects[i][1]); where Objects is the name of the various files I want to make from a template.
The script works fine in making these files from a template however after each one has been made I would like to open the new file, hide some sheets in it and to set its permissions. The addresses for the permissions are held in Objects[i][3] but I cannot work out how to open these new files, then make the changes accordingly and then close the file.
I'm assuming by drive.MakeCopy() you are referring to File.MakeCopy(). This is easy, the MakeCopy function returns the File object it has created. You can then set permission this File object using functions such as addEditor(), addViewer(), setSharing(), etc.
So if Objects[i][3] contained the email address of someone who should be able to edit the new file, then you could do:
var new_file = drive.makeCopy(Objects[i][1]);
new_file.addEditor(Objects[i][3]);
You don't really need to "Close" the file after working with it, Apps Script takes care of this automatically.
See:
https://developers.google.com/apps-script/reference/drive/file#addEditor(String)
https://developers.google.com/apps-script/reference/drive/file#setSharing(Access,Permission)
https://developers.google.com/apps-script/reference/drive/file
I am writing a script for my company that will automatically make a copy of a file in my own drive, then transfer ownership of the copy to a list of users from a spreadsheet. The script works completely except for that the file is created in the user's "All Files" and doesn't show up in their My Drive! Transferring ownership is not the problem I'm having, the problem is the location of the transferred file in the new owner's Drive.
I've tried sending it to their root folder but that didn't work, I'm not sure why? It sends it to My Drive for me, but it is copied into a folder that is already in My Drive for me (because I copy the file from a file that is in a folder in my drive already) but for anybody else on the list that the file is newly shared and transferred to, moving it to root does nothing and it remains in All Files without any labels on the file.
Does anybody have an idea how to move it automatically?
Here is the code that I've been using that works for my own My Drive but nobody else's on the list:
//Take new file out of parent folders
var folders = newDoc.getParents(); //get parents
for (var j = 0; i < folders.length; i++){ //loop through & delete parents
DocsList.getFileById(newDocId).removeFromFolder(folders[i]); //using DocsList
}
var root = DocsList.getRootFolder(); //Get root
DocsList.getFileById(newDocId).addToFolder(root); //add to my drive
edit: I was told that transferring ownership should automatically put it into the My Drive file but that isn't happening??
edit 2: I've tried using "addFile()" which SHOULD add it to My Drive folder in the user's folders, but it only adds it to the my drive of the person running the script and the rest of the users STILL get it only in their "All Items" folder. This is driving me nuts, someone please help ;C
So after a few days of driving myself INSANE and asking around literally everywhere I could think of we finally got word that this is an effect of the new Google Drive UI Rollout, so apparently right now it's just like THIS now, until google fixes it :C
WHAT AN UNSATISFYING ENDING.
Unfortunately, it's not going to be possible exactly the way you describe.
As you've found, the file ownership can be changed. That's because you own the file and the file is accessible by Script through the authorization you granted to your Drive account.
Placing a file into a location on a different user's Drive is not possible because the script has no privilege to set a location on a Drive it knows nothing about.
However, it would be possible to set the location by running a script under each user account, but obviously that's a little arduous to setup and maintain.
One other option: If you use GAM (Google Apps Manager) it IS possible to make changes across accounts. This is because GAM is run under admin user access. Example from the GAM site:
This example updates the "My Ramblings" file to be starred and placed under a folder called "Brilliant things I've said" (assumes a folder by that name already exists for the user)
gam user bsmith#acme.com update drivefile drivefilename "My Ramblings" starred true parentname 'Brilliant things I've said'