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.
Related
Using prior articles and questions found within stack overflow I was able to find a snippet of App Script that searches Gmail labels for attachments and moves them to a specific folder in Google Drive.
function saveAttachmentInFolder(){
var folder = DriveApp.getFolderById('xxosi2');
var userId = "please.thanks#gmail.com";
var query = "label:thankyoucards-reports";
var res = Gmail.Users.Messages.list(userId, {q: query});//I assumed that this works
res.messages.forEach(function(m){
var attA=GmailApp.getMessageById(m.id).getAttachments();
attA.forEach(function(a){
folder.createFile(a.copyBlob()).setName(a.getName());
});
});
}
I need to modify this code to perform the following additional functions:
If file exists, overwrite and retain version history
I have also played around with the answer found in the following thread to no avail as I believe this is hard coded in some way and too specific to the one file type (xlsx) Copying attachments from Gmail to Google Drive folder and overwriting old files with Apps Script.
I believe your goal is as follows.
You want to check this using the filename between the existing file in the folder and the attachment file.
You want to overwrite the existing file with the attachment file.
In this case, how about the following modification? In this case, Drive API is used. So, please enable Drive API at Advanced Google services.
From:
folder.createFile(a.copyBlob()).setName(a.getName());
To:
var filename = a.getName();
var files = folder.getFilesByName(filename);
if (files.hasNext()) {
Drive.Files.update({}, files.next().getId(), a.copyBlob(), {supportsAllDrives: true});
} else {
folder.createFile(a.copyBlob()).setName(filename);
}
When this modified script is run, the existing file is searched from the folder using the filename of the attachment file. When the file is found, the file is overwritten by the attachment file. When the file is not found, the file is created as a new file.
Note:
In this modified script, the existing file is overwritten. So, please be careful about this. I would like to recommend using a sample file for testing the script.
Reference:
Files: update of Drive API v2
The app script I'm running to move files with a certain name in the title to a new folder is copying the same files every time the script is run. Removing the files from the source folder doesn't seem to work.
Form responses are collected in a spreadsheet and an add-on is used to put the responses into a form that generates a new Google doc. The Google Docs is for all of our various buildings, which each have a unique name. The script should iterate through the file names and add the file to the specific buildings folder and then remove it from the folder that has every building altogether. When I run the script, it works, but on subsequent runs, all of the files that were copied before keep getting copied again with any new files in addition to it.
function movePittsburghFiles() {
var sourceFolder=DriveApp.getFolderById('taqwdGs4MUEyUEl1WQ8');
var destFolder = DriveApp.getFolderById('taqwV085WTVtdTdf');
var files = DriveApp.searchFiles('title contains "3N - Pittsburgh Weekly"');
while (files.hasNext()) {
var file = files.next();
destFolder.addFile(file);
sourceFolder.removeFile(file);
}
The script works, but it keeps finding the same files in the source folder that were already moved in previous runs of the code and copying them again. When I go to the folder in Google Drive, those files are not listed, but somehow the script keeps finding them.
Issue:
You're searching the whole drive, When you're calling .searchFiles() on DriveApp.
Solution:
Call .searchFiles() on the sourceFolder instead.
Snippet:
var files = sourceFolder.searchFiles('title contains "3N - Pittsburgh Weekly"');
References:
Folder#SearchFiles
I am having trouble adding files to the shared google drive folder as well as not being able to remove specific types of files of any kind within my script. The shared google drive file gives me issues. The code below works for me on my own personal google drive folder for adding and removing CSV's from one google drive folder to any another that I specify. However, it does not work with our general shared google drive folder. I have been authorized permission via the google cloud console API for Drive & Sheets but I am still having permission issues. Any help or clarification on this issue would be greatly appreciated.
Here are two different pieces of code. The first one with function moveFiles() works on my personal drive but not in the shared folders. Here is also some more code that I was playing around with to test the shared folders in a simpler manner. I was able to get the putFile() function to put a newDoc inside a shared google drive folder but not able to remove it.
function moveFiles(source_folder, dest_folder)
{
// set current destination to grab the folder from
var currentFolder=DriveApp.getFolderById("1emSsRay_WI_z_qBUpQIoccxQID28FvB0");
// grab only the csv's within current folder
var docs = DriveApp.getFilesByType(MimeType.CSV);
// set target destination where we will store old csv's that have been processed & Analyzed
var destination = DriveApp.getFolderById("1wYG1Gd5z0-nucedSMOBn8ZJs68ZgR8Hb");
// iterate through the csv's files within the currentFolder, add them to the destination and remove them from the current folder
while (docs.hasNext())
{
var doc = docs.next();
destination.addFile(doc); // get error "Cannot use this operation on a shared drive item(line 13, file "SharedDriveMove")
currentFolder.removeFile(doc);
}
}
function putFile()
{
var newDoc = DocumentApp.create('Testing Team Drive MoveTo').getId();
var file = DriveApp.getFileById(newDoc);
var moveFile = DriveApp.getFolderById('1emSsRay_WI_z_qBUpQIoccxQID28FvB0').addFile(file);
}
function takeFile()
{
var filesIterator = DriveApp.getFilesByName('Testing Team Drive MoveTo');
while (filesIterator.hasNext())
{
var file = filesIterator.next();
}
var cleanup = DriveApp.getFolderById('1wYG1Gd5z0-nucedSMOBn8ZJs68ZgR8Hb').addFile(file,{"supportsAllDrives": true}); // get error "Cannot find method addFile(File,Object).(line 15,file"Code")
var moveFile = DriveApp.getFolderById('1emSsRay_WI_z_qBUpQIoccxQID28FvB0').removeFile(file,{"supportsAllDrives": true});
}
DriveApp is an Apps Script class whereby it has it's limitations among Drive. Also, as the documentation says:
This method does not delete the file, but if a file is removed from
all of its parents, it cannot be seen in Drive except by searching for
it or using the "All items" view.
You should do it with the Drive API instead:
Use "supportsAllDrives", otherwise it won't find the file if it's in a Shared Drive (until this is deprecated in 2020).
Drive.Files.remove("your file ID", {"supportsAllDrives": true});
You also have to authorize the Drive.File scope.
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)
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.