0

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>
Rubén
  • 34,714
  • 9
  • 70
  • 166
Giovanni
  • 11
  • 3
  • [Here's an example of an app that uploads a file](https://stackoverflow.com/a/63272945/7215091) – Cooper Aug 13 '20 at 22:21
  • 1
    What do you mean by "it's not working"? Is it throwing an error? – Rubén Aug 13 '20 at 22:24
  • Not working means that it just redirects me on another page with this URL: [link](https://n-qkypebf4ybgbg4fmmv43xgwl44dulp5aw7o4yua-0lu-script.googleusercontent.com/userCodeAppPanel?classi=classe3&myFirstName=mario&myLastName=rossi&myFile=Bulgaria+%28Christo+Anestev%29.jpg) – Giovanni Aug 14 '20 at 08:02
  • Does the file get uploaded and it's just a redirect issue, or does none of it work? Where did you get this code from? – Rafa Guillermo Aug 14 '20 at 10:42
  • Does this answer your question? [Blank PDF files when using Google API](https://stackoverflow.com/questions/60827372/blank-pdf-files-when-using-google-api) – Rafa Guillermo Aug 14 '20 at 10:54
  • @RafaGuillermo it is NOT just a redirect issue, the files are NOT uploaded. I got the code from an answer here on stack overflow. I edited the code to try to achieve what I wanted but this is not working. Anyway, also the original code is not working for me, so I don't know if the problem is mine, or if the code is really incorrect. I will try to use FileReader and let you know if the problem is solved. – Giovanni Aug 14 '20 at 12:29

0 Answers0