Retrieving Google drive folder contents versus Google docs folder - What is Different? - google-apps-script

How would this script be modified? I would like to work this tutorial but I believe it was written before drive replaced docs.
Section 1: Embedding folders in a File Cabinet
Linking Google Docs Folders with Sites File Cabinet
Open the Script Editor in your Site.
Click More Actions > Manage Site.
In the left-hand sidebar, click Apps Scripts.
Click Launch Editor.
Copy and paste the following code into the editor.
function showFolderInSite() {
var files = DocsList.getFolderById("PASTE_FOLDER_ID_HERE").getFiles();
var page = SitesApp.getPageByUrl("PASTE_PAGE_URL_HERE");
for(i in files){
page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
}
}
Open your folder in Google Docs and copy the folder ID from the URL that is in the browser window. The folder ID is everything after the folder.#. portion of the URL.
For example, given the following example url
https://docs.google.com/?tab=mo&authuser=0#folders/folder.0.0B3xR7WfXrj7aOGY4Y2M3OWItZjAyYy00ZTM5LTg3MTgtNjNkYWZhZTZmYmU3
the folder ID is this section: 0B3xR7WfXrj7aOGY4Y2M3OWItZjAyYy00ZTM5LTg3MTgtNjNkYWZhZTZmYmU3. Note that it is everything after the folder.0.. The following graphic also shows the folder ID:

