first of all I want to say I know almost nothing about javascript. I'm trying to develop a form with google app script where you can upload a file and it will automatically put it in a specific folder based on the response you enter in the form. For example, if I'm asking in the form which class you want to choose, and the user choose class "1", then the file will be uploaded in the "class 1" folder. With the help of the answers in this forum, I was able to create something that should work, but for some reason it is not. With my limited knowledge I'm not able to find the issue, even if this is a very easy fix, because as I said I don't know well Javascript. Some of the words in the code are in Italian, hope this is not a problem. This is my code, but for some reasons it is not working:
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function uploadFiles(form) {
try {
if(form.classi == "classe1") {
var dropbox = "Classi prime";
}
else if(form.classi == "classe2") {
var dropbox = "Classi seconde";
}
else if(form.classi == "classe3") {
var dropbox = "Classi terze";
}
else if(form.classi == "classe4") {
var dropbox = "Classi quarte";
}
else if(form.classi == "classe5") {
var dropbox = "Classi quinte";
}
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var blob = form.myFile;
var fileBlob = form.myFile;
var fname = fileBlob.getName();
var newName = form.myLastName+form.myFirstName; // simple example
// extract extension using RegEx
// from http://stackoverflow.com/a/680982/1677912
var extensionfinder = /(?:\.([^.]+))?$/;
var ext = extensionfinder(fname)[1];
fileBlob.setName(newName+'.'+ext);
var file = folder.createFile(blob);
file.setDescription("Caricato da " + form.myFirstName+form.myLastName);
return "<p> Il tuo file è stato caricato correttamente, " + form.myFirstName + "!";
}
catch (error) {
return error.toString();
}
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<title>Caricamento file Liceo Augusto</title>
<style>
input[type=text], select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 16px;
resize: vertical
}
input[type=submit] {
background-color: #c72222;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #6e1010;
}
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
margin: auto;
width: 70%;
border: 3px solid black;
}
</style>
</head>
<body>
<h1><center>Compila il modulo per inviare il tuo documento</center></h1>
<div class="container">
<form id="uploadFilesForm">
<label for="classe">Seleziona la classe:</label>
<select id="classi" name="classi">
<option value="classe1">1</option>
<option value="classe2">2</option>
<option value="classe3">3</option>
<option value="classe4">4</option>
<option value="classe5">5</option>
</select>
<label for="name">Nome:</label>
<input type="text" id="fname" name="myFirstName" placeholder="Il tuo nome...">
<label for="lname">Cognome:</label>
<input type="text" id="lname" name="myLastName" placeholder="Il tuo cognome...">
<input type="file" name="myFile">
<br><br>
<center><input type="submit" value="Invia documento"
onclick="this.value='Caricamento...';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;"
>
</center>
</form>
</div>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
</body>
</html>