FILE_UPLOAD item type in google forms

喜你入骨 提交于 2020-02-24 05:27:21

问题


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.


回答1:


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.




回答2:


/* 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


来源:https://stackoverflow.com/questions/45891218/file-upload-item-type-in-google-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!