There are some minor changes in the way folders url are constructed but the idea is always the same : the ID is everything thats is different between two items of the same type (2 spreadsheets, 2 folders ...that's just how they build up the urls)
In this example below, the ID is just eveything after folders/
ie the line of code to get the files in this folder would be like this :
var files = DocsList.getFolderById("0B3qSFd3iikE3TkN2THNDUnAwX2s").getFiles();
Hoping this will help you to finalize your tuto.
Note that some url are more complex in that some parameters are added behind the ID string (it is actually the case most of the time ;-)

Related

Is there a way to use Google App Script to create a Drive file sharable in view-only but that can't be re-shared or downloaded?

I have just recently starting coding and using AppScrpits, and I have set up an app that among other things creates PDF files in a Drive folder and stores the view links in a Sheet.
These files should be accesible only by people with the same domain in view only, non-sharable and non-downloadable.
I set the access and permission as intenden and everything works fine, but the PDF files are still downloadable and sharable by the viewers. Normally I would set this option by right cliking the file and setting the configuration manually, but since I have to process a large number of files I wonder if there is anyway to do this with appscripts.
With the code below access and view permission works as intended, but I can't find any referance to set aditional configuration so that the file is non-downloable or sharable.
let pdf = DriveApp.getFileById(nuevo.getId())
.getBlob()
.getAs('application/pdf');
pdf.setName('placeholder'.pdf');
let file = ubicacion.createFile(pdf);
file.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.VIEW);
var ultimaFila = sheet.getLastRow() +1;
sheet.getRange('Hoja 1!B' + ultimaFila).setValue(file.getUrl());
Thanks!!!

change default saving location in "make a copy" of a Google Doc

Im sure you know the option, that, if you click on a link of a GOogle Doc, you can adjust the link with
/copy, and each time you open the document, you have the option to click on "Make a copy" and
a copy of the original file (Doc, sheet...) gets created and saved on "My drive" per default.
My question now is, can I somehow change this default path, where the files gets saved?
Like, can i say, save it per default in Google Drive folder xy, or can I even say, that
he shall ask in which of my Drive folder, the copy shall get saved?
Thanks for any hint
Try using makeCopy(name, destination).
makeCopy(name, destination)
Creates a copy of the file in the destination directory and names it with the name provided.
function myFunction() {
var files = DriveApp.getFilesByName('Sample Document');
while (files.hasNext()) {
var file = files.next();
var copyFolder = DriveApp.getFolderById('FOLDERID')
file.makeCopy('Sample Document Copy',copyFolder)
}
}
Note:
The folder must be selected using folderID not byName( based on experience and the tutorial example)
Valuable Sources:
Developer Guide
tutorial - How to use the makeCopy(name, destination) function (This is very useful!)
Hope it helps.

How to access to a subFolder with google app script?

Im using the script editor in a Google spreadsheet, and I want to create a folder inside a root folder , already created. I've tried to follow the official notice of the API DriveApp, but without success.
Here is my code :
function createSubFolder(rootFolder,name){
DriveApp.getFoldersByName(rootFolder);
DriveApp.createFolder(name);
}
I've tried also ( following this old post working with the old version of DriveApp : i.e DocsList) :
function createSubFolder(rootFolder,name){
DriveApp.getFoldersByName(rootFolder).createFolder(name);
}
It seems to be very easy but I don't see where am I wrong.
Does anyone know the way to do it?
Thanks
You need to assign values to a variable.
function createSubFolder(rootFolder,name) {
var AllFoldersWithThisName = DriveApp.getFoldersByName(rootFolder);
var theFirstFolderWithThisName = AllFoldersWithThisName.next();
theFirstFolderWithThisName.createFolder(name);
};
In Google Drive, you could have 1,000 folders all with the same name. Same thing with file names.
The getFoldersByName() method returns:
FolderIterator — a collection of all folders in the user's Drive that have the given name
You need to get one of the folders out of the collection of folders. That is done with the next() method.

Embedding folders in a Google File Cabinet and including files in the folders

Refering to the link and the section 'Embedding folders in a File Cabinet' I've used this script to control and update a file cabinet in my google sites page. However, it only works on the root folder and doesn't add files that are in the sub folders. Please could you help me understand how you pick this up and also control the file cabinet so that it displays this easily?
This is the current appscript code:
function showFolderInSite() {
//var files = DocsList.getFolder("xxxxxxx").getFiles(); commented out this line as using getfolderbyID.
var files = DocsList.getFolderById('xxxxxxxxxxxxx').getFiles();
var page = SitesApp.getPageByUrl('https://sites.google.com/x/xxxxxxxx/xxxxx/xxxxxxxxx');
var attachments = page.getAttachments();
for (i in attachments) {
attachments[i].deleteAttachment();
}
for (i in files) {
page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
}
}
I just tried to attach a picture but apparently I need some reputation points to post images.. how do I get those?! this is the link to it incase it does work <> In the meantime the picture shows the folders have been added but there are no files when you expand them.
Edit 5/8/14: No replies yet (hope someone can help soon). Some ideas of mine... Do I need to track what is a file and what is a folder and then assume that when the array is populated what comes through after a folder will be what is found in that folder. One problem with that is how do you differentiate a folder in a sub folder.
var files = DocsList.getFolderById('xxxxxxxxxxxxx').getFiles();
This row returns all the files of the first level in the folder. It does not retrieve any subfolders or their files. This is the same for DriveApp note that Docslist is deprecated
So getfiles() from DriveApp:
Gets a collection of all files that are children of the current
folder.
This means that you need to recursively fetch all the subfolders and their files. Also note that folders are also files in Google Drive, they only have a different mime type, application/vnd.google-apps.file
Your current script fetches the files, including the folders of the current folder, and creates links to those files and folders. It does not upload those files into the file cabinet, only refers to them. If that is the goal, you only need to recurse into the subfolders. Here you also need to note that file cabinets only go one level deep, you will not be able to create a similar structure using the file cabinet if the folders are more than one level deep.

When adding a file to a folder, how to make it not default save in Google Drive?

In my script I have template docs in the main drive, but when the copy is created and populated with my excel data, I want it to be saved in a folder in drive. I have the script do that, but it has one copy of the file in the Main Google Drive, and the other in the folder I want it in. If I delete one of the copies, it deletes both.
Is there any way I can have it save automatically in the specified folder without also being in the main drive folder?
Folders in Google drive are not exactly like forders in a computer : having the file in your 'root' folder and in another folder doesn't mean there are 2 files, but rather 1 and only file with 2 labels... that's why you can't delete one without deleting the other !
The solution is simply to play with these labels in the script, here is how it works : (I commented each step to make it clear.)
function othertest(){
folder=DocsList.createFolder("MyFolder"); // or getFolderById or whatever other way to get your target folder
var file=DocsList.createFile('File2', 'Empty');// just an empty file for test but this would be your file copy that you want to "move"
file.addToFolder(folder);// put it in the folder
file.removeFromFolder(DocsList.getRootFolder());// and remove from the root
}
The other possible solution is to create the file directly in the target folder since the folder object supports the createFile method. (Not sure though that you can do it in your specific use case)
here is an example, you can see that the file is not in the root folder.
function createFileinFoldertest() {
var folder = DocsList.getFolder('test')
folder.createFile('Empty test fileName','nothing in there')
}