1

I have a Google Drive with a folder called "Submissions". I also have a Google Apps script which both creates a 'form' as well as depositing what the end-user has uploaded (from the form) into the submissions folder.

Here is an example of the script (server.gs):

 function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');
}

function uploadFiles(form) {

  try {

    var dropbox = "Submissions";
    var folder, folders = DriveApp.getFoldersByName(dropbox);

    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(submissions);
    }

    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Uploaded by " + form.myName);

    return "File uploaded successfully " + file.getUrl();

  } catch (error) {

    return error.toString();
  }

}

And the Form.html is as follows:

<form id="myForm">

<label>Name:</label>
<input type="text" name="myName">

<label>Email:</label>
<input type="text" name="myEmail">

<label>Demo Title (e.g "Artist - Demo Name"):</label>
<input type="text" name="myTitle">

<label>Choose Demo File:</label>
<input type="file" name="myFile">

<input type="submit" value="Submit" 
           onclick="google.script.run
                    .uploadFiles(this.parentNode);
                    return false;">

What this does is create a form which has "Name", "Email", "Demo Title" text fields as well as a "Submit" button. The user attaches a file and uploads the file using this form.

Now the file upload works absolutely fine. Anyone can upload a file and it will appear in a folder (Submissions) on my google drive correctly... However, the other details simply vanish as if they were never entered. I want THOSE details to appear in an existing spread sheet with preferably a timestamp.

Can anyone help me?

TJV
  • 11
  • 2

1 Answers1

0

You will have to use Class SpreadsheetApp, with this class you will be able to use SpreadsheetApp.getActiveSpreadsheet(), getSheetByName(), getRange(), getLastRow(), and getLastColumn() and more.

Here is a sample code from this tutorial:

var doc = SpreadsheetApp.openById(ID);
var sheet = doc.getSheetByName(SHEET_NAME);
// we'll assume header is in row 1 but you can override with header_row in GET/POST data
var headRow = e.parameter.header_row || 1;
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1; // get next row
var row = [];
// loop through the header columns
for (i in headers){
if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
row.push(new Date());
} else { // else use header name to get data
row.push(e.parameter[headers[i]]);
}
}
// more efficient to set values as [][] array than individually
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
// return json success results
return ContentService
.createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
.setMimeType(ContentService.MimeType.JSON);
} catch(e){
// if error return this
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
} finally { //release lock
lock.releaseLock();
}

EDIT:

For example this is your code

 <form id="myForm">

<label>Name:</label>
<input type="text" name="myName">

<label>Email:</label>
<input type="text" name="myEmail">

<label>Demo Title (e.g "Artist - Demo Name"):</label>
<input type="text" name="myTitle">

<label>Choose Demo File:</label>
<input type="file" name="myFile">

<input type="submit" value="Submit" 
           onclick="google.script.run
                    .uploadFiles(this.parentNode);
                    return false;">

(server.gs)

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');
}

function uploadFiles(form) {

  try {

    var dropbox = "Submissions";
    var folder, folders = DriveApp.getFoldersByName(dropbox);

    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(submissions);
    }

    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Uploaded by " + form.myName);

    return "File uploaded successfully " + file.getUrl();

    //pass data to spreadsheet
    processForm(YOUR_VARIABLES)

  } catch (error) {

    return error.toString();
  }

}
function processForm(YOUR_VARIABLES) {

  var Name = name;
  var email = email;


  var ss = SpreadsheetApp.openById(SPEADSHEET_ID); 
  var sheet = ss.getSheetByName(SHEET_NAME);

  //Change this depending on the order and number of inputs to be recorded
  sheet.getRange(sheet.getLastRow()+1, 1, 1, 5).setValues([[Name,email]]);  
}

You can also check this answers to get some ideas on how to implement the spreadsheet app : Get form input text value to insert in a Google Spreadsheet , Uploading file using Google Apps Script using HtmlService

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
  • Thanks for the reply. I should have mentioned that my coding skills are essentially beginner level so what I do with your answer is a tad confusing. Do I put this new code in the 'server.gs' script or the 'form.html' part? It looks as though I need to change your code but exactly how is beyond me :( – TJV May 24 '16 at 08:54
  • No need to change the whole code, just add to integrate`SpreadSheetApp()` Class. Just to follow up you want to save data to the Spreadsheet instead of saving it directly in drive right? – Mr.Rebot May 24 '16 at 08:58
  • I want the upload part of the form to do exactly as it is doing (Audio file goes into folder), however I want the other details entered (name, email etc) to go into an existing spreadsheet, with a timestamp for each line added (per submission). I assume that isn't as easy as adding "SpreadSheetApp()" to my code – TJV May 24 '16 at 10:18
  • So when the end-user fills out the form they have the 6 entry fields: Name, Email, Demo Title, 'Upload Field', Notes, 'Submit button'. When they fill all information out, only the 'upload field' works. To describe this further, all other information (name, email, demo title and notes), disappear as they have nowhere to go. I need to change my existing code as per above to enable the other info to go into an existing google sheet. – TJV May 27 '16 at 00:01
  • Change to add features to your code, right? I edited my answer, I hope that helps and clear things up for you – Mr.Rebot May 27 '16 at 00:48