I am trying to transfer ownership of all my .pdf files to another account with more space. I am testing the code with a single folder in my drive.
function transfer() {
var user = Session.getActiveUser();
var folder = DriveApp.getFolderById('123folder-id456789-VxdZjULVQkPAaJ');
var files = folder.getFilesByType(MimeType.PDF);
while (files.hasNext()) {
var file = files.next();
if (file.getOwner() == user) file.setOwner('example#gmail.com');
}
}
When I run the code, none of the files change ownership.
How about this modification?
Modification points:
In your script, it tries to compare the objects of Session.getActiveUser() and file.getOwner(). I think that this is the reason of your issue.
So how about this modification? Please think of this as just one of several answers.
Modified script:
function transfer() {
var user = Session.getActiveUser().getEmail(); // Modified
var folder = DriveApp.getFolderById('123folder-id456789-VxdZjULVQkPAaJ');
var files = folder.getFilesByType(MimeType.PDF);
while (files.hasNext()) {
var file = files.next();
if (file.getOwner().getEmail() == user) file.setOwner('example#gmail.com'); // Modified
}
}
In this modification, the emails are compared.
References:
getActiveUser()
getOwner()
Class User
If this didn't resolve your issue, I apologize.
Currently drive can't transefere the ownership of file that are not build-in, like pdf, zip, etc. So you must download them and reupload from the other account. I wrote a colab to do that without consume my bandwith. It can recursively transfere an entire folder with both build-in file types and other file types.
Related
I found this script that will upload files from google forms response to a folder using the first response (name of applicant) to name the folder.
function onFormSubmit(e) {
const folderId = "###"; // ID of the destination folder
const form = FormApp.getActiveForm();
const formResponses = form.getResponses();
const itemResponses = formResponses[formResponses.length - 1].getItemResponses();
// Prepare the folder.
const destFolder = DriveApp.getFolderById(folderId);
const folderName = itemResponses[0].getResponse();
Logger.log(folderName)
const subFolder = destFolder.getFoldersByName(folderName);
const folder = subFolder.hasNext() ? subFolder.next() : destFolder.createFolder(folderName);
// Move files to the folder.
itemResponses[1].getResponse().forEach(id => DriveApp.getFileById(id).moveTo(folder));
}
I tried using it but it will only upload one file - the one correspnding to the specific question number in the line itemResponses[1].getResponse()
How Do i modify this script so it will upload all of the files in the response regardless of their position without creating multiple folders?
i.e. is it possible to set a range of questions that all the files will upload?
Also, i want to modify the script so that name of the folder will also consist of the response sequence number as a prefix to the name of responder, how is that possible?
I'm writing a script to produce some artefacts from my build so I want to clean up unwanted files first. I'm using CleanDirectory(dirPath, predicate).
I'm finding it disturbingly hard to work out the directory for a file. If I use GetDirectoryName() that seems to just get me the immediate parent, not the full directory path.
Func<IFileSystemInfo, bool> predicate =
fileSystemInfo => {
// Dont filter out any directories
if (fileSystemInfo is IDirectory)
return false;
var path = fileSystemInfo.Path.FullPath;
var directory = ((DirectoryPath)path).GetDirectoryName();
...
}
Obviously I can use the .NET Framework System.IO classes to do this easily but then I get strings with the slashes in the wrong direction, and things do not smoothly inter-operate with Cake which uses POSIX paths.
OK I've worked out a solution. The key to IFileSystemInfo is to try and cast the Path to various derived types/interfaces, which then provide the functionality you are probably looking for. Example:
Func<IFileSystemInfo, bool> predicate =
fileSystemInfo => {
// Dont filter out any directories
if (fileSystemInfo is IDirectory)
return false;
// We can try and cast Path as an FilePath as know it's not a directory
var file = (FilePath) fileSystemInfo.Path;
if (file.FullPath.EndsWith("Help.xml", StringComparison.OrdinalIgnoreCase))
return false;
// GetDirectory() returns a Path of type DirectoryPath
var directory = file.GetDirectory().FullPath;
...
}
I'm attempting to save a screenshot using a generic method in protractor. Two features, it creates the folder if it does not exist and it saves the file (with certain conditions).
export function WriteScreenShot(data: string, filename: string) {
let datetime = moment().format('YYYYMMDD-hhmmss');
filename = `../../../test-reports/${filename}.${datetime}.png`;
let path =filename.substring(0, filename.lastIndexOf('/'));
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
let stream = fs.createWriteStream(filename);
stream.write(new Buffer(data, 'base64'));
stream.end();
}
This can be used by calling browser.takeScreenshot().then(png => WriteScreenShot(png, 'login/login-page')); Using this example call, a file will be created, I assumed, in the path relative where my WriteScreenShot method's file resides. But that does not appear to be the case.
For example, when I run my spec test in the spec's folder, the image gets saved in the correct place. But if I run it at the project root, an error is capture. Obviously, this has to do with my relative path reference. How do I capture the project's root directory and build from that so that I can run the test from any directory?
This is a classical directory access error. Let me just explain what is happening to your code -
let path =filename.substring(0, filename.lastIndexOf('/'));
The above line outputs to ../../../test-reports
fs.existsSync checks whether thispath exists or not -
case 1 :(postive flow) Your spec folder is in the same current working directory in which you are trying to create reports folder. When you run your test, the path exists, it generates the test-reports directory & screenshots and your code works fine.
case 2:(negative flow) When you try to run it from the root directory which is the current working directory now, fs.existsSync tries to check the path & the reports folder inside it. If it doesn't exist , fs.mkdirSync tries to create your directories but it would fail as it cannot create multiple directories.
You should be using native path module of nodejs to extract the path instead of using file substring and the mkdirp external module for creating multiple directories.
import * as path from 'path';
let {mkdirp} = require('mkdirp'); // npm i -D mkdirp
export function WriteScreenShot(data: string, filename: string) {
let datetime = moment().format('YYYYMMDD-hhmmss');
filename = `../../../test-reports/${filename}.${datetime}.png`;
let filePath = path.dirname(filename); // output: '../../..' (relative path)
// or
let filePath = path.resolve(__dirname); // output: 'your_root_dir_path' (absolute path)
// or
let filePath = path.resolve('.'); // output: 'your_root_dir_path' (absolute path)
if (!fs.existsSync(filePath )) {
mkdirp.sync(filePath); // creates multiple folders if they don't exist
}
let stream = fs.createWriteStream(filename);
stream.write(new Buffer(data, 'base64'));
stream.end();
}
If you are curious to know the difference btw mkdir & mkdir-p please read this SO thread.
I have a google spreadsheet which contains multiple sheets (or tabs) within it. Each sheet is populated from its own unique form. None of the forms are embedded in the spreadsheet.
Periodically, I need to delete all the data in the sheets, and also delete all the old responses which are saved in each of the forms. I can do this using a .gs script which resides in the spreadsheet. It accesses the form by its ID (the long string which appears in its URI). This requires the ID string to be hardcoded in my .gs script.
Ideally, I would like to access each form from the sheet object (i.e. the destination for each forms entries). Mock up code would look like this...
var ss = SpreadSheedApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var form = sheet.getMyAssociatedSourceForm(); // my dream method :-)
form.deleteAllResponses() // this method already exists
Does anyone know if this is possible? Or will I have to continue to use the ID (which is currently working)?
rgds...
I think you can do this without literally typing in ID's into your script. But, you would need to get every Form in your drive, loop through them all and get the destinationId() of every Form.
Google Documentation
Then compare the destinationId with the current spreadsheets ID, which you can get without needing to "hard code" it:
function deleteAllResponses() {
var thisSS_ID = SpreadsheetApp.getActiveSpreadsheet().getId();
var allForms = DriveApp.getFilesByType(MimeType.GOOGLE_FORMS);
var thisFormFile, thisFormFileID = "", thisForm, theDestID = "";
while (allForms.hasNext()) {
thisFormFile = allForms.next();
thisFormFileID = thisFormFile.getId();
thisForm = FormApp.openById(thisFormFileID);
try {
theDestID = thisForm.getDestinationId();
} catch(err) {
continue;
};
if (theDestID === "" || theDestID === undefined) {
continue;
};
if (theDestID === thisFormFileID) {
thisForm.deleteAllResponses();
};
};
};
I have not tested this, so don't know if it works. If it does, let me know in the comments section.
I'm using a <S:nativeDragDrop> and getting files dragged over a component like so:
var arr:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
I'm not sure how to restrict what type of files can be dragged. Is there a native control for this? The help documents mention the possibility of defining completely different ClipboardFormats, but I have no idea how to do that; I could run regex on the filenames as well, but that seems overcomplicated.
Wondering if there's a way like with FileReference.browse to specify specific file extensions
As far as I know, there is not a built-in way to filter dropped files. However, in your NATIVE_DRAG_ENTER handler, you could loop through the list of files and choose not accept the drag based on their file types. Or, you could merely ignore the unsupported types when you are processing the NATIVE_DRAG_DROP.
var validTypes:Object = {png : true, jpg : true, gif : true};
function nativeDragEnter(event:NativeDragEvent):void {
var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array
for each(var file:File in files) {
if(!validTypes[file.extension.toLowerCase()]) // Don't accept drag if any of the dropped files aren't supported.
return;
}
DragManager.acceptDrag(InteractiveObject(event.target));
}
function nativeDragDrop(event:NativeDragEvent):void {
var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array
for each(var file:File in files) {
if(validTypes[file.extension]) //accept only certain files
processFile(file);
}
}
As a side note, I assumed you are working on an AIR app here, but if you aren't, you'll have to use the FileReference class instead of File.