问题
I have a Google Form that uses the File Upload feature that is mentioned on the below site, https://sites.google.com/site/scriptsexamples/home/announcements/google-forms-file-upload-feature
I am wondering 2 things:
- Is there a way to prevent the renaming of the uploaded file. (IE: original_file_name - Name of Uploader)
- Concerning the below quote:
If a respondent decides to upload a file directly from his Drive, a copy will be made and the form owner will become owner of the copy.
Is it possible to identify the original file and remove it so that it does not junk up the uploaders root Drive folder.
function getCurrentResponses() {
var form = FormApp.openById('formID');
var formResponses = form.getResponses();
var numResponses = formResponses.length;
var lastResponse = formResponses[numResponses - 1];
var lastResponseItem = lastResponse.getItemResponses();
var emailAddress = lastResponseItem[0].getResponse();
Logger.log('emailAddress: ' + emailAddress);
var zone = lastResponseItem[1].getResponse();
Logger.log('zone: ' + zone);
var projectName = lastResponseItem[2].getResponse();
Logger.log('projectName: ' + projectName);
var igeURL = lastResponseItem[3].getResponse();
Logger.log('igeURL: ' + igeURL);
//Creating folder for submitted project
var DriveFolder = DriveApp.getFolderById("destinationFolder");
var folderName = projectName;
//create the folder
var folderpath = DriveFolder.createFolder(folderName).addEditor(emailAddress).getId();
//get the path to the folder
var pathtoemail = "https://drive.google.com/drive/folders/"+folderpath;
var file = DriveApp.getFileById(igeURL);
file.getParents().next().removeFile(file);
DriveApp.getFolderById(folderpath).addFile(file);
var fileName = file.getName();
//Create the message and subject of the email
var message = 'Thank you for submitting. Here is the link to your drive folder. Please use this folder to upload and receive related documentation. ' +pathtoemail ; //a custom message, feel free to change anything inside the quotes
var subject = "Form Related Documentation." ;
//send the email
MailApp.sendEmail(emailAddress, subject, message, {
// cc: 'email@email.com' // optional cc
});
/*
* Convert Excel file to Sheets
* https://gist.github.com/azadisaryev/ab57e95096203edc2741
* Retrieved 06/08/2017
*/
var xlsId = igeURL; // ID of Excel file to convert
var xlsFile = DriveApp.getFileById(xlsId); // File instance of Excel file
var xlsBlob = xlsFile.getBlob(); // Blob source of Excel file for conversion
var xlsFilename = xlsFile.getName(); // File name to give to converted file; defaults to same as source file
var destFolders = folderpath; // array of IDs of Drive folders to put converted file in; empty array = root folder
Logger.log("destFolders: " + destFolders);
var ss = convertExcel2Sheets(xlsBlob, xlsFilename, destFolders);
var ssID = ss.getId();
Logger.log(ss.getId());
var convertFile = DriveApp.getFileById(ssID);
convertFile.getParents().next().removeFile(convertFile);
DriveApp.getFolderById(folderpath).addFile(convertFile);
}
/**
* Convert Excel file to Sheets
* https://gist.github.com/azadisaryev/ab57e95096203edc2741
* Retrieved 06/08/2017
* @param {Blob} excelFile The Excel file blob data; Required
* @param {String} filename File name on uploading drive; Required
* @param {Array} arrParents Array of folder ids to put converted file in; Optional, will default to Drive root folder
* @return {Spreadsheet} Converted Google Spreadsheet instance
**/
function convertExcel2Sheets(excelFile, filename, arrParents) {
var parents = arrParents || []; // check if optional arrParents argument was provided, default to empty array if not
if ( !parents.isArray ) parents = []; // make sure parents is an array, reset to empty array if not
Logger.log("arrParents: " + arrParents);
// Parameters for Drive API Simple Upload request (see https://developers.google.com/drive/web/manage-uploads#simple)
var uploadParams = {
method:'post',
contentType: 'application/vnd.ms-excel', // works for both .xls and .xlsx files
contentLength: excelFile.getBytes().length,
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
payload: excelFile.getBytes()
};
// Upload file to Drive root folder and convert to Sheets
var uploadResponse = UrlFetchApp.fetch('https://www.googleapis.com/upload/drive/v2/files/?uploadType=media&convert=true', uploadParams);
// Parse upload&convert response data (need this to be able to get id of converted sheet)
var fileDataResponse = JSON.parse(uploadResponse.getContentText());
// Create payload (body) data for updating converted file's name and parent folder(s)
var payloadData = {
title: filename,
parents: []
};
if ( parents.length ) { // Add provided parent folder(s) id(s) to payloadData, if any
for ( var i=0; i<parents.length; i++ ) {
try {
var folder = DriveApp.getFolderById(parents[i]); // check that this folder id exists in drive and user can write to it
payloadData.parents.push({id: parents[i]});
}
catch(e){} // fail silently if no such folder id exists in Drive
}
}
// Parameters for Drive API File Update request (see https://developers.google.com/drive/v2/reference/files/update)
var updateParams = {
method:'put',
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
contentType: 'application/json',
payload: JSON.stringify(payloadData)
};
// Update metadata (filename and parent folder(s)) of converted sheet
UrlFetchApp.fetch('https://www.googleapis.com/drive/v2/files/'+fileDataResponse.id, updateParams);
return SpreadsheetApp.openById(fileDataResponse.id);
}
回答1:
I just wanted to post that I was able to create a work-around for the renaming of the file.
var form = FormApp.openById('formID');
var formResponses = form.getResponses();
var numResponses = formResponses.length;
var lastResponse = formResponses[numResponses - 1];
var lastResponseItem = lastResponse.getItemResponses();
var submittedID = lastResponseItem[3].getResponse();
var file = DriveApp.getFileById(submittedID);
var oldFileName = file.getName();
var indexOldFileName = oldFileName.indexOf("-");
var newFileName = oldFileName.slice(0,indexOldFileName - 1);
var indexOldFileExt = oldFileName.lastIndexOf(".");
var newFileExt = oldFileName.slice(indexOldFileExt);
var modName = newFileName + newFileExt;
file.setName(modName);
As for my second question, I still haven't been able to solve it. I will post if I find a solution.
回答2:
I change your code a little bit so the name of the file is changed to its ID.
function changenametoid() {
var form = FormApp.openById('formID');
var formResponses = form.getResponses();
var numResponses = formResponses.length;
var lastResponse = formResponses[numResponses - 1];
var lastResponseItem = lastResponse.getItemResponses();
var submittedID = lastResponseItem[0].getResponse();
var file = DriveApp.getFileById(submittedID);
var oldFileName = file.getName();
var indexOldFileName = oldFileName.indexOf("-");
var newFileName = submittedID;
var indexOldFileExt = oldFileName.lastIndexOf(".");
var newFileExt = oldFileName.slice(indexOldFileExt);
var modName = newFileName + newFileExt;
file.setName(modName);
}
来源:https://stackoverflow.com/questions/44445931/prevent-renaming-of-google-forms-file-upload