Get ID of newly created Folder in Google Apps Script - google-apps-script

As part of a Google Apps Script project, I'm trying to move the active spreadsheet and several uploaded files to a new folder that is created within a shared directory.
I have been able to create the new folder with:
DriveApp.getFolderById(parentFolder).createFolder(rename);
But to be able to move the files where I need to know the ID of the folder that's just been created.
The rest of the code works ok - if I just put in a string as another folders ID everything gets moved. I'm just stuck on actually finding that new folder ID.
Any ideas?
ta

You can retrieve folder ID using "getId()". For example, your code can be written as follows.
var id = DriveApp.getFolderById(parentFolder).createFolder(rename).getId();
If the folder name is only one on Drive, you can retrieve folder ID from folder name like below.
var id = DriveApp.getFoldersByName(rename).next().getId();

Related

Application or script that can generate folder structure + make a copy of a google sheet?

For my work, each time we bring on a new client we need to create a new folder structure in google drive (the same structure for every account) and in one folder we generate a copy of a google sheet that we have. This is rather time consuming, so was hoping there was a way to easily duplicate a google drive folder structure and have it make a copy of the google sheet that we include in one of the folders.
The perfect result would be to easily copy/duplicate an exact folder structure (obviously renaming it for the client) and then have the folder have the copy of the google sheet in the correct folder already
Creating a folder structure
function createClientAccountFolder(brandName) {
//var brandName=brandName || 'BrandName';
var Template1=DriveApp.getFileById('TemplateId');//You need to add the id of your template here
var acctRoot=DriveApp.getFolderById('Accounts Root Folder Id');//you need to add the id of the root accounts folder here
if(brandName) {
var mainFldr=acctRoot.createFolder(Utilities.formatString('MainFolder-%s',brandName));
var liveFldr=mainFldr.createFolder(Utilities.formatString('%s-Live',brandName));
var editedFldr=mainFldr.createFolder(Utilities.formatString('%s-Edited',brandName));
var imgsFldr=liveFldr.createFolder('Images');
imgsFldr.createFolder('Ecommerce');
imgsFldr.createFolder('LifeStyles');
imgsFldr.createFolder('Model');
Template1.makeCopy(liveFldr);
}else{
SpreadsheetApp.getUi().alert('No Brand Name Specified')
}
}
You could enhance this script with a dialog that allows you to select different folders and possibly even add additional root account folders and the same with templates. But that is considerably more involved than what you have now.

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

Overwrite File in Google Drive

I'm using this Google Script:
Gmail2GDrive
and it works like a charm except that I need it to overwrite the existing file as I'm intending to link to it and share it.
How would I need to modify this script?
You can't really overwrite a file in Google Drive. (Not that I am aware of.)
Each file has its own unique ID which means the name of the file is kind of irrelevant. (When attempting to override a file in the Windows Explorer terminology.)
That being said I have used this in the past to move files using Google App Script. Hope it helps:
//Get the ID of the Destination Folder
var destinationFolder = DriveApp.getFolderById("Your Folder ID");
//Create a copy of the Merged Sheets Temp and move to the Desitnation Folder.
DriveApp.getFileById(FileToMove.getId()).makeCopy("Give the File a name", destinationFolder);
//Move Merged Sheets Temp to Trash
var tempMergeDocument = DriveApp.getFileById(FileToMove.getId()).setTrashed(true)

Google Drive Service. Copied file to group drive folder. File is blank

I'm having an issue with the file handling of a Google Sheet that is produced from another Google Sheet when I run a google apps script. When I run the script, a new output file is produced and sent to folders with a specific label. The goal is to have the output file added to a specific folder in a group drive. The script and files regarding my problem all reside on the group drive too fyi.
When I run the script this is what happens.
A file is created within my personal Google Drive because I have a folder with the same exact name as the one on the group drive (this was my test environment for the script). This file on my drive has all the desired output within the Google Sheet. A file was also being added to the root of my drive, but the script now removes it from the root.
A file is created on the group drive, but it is completely empty. The file is correctly named, but there are no contents within the actual spreadsheet.
Here is the script that is handling the output file:
var sourceFilename = ss.getName();
var splitSourceFilename = sourceFilename.split("LMS");
var targetFilename = splitSourceFilename[0] + "Allowed_Layers_Vx";
// move the new spreadsheet to Allowed Layers Lists folder
var folders = DriveApp.getFoldersByName("Allowed Layers Lists");
while (folders.hasNext()) {
var folder = folders.next();
var ssNew = SpreadsheetApp.create(targetFilename);
var copyFile = DriveApp.getFileById(ssNew.getId());
folder.addFile(copyFile);
DriveApp.getRootFolder().removeFile(copyFile);
}
Some other information about the Group Drive:
I did not create the Group Drive. It is shared with me
I am the owner of the folders in which the files and scripts reside.
I am the owner of all the files and scripts in these folders that I own too.
I have edit permissions at every level of this group drive.
there is nothing in your script above that actually does the "file copy". It just creating new empty file with SpreadsheetApp.create(targetFilename) and adding that to the folder.
To make a copy of the Google Sheet, you may refer to the answer at Google App Script: How do I make copy of spreadsheet and save it to particular folder?
I ended up moving that block of code to the end of the script after the data processing occurs. For some reason, within my own drive, it didn't matter when I added the file to another folder, the script would still update all instances of that file. However, within a group drive, only the file within my own root was being updated (till I moved that block of code to the end of the script).
Any explanations on why this occurs is welcome.

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.