Using Google Apps Scripts to generate shareable links of new files - google-apps-script

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.

Related

Change upload location of a Google Form to a Google Sites

I need help please!
I am making a repository of proyects in Google sites. This repository is a Data base in Google Sheets feeded by a Google Form, and has an interfase in Google Sites using Awesome Tables.
My problem is that each Google Form has a file upload which the documents are automaticaly saved into my drive, but I need that this files are saved automaticaly to Google Sites so anyone can consult and change them.
Google Sites has specific buttons that can upload files, so I reckon that I can make a script saves the files directly to Googel Sites or use the link of the file in my Drive and activate this button to upload the Files.
I figured I can use this script and add the Google Sites destination, I just don't know how. Sorry for my lack of Script knowledge.
function onFormSubmit(e) {
Logger.log(JSON.stringify(e));
var ft = e.values.slice(1); //This is where are the file url were located
var fA = ft[0].split(',');
for(var i = 0; i < fA.length; i++) {
var file = DriveApp.getFileById(fA[i].slice(fA[i].indexOf("=")+1));
Drive.Files.update({"parents": [{"id": "FolderId"}]}, file.getId());
Utilities.sleep(2000);
}
}

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);
}

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.

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);
};