Can copy file, can't add it Google script - google-apps-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);

Related

Applescript help: Move files to folders according to a CSV file

I am looking for an AppleScript that moves files in a single directory into new folders based on info provided in a CSV file, where the columns are A) Filename with extension B) New folder title. Thank you.

Google App script: Overwrite existing csv file. Not create a copy using createFile

Hi I've got this Google App script that needs to overwrite a file.
At the moment it creates a copy.
Is there a line or two that can check if it exists,then delete?
Or is there an alternative to createFile that overwrites?
DriveApp.getFolderById(fold).createFile(file.getName()+'-'+sheet+'.csv', csv);
Many thanks for looking!
Filenames are not unique on Google Drive - the uniqueness of files is determined by their ID. Whereas on a regular file system, creating a file with a similar filename would erase the old file, in this case you are creating a new unique file every time.
As far as I know, the easiest way would the be to move the existing file to the trash. You can keep you existing script but add to it. Assuming your file will always exist, this should work:
const folder = DriveApp.getFolderById(fold);
folder.getFilesByName(file.getName()+'-'+sheet+'.csv').next().setTrashed(true);
folder.createFile(file.getName()+'-'+sheet+'.csv', csv);
If you are unsure that a file with that name will exist, or if there might be multiple files with that name, you will have to iterate through all them:
const folder = DriveApp.getFolderById(fold);
const files = folder.getFilesByName(file.getName()+'-'+sheet+'.csv');
while(files.hasNext()){
let f = files.next();
f.setTrashed(true);
}
folder.createFile(file.getName()+'-'+sheet+'.csv', csv);
I haven't tested this code, but it should be pretty close to what you need.
Edit
Contrary to what I said, the method DriveApp.File.setContent(content) can overwrite the content of a file. The above solution still works and avoids potential data loss.

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.

google drive scripting folder location

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.

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