move generated google Form to specific google drive folder using google apps script - 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);
}

Related

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.

is it possible to download GMail attachment to local folder?

i want to download attachment from gmail and save it to local folder using google script. i did some research about this and i could't find any solution.
so far, i managed to save gmail's attachment to google drive only.
var attachmentBlob = attachment[0].copyBlob();
var file = DriveApp.createFile(attachmentBlob);
folder.addFile(file);
or, is it possible to create a google script to auto download file from google drive?
need some advice.
As #Sujay already mentioned you cannot download a file to a local folder using Google AppScript because GAS runs server-side.
You have already taken a good step with the code to save the attachment as a file to your Google drive.
To answer your sub-question 'is it possible to create a google script to auto download file from google drive?', you do not need a script to do that, that is what the Google Drive Desktop App (https://www.google.com.ng/drive/download/) does exactly.
It syncs all the files you add to your drive to your local-pc. You can also edit Google Drive preferences to sync select folders only, in your case that might be the drive folder referenced in your folder variable.
GAS runs on the server side, and only has access to Google's internal architecture. To save locally it'll need to create some kind of a blob and trigger a download within your browser. not sure if you can do that.

Move a Google Drive folder to a new location using GAS

Does anyone know how to move a Google Drive folder to a different location using Google Apps Script?
You can make the content of one folder available inside another folder with the addFolder() method - the content will now be available inside the original folder as well as the target folder.
function moveFolder() {
var source = DriveApp.getFoldersByName("source").next();
var folder = DriveApp.getFoldersByName("target").next();
folder.addFolder(source);
}

Use DocumentApp to create in a specific folder? without moving and deleting

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.

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.