Use DocumentApp to create in a specific folder? without moving and deleting - google-apps-script

I'm currently moving then deleting. Is it possible just to create the file in the correct folder. I don't seem to be able to make DocsList interact with DoccumentApp
var targetFolder = DocsList.getFolderById("0B2o_9r2k_45fUFpja0FRWXc4YWM");
var mynewDoc = DocumentApp.create(datasource[row][2]);
'var gFID = DocsList.getFileById(newFileID)
gFID.addToFolder(targetFolder);
gFID.removeFromFolder(DocsList.getRootFolder());`

Documents within Google Apps are not particularly in a folder. We can think of folder more as labels, even though the engineers have made stride to make it look like the paradigm we are used too. So files can exist in multiple folders and creation happens in the root folder, after which you can add them to many folders as you want, and as you rightly do remove them from the root folder.
I would suggest you start using the Drive Services instead of DocList. DocList was created before Google Drive and while it continues to work, and no deprecation has been announced, it would be safer to move to Drive which offers a larger and more comprehensive API. (DocList is still labeled as experimental, while Drive is not)

Another working variant of the code follows:
// content, in my example, is a Blob with a mail attachment
var doc = DriveApp.createFile(content);
doc.setName(title);
var fld = DriveApp.getFolderById('the destination folder id');
fld.addFile(doc);
The new file appears under the desired folder.

Related

move generated google Form to specific google drive folder using google apps script

I am creating a few forms using Google Apps Script which automatically generates in my root (My Drive) of my Google Drive.
I want to move these forms to a specific folder of a Shared Drive.
I googled about moving files (or any object) to another folder, most of them are suggesting that copy the file to a specific location, then delete from the source location.
But in this case, my formIds are changing which I don't want to change.
Is there any better way to move files (in my case Forms) to another location in Google Drive?
As a developer you should always favor canonical sources over 3rd party tutorials that pop-up in google search. The official documentation for the Drive API has guides describing how to move files from one drive folder to the next. I suggest you start there.
the following code should work for moving any file, including forms, to a destination folder in Google Drive:
var fileId = '1234567890abcdefghijklmnopqrstuvwxyz'; //<--replace string with your file ID
var destinationFolderId = 'zyxwvutsrqponmlkjihgfedcba0987654321';//<--replace string with your destination folder ID
function moveFile(fileId,destinationFolderId) {
var destinationFolder = DriveApp.getFolderById(destinationFolderId);
DriveApp.getFileById(fileId).moveTo(destinationFolder);
}

Using Google Apps Scripts to generate shareable links of new files

I recently discovered this Google Apps Scripts feature and am wondering if my issue can be addressed by using that.
Let's say I have a machine generating new files (screenshots or videos taken from a capture card) that are being saved in a folder that is sync'd to my Google Drive. What I would like to do is somehow automatically generate a shareable link for each NEW file that gets added and send it to an email address (may or may not be a Gmail address).
Is this something that I can use Google Apps Scripts for? I tried looking into batch files first but I can't generate the shareable link automatically (or couldn't figure out how).
I don't have any code yet, just looking at potential approaches.
Thanks in advance!
Yes almost everything is possible in GAS. However, you are likely to find better answers when you post some code that you have tried yourself before asking the answer here.
However, if the machine automatically saves the files in a folder in google drive you can scan that folder with a script and find the links. Attach a trigger to it with a, for example, 5 minute interval and write those links on a spreadsheet.
an example:
function links(){
var sheet = SpreadsheetApp.getActive.getActiveSpreadsheet();
var folder = DriveApp.getFolderById('put google folder id here');
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var vals = file.getUrl();
sheet.getRange(your range you want to paste the link).setValues(vals);
}
}
This will find the links of all the files in the folder and paste that onto a google spreadsheet where the code is linked to.
Use that spreadsheet as a database and send an email by using that database.

Can I write a script on AODocs to automatically change the permission on folders?

I am using AODocs for company work and we would like to be able to lock folders that are not relevant anymore, without having to delete them in case we need specific information later. However, leaving them open might let people add things inside we dont want. I know it isn't possible to lock folders individually in AODocs but that you have to use the permissions.
Doing this all by hand knowing we have over 70 folders per month to close would be a tall task to ask anybody to do. Therefore I wanted to know if it was possible to create a script that automatically changes the permission on folders that are selected?
Following a quick check of the AODocs documentation, here is what I advise you:
You will need to set up your library in flexible permissions and run a script to force the folder (and its content) to be in read-only when you decide it.
You cannot "tag" folders to know which ones to lock, so you should perhaps use a tag in the name of the folder like "[locked]".
AODocs also has a forum about custom script questions (you need to be signed into the support platform):
AODocs team and engineering are closely watching this community.
Custom script documentation is not publicly published but you can surely request access there.
Hope it helps.
I suggest you use a Google Apps Script to develop something like that.
If you create an Apps Script doc on your Drive, you can manage all of the folders you want. Try this script:
function myFunction() {
//take all folders in Drive
var folders = DriveApp.getFolders();
//loop on folders
while (folders.hasNext()) {
//take the current folder
var folder = folders.next();
Logger.log(folder.getName());
/*I take all editors, but it's possible to take even all viewers inserting var users = folder.getViewers();*/
var users = folder.getEditors();
//loop on users
for(var i=0; i<users.length;i++) {
//revoke all permissions to current user
folder= folder.revokePermissions(users[i]);
}
}
}
The result is: all editors are removed.
Furthermore, it's possible to schedule the script execution, creating a trigger
**N.B.**This method does not block users from accessing the Folder if they belong to a class of users who have general access — for example, if the Folder is shared with the user's entire domain.

Unable to access certain Google Drive folders using Zend GData libraries although folder is listed in My Drive

I'm querying Google Drive and passing in a folder name if which I want to list the folders/file contained with in it. My code works, I can view content of some folders, but for certain folders that are visible within the Google Drive web UI, I don't get any entries returned, but there is content.
I'm using Zend_Gdata_Docs and Zend_Gdata_Docs_DocumentListFeed is doing the heavy lifting, but the _entry array which should contain the folder contents is empty when it should contain an array of Zend_Gdata_Docs_DocumentListEntry objects.
I'm passing the folder by appending the folder name to https://docs.google.com/feeds/documents/private/full/-/some-folder-name
Whats proving to me it a Google issue is if I rename the folder causing issues and create a new folder with the same name my code works and I see the contents.
All very strange any pointers before I ditch Google Drive and move to Dropbox?
You need to pass the folder id not the name
Looks like the issues was with the visibility of the files and folders in Google Drive. As said before the code is good all things pointed to something up with Google Drive.
To make the files and folders visible again I did the following;
From within drive.google.com select the the files/folders in question
From the 'More' button choose 'Share'
Change the 'Who has Access' settings, click 'Save' then 'Done'
Revert 'Who has Access' to previous setting if required.
So the visibility on certain folders is being lost or corrupted somehow.
UPDATE
I wrote a Google Apps Script that updates the permissions of the sub folders and call it from my web app. The script need to created as Web App and deployed for use. THe script is called via regular script src (as XHR isn't allowed with Google Apps Scripts)
function doGet(request) {
var folders = DriveApp.getRootFolder().getFoldersByName(request.parameters.folder);
while (folders.hasNext()) {
var folder = folders.next();
var subFolders = folder.getFolders();
while (subFolders.hasNext()) {
var subFolder = subFolders.next();
subFolder.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.VIEW );
}
}
var result = '';
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JAVASCRIPT);
}
Hope this is of some help to someone.

How The mimetype of google drive affects my real files?

First of all, thanks for reading.
I was trying to figure out how the mimetype that google docs applies to the files can affect my "real" files in my computer.
I have two files with the same extension VBS (yes, visual basic script).
One was uploaded by me, and the other one was created with the code mirror google drive app.
I can open the second one with any google drive aplication for editting, I suppose it have the text mimetipe, but I can't open the other file (remember, the same vbs extension).
My question is, how can I change the mimetipe of all my VBS files, and how it will affect to the real files when synq with my computer?
Well, as I told before, I don't understand why google is changing the mimetype of a file based on his extension, but here is a workaround which could be useful to people in my situation.
The easiest example is when you have script files in your drive folder, which are not other than plain textfiles (as visual basic script, or even bash files) but google drive assign a different mymetype based on its extension.
This function makes a copy, because I'm not 100% sure it is safe.
At the end of the function I have to add to a new folder (where the original file is) and remove the old one (root folder) because I don't know how to just move it. If anybody know how to improve it feel free to do it.
function changeMymeType(fileId){
var file = DocsList.getFileById(fileId);
var content = file.getContentAsString();
var name="copy_"+file.getName();
var folder = file.getParents();
var newfile = DocsList.createFile(name, content, "text/plain");
var oldFolder = newfile.getParents()[0];
newfile.addToFolder(folder[0]);
newfile.removeFromFolder(oldFolder);
};