Reputation: 772
Google recently added a file upload feature to their forms.
However, I am unable to find any documentation on how to use this in google scripts. If I look at the item types API on google, it isn't listed
https://developers.google.com/apps-script/reference/forms/item-type
However, if I check the types in my form, I get a type of "FILE_UPLOAD"
responseItem.getItem().getType() //returns FILE_UPLOAD
If I look at the object, it is an array with a string.
I am just trying to figure out how to get the filename and rename it.
Upvotes: 3
Views: 2426
Reputation: 21
/* Explaining a litle more
*
* although in documentation there's no FormApp.ItemType.FILE_UPLOAD
*
* using debugger or Logger.log you can go through items in form
* and get which text and ord number are used.
* for example :
*/
var items = FormApp.getActiveForm().getItems();
items.forEach(function(item){
var name = item.getType().toString();
var number = item.getType().ordinal();
var title = item.getTitle();
Logger.log("Item ["+title+"] Type="+name+" Ord Num="+number);
});
// Item for File Upload has:
// Type = 'FILE_UPLOAD' and
// Ord Num = 16,
// exactly one more than VIDEO which are 15. * since enum begin with 0
Upvotes: 1
Reputation: 772
So after thinking about this a bit I realized that the string that is returned by the FILE_UPLOAD type is probably the google drive File id. Turns out that is correct. So you can use the DriveApp class to get the file and rename it.
Here is how I did it:
for (var f = 0; f < responseItems.length; f++) {
if (responseItems[f].getItem().getType() === "FILE_UPLOAD") {
files = responseItems[f].getResponse();
if (files.length > 0) {
for (var n in files) {
var dFile = DriveApp.getFileById(files[n]);
dFile.setName("new name_" + n);
}
}
}
}
When you run this you will be asked to give permission to the script to access files in drive.
Hope this helps someone else trying to figure this out.
Upvotes: 6