1

Let's say I have a Google Docs Form that gathers the following info:

  • Timestamp (default field)
  • Names
  • Ref#

The form data then appears on the spreadsheet as follows:

4/10/2013 16:20:31  |  Jack, Jill, Oscar  |  Ref6656X

(Note: the number of names may be anywhere from 1 to many)

I need the data to appear on the spreadsheet as follows:

4/10/2013 16:20:31  |  Jack  |  Ref6656X
4/10/2013 16:20:31  |  Jill  |  Ref6656X
4/10/2013 16:20:31  |  Oscar |  Ref6656X

I can often decipher and edit Google Apps Script (JavaScript?), but I don't know how to think in that language in order to create it for myself (especially with an unknown number of names in the Name field). How can I get started on solving this?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
5th4x4
  • 1,497
  • 8
  • 21
  • 27

1 Answers1

1

First of all, you've got some choices to make before you start writing your code.

  • Do you want to modify the spreadsheet that's accepting form input, or produce a separate sheet that has the modified data? If you want to have a record of what was actually input by a user, you'd best leave the original data alone. If you're using a second sheet for the massaged output, the presence of multiple tabs might be confusing to your users, unless you take steps to hide it.

  • Do you want to do the modifications as forms come in, or (in bulk) at some point afterwards? If you already have collected data, you'll have to have the bulk processing, and that will involve looping and having to handle insertions of new rows in the middle of things. To handle forms as they come in, you'll need to set up a function that is triggered by form submissions, and only extend the table further down... but you've got more learning to do - see Container-Specific Triggers, Understanding Triggers and Understanding Events for background info.

  • Will you primarily use Spreadsheet service functions, or javascript Arrays? This choice is often about speed - the more you can do in javascript, the faster your script will be, but switching between the two can be confusing at first.

Here's an example function to do bulk processing. It reads all existing data into an array, goes through that and copies all rows into a new array, expanding multiple names into multiple rows. When done, the existing sheet data is overwritten. (Note - not debugged or tested.)

function bulkProcess() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dataIn = ss.getDataRange().getValues();
  var dataOut = [];

  for (var row in dataIn) {   // Could use:  for (var row = 0; row < dataIn.length; row++)
    var names = dataIn[row][1].split(','); // array of names in second column
    var rowOut = dataIn[row];
    for (var i in names) {
      rowOut[1] = names[i];  // overwrite with single name
      dataOut.push(rowOut);  // then copy to dataOut array
    }
  }
  // Write the updated array back to spreadsheet, overwriting existing values.
  ss.getRange(1,1,dataOut.length,dataOut[0].length).setValues(dataOut);
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • I had resigned myself to the realization that this question would simply languish here, so this is a pleasant surprise! As for choice #1, I was planning on capturing all of the form data onto a mirror spreadsheet since I don't want to manipulate the live actual data. As for the existence/appearance of multiple tabs, that will be a non-issue in this particular case. (more to follow) – 5th4x4 Apr 16 '13 at 19:22
  • As for choice #2, my original plan was to process the newest data as soon as it arrived (via On Form Submit). However I am unsure of how that trigger will behave if 2 or more submissions occur simultaneously (or near-simultaneously). Therefore I was going to test to see how long it would take to process all form submissions in bulk instead. My major concern with that however, is that I will need to keep the data refreshed for viewing on a User Page, and my early experience with Google Docs has shown it to be quite sluggish. – 5th4x4 Apr 16 '13 at 20:12
  • That said, you have now enlightened me in regard to speed/efficiency when utilizing sheet functions vs Javascript processing. Personally I prefer coding everything, but Javascript is still new to me, so I've been relying pretty heavily on spreadsheet functions as much as possible so far. – 5th4x4 Apr 16 '13 at 20:13
  • Have a look at [What is faster: ScriptDb or SpreadsheetApp?](http://stackoverflow.com/questions/15145918/what-is-faster-scriptdb-or-spreadsheetapp), for evaluation of a number of I/O options. Any time possible, (a) use arrays and (b) write in bulk to a spreadsheet. – Mogsdad Apr 16 '13 at 20:23
  • Your explanations and examples have proven invaluable to me, so while I'm still constructing the different elements to this solution, the only "true answer" to my question will be based heavily on your input here (and elsewhere in SO). Therefore I'm going to just go ahead and accept your answer as the solution. (and I'll post the final compilation in an edit to my original post when I am fully finished). Thanks again for all of your help! – 5th4x4 Apr 18 '13 at 15:59