google drive scripting folder location - google-apps-script

I have a GApp which scratch builds a new GDrive spreadsheet. I shows up in my root directory.
I would like to place the new SS in a specific folder instead.
I cannot seem to find how to accomplish this.

From the API Reference
// This example creates a new file and adds it to a folder
// Note: This can also be used on a folder to add it to another folder
var file = DocsList.createFile('my test file', 'test file contents');
var folder = DocsList.createFolder('My Test Folder');
file.addToFolder(folder);
Logger.log(file.getParents()[0].getName()); // logs 'My Test Folder'
Just a heads up, depending on how you create your spreadsheet you might have difficulties, DocumentApp.create() and file appear to be different object types. But no worries, file has its own create method (DocsList.createFile(name, contents)) which can be referenced here
Hope that helps.

Related

apps-script - Create new google doc in existing folder

I am creating a google doc from apps script as follows:
var newDoc = DocumentApp.create(docName);
I want this new document to be created in an existing folder in my drive. I tried the following way:
var dir = DriveApp.getFolderById("folder-id");
dir.addFile(newDoc);
But I get the error as:
ReferenceError: "DocsList" is not defined.
Is there any way to create a new document in my existing folder or move my file to an existing folder via apps script? Any help will be appreciated.
Folder.addFile() requires that you pass it a File, but DocumentApp.create() returns a Document. What you need to do is use newDoc.getId() to get its unique identifier, and then use it in DriveApp.getFileById() to properly move the file.
var newDoc = DocumentApp.create(docName); // Create a Document
var docFile = DriveApp.getFileById(newDoc.getId()); // Get Document as File
var dir = DriveApp.getFolderById("folder-id"); // Get the folder
dir.addFile(docFile); // Add the file to the folder
DriveApp.getRootFolder().removeFile(docFile); // Optionally remove the file from root
Please also note that Google Drive files can exist in multiple folders. So if you only want the file to be listed in the "folder-id" folder, then you'll need to remove it from the root folder where it was created by default.

Google script Create new sheet file and move it to a specific folder Then copy the contents and 2 tabs from the active sheet to the newly created file

Welcome: I will not go into the code at the beginning, but I would like to ask a few questions. To learn the logic of operation.
Question 1:
When I create a new file using the function
var newFile = SpreadsheetApp.create("Name new sheets")
var newFileId = newFile.getId()
A new file is always saved to the root directory of the google disk?
Question 2
If I always write root directory of the google disk.The only way to move a newly created file is to use
var TARGE_FOLDER_ID = "asdFHd4hasdasn6nJMGLSQt8das331"
DriveApp.getFileById(newFileId).makeCopy("Name new sheets",TARGE_FOLDER_ID);
And after copying the removal from the main google drive folder?
When I create a new file using the function
Yes SpreadsheetApp.create("Name new sheets") puts files in the root directory you will have to move them
If I always write root directory of the google disk.The only way to move a newly created file is to use
Yes something like this Move file
var folder=DriveApp.getFoldersByName(folderName).next();//gets first folder with the given foldername
var file=SpreadsheetApp.create(fileName);
var copyFile=DriveApp.getFileById(file.getId());
folder.addFile(copyFile);
DriveApp.getRootFolder().removeFile(copyFile);

Acquiring the Google Drive folder my google script is located

With the help of a nice article (https://www.labnol.org/internet/google-drive-tree/21198/), I just setup my first google script in Google Drive.
Quick Question:
How may I get a running script /myFolder1/music/myFirstScript.gs, to determine it's running in /myFolder1/music?
This didn't work. I got the Url but nothing in the log file showed the correct answer.
var files = DriveApp.searchFiles('title contains "GoogleTreeAgenda5"');
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
Logger.log(file.getUrl());
Logger.log(file.getDownloadUrl());
Logger.log(file.getDescription());
Logger.log(file.getOwner());
Logger.log(file.getParents());
Logger.log(file.getParents()[0].getName());
Logger.log("------------------------------");
}
Longer Behind The Scenes Reason:
The file will be modified a bit so that each month I will create a new tree structure off the root with agenda topics and the script will create an html file from the directory tree which is based on topic. Basically it will be an outline creator based on the current location of the script file. So Let's say this month it's in: /myFolder1/music (so in other words it's /myFolder1/music/myFirstScript.gs. The script needs to determine that folder is /myFolder1/music so that I can have it print the tree structure starting from the folder it's located in to an html file..
In the example application provided by the site I noted above, there are 2 options to print the tree from the google script:
1) Tree starting from the root folder
var parentFolder = DriveApp.getRootFolder();
2) Starting from a particular folder (but I can't figure out and will need "Folder_Name" to be determined dynamically from the location where the .gs file is located)
var parent = DriveApp.getFoldersByName("FOLDER_NAME").next();
I've used getScriptId() this will:
Gets the script project's unique id.
This id is also the unique id in the Google Drive. Then from there, create a recurring getParents() until you reach the root folder.
Here is the complete code:
function myFunction() {
var scriptID = ScriptApp.getScriptId();
// Logger.log(scriptID)
// Log the name of every parent folder
var driveFile = DriveApp.getFileById(scriptID);
var parentFolder = driveFile.getParents();
while (parentFolder.hasNext()) {
var folder = parentFolder.next();
Logger.log(folder.getName());
}
}
NOTE:
There is no real or concrete path since a file can have multiple parents. It is also stated here in this related SO post.
Hope this helps.

Can copy file, can't add it Google script

In a Google script, I have a folder object (testFolder) and a file object (testFile). I have these two lines of code:
testFolder.addFile(testFile);
testFile.makeCopy('this is a copy', testFolder);
The second line correctly copies the file into the folder.
The first line seems to do nothing. I'm expecting it to add a reference to the file and place it in the folder.
I obviously have the correct objects and I am the owner of the file and the folder, so any other ideas?
Thanks
You can make a copy create new file in Google Drive. But one cannot simply move a file into a folder. You have to make a copy into the desired folder and then remove the old file. Or you have to directly create the file in the desired folder location.
What is your code for getting the 'testFile' and 'testFolder' objects? Are you referencing them by ID or by name? If you have multiple folders of the same name in your Google Drive, this could be causing the issue. Here's the script that worked for me
var file = DriveApp.getFileById('YOUR_FILE_ID');
var folder1 = DriveApp.getFolderById("YOUR_FOLDER1_ID");
var folder2 = DriveApp.getFolderById("YOUR_FOLDER2_ID");
file.makeCopy('another copy', folder1);
folder1.addFile(file);

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.