问题描述
我有一个Google表单,该表单使用以下网站上提到的文件上传功能, https://sites.google.com/site/scriptsexamples/home/announcements/google-forms-file-upload-feature
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
我想知道两件事:
- 有没有一种方法可以防止重命名上载的文件. (即:original_file_name-上载程序的名称)
- 关于以下引用:
- Is there a way to prevent the renaming of the uploaded file. (IE: original_file_name - Name of Uploader)
- Concerning the below quote:
是否可以标识原始文件并将其删除,以免浪费上传者的根驱动器文件夹.
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 protected]' // 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);
}
推荐答案
我只是想发帖说,我能够为文件重命名创建解决方法.
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.
这篇关于防止重命名Google Forms文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!