1

I need to be able to store (by appending to the record) the create date of google form responses where the initial timestamp does not get overwritten when the form is edited. This form is Hosted on Google and is within a Google Apps for Business environment.

This information exists but at some point but gets dumped on form update. It seems like this would have a wide application to people using Google Forms that are edited after their creation for analytical purposes.

This Google Docs Forum explains a situation quite similar to mine but didn't include the code they used.

I have dug through the google developer site and determined that the best way I should implement this function would be to use a onFormSubmit trigger on the response spreadsheet rather than the form itself since the field I want to reference (Timestamp) isn't selectable until written to the google sheet and because I want to store the result in a new column in the spreadsheet anyway.

I tried referencing this solution and modifying for my purpose as it was the closest code snippet which I found to the problem I am trying to solve.

Below is my commented code. Although, there are things I still need to address such as my if else and my hardcoding the createDateColumn, I was able to get this to run without these (using run menu in Google App Script linked to spreadsheet) but the cell it should be writing the timestamp to for some reason now has a value of "Range" after the script was executed so I assume I am setting the value incorrectly.

I also tried adding a new entry into the form however, the onFormSubmit didn't execute on its own so there may be other unaddressed issues as well.

Any help is appreciated. My coding skills are a little rusty.

    function onFormSubmit(e) 
{
  var ss = SpreadsheetApp.getActiveSheet();

  var lastRowInx = ss.getLastRow(); // Get the row number of the last row with content
  var createDateColumn = 50; //CreateDateColumn is currently in AX (Column 50) Possibly find named range since hardcoded isn't ideal. LastColumn doesn't work because some responses not required

//  if () condition should be ss.getRange(lastRowInx, createDateColumn) == ""; so that subsequent edits to Google Form which trigger timestamp updates don't overwrite original create date
  //{
    var timestamp = ss.getRange(lastRowInx, 1); // Get timestamp entry
    var createDate = ss.getRange(lastRowInx, createDateColumn);
    createDate.setValue(timestamp);
  //}
  //else {} //should be do nothing
}

****UPDATE**** It works! Thanks for your help @Sandy.

To fix my other issues I mentioned previously, I also modularized the code as I am doing a few other tasks onFormSubmit and I wanted to be able to use the createDate in another function. Note: I have removed the other functions from the code snippet as they don't apply to the question.

I also used getMaxColumns() instead of hardcoding the column number.

Here is the updated code for future user's use

function onFormSubmit(e) 
{
 var ss = SpreadsheetApp.getActiveSheet();
 var lastRowInx = ss.getLastRow(); // Get the row number of the last row with content
 var createDateColumn = ss.getMaxColumns(); //CreateDateColumn is currently in AX (Column 50) which is the last/max column position

  var createDate = setCreateDate(ss, lastRowInx, createDateColumn);
}//This is the end of onFormSubmit
function setCreateDate(ss, lastRowInx,createDateColumn) // Stores the create date timestamp in Column AV on the backend
{
  if (ss.getRange(lastRowInx, createDateColumn).getValue() == "") // so that subsequent edits to Google Form which trigger timestamp updates don't overwrite original create date
  {
    var timestamp = ss.getRange(lastRowInx, 1).getValue(); // Get timestamp entry which is currently in A (Column 1)
    var setDate = ss.getRange(lastRowInx, createDateColumn);
    var createDate = new Date(timestamp);
    setDate.setValue(createDate).setNumberFormat("MM/dd/yyyy hh:mm:ss");
  }
  else {} //should do nothing
  return createDate;
}//This is the end of setCreateDate function
SavageTiner
  • 35
  • 2
  • 8
  • Adding event trigger in Resources menu resolved it not triggering. And @Sandy-good 's resolution got the value to store. Thanks (Sometimes it is something that seems so obvious in retrospect.) However, formatting on column preserving date time not maintained. Although it says it is still selected (and the information is there) it doesn't show time unless the formatting is reapplied manually. Any thoughts? – SavageTiner Apr 06 '16 at 21:21
  • You can set the formatting for an entire column by clicking on the column heading to highlight the entire column, and then set the data format. I'm not sure if this will solve your problem, but that was my first idea. – Alan Wells Apr 06 '16 at 21:31
  • Yeah I have done that @Sandy. That is how I know when the script puts the new value it that it is not respecting the format. It does conform if I reapply the formatting to the cell or the column but it doesn't by default. I'll try some more googling. If not it isn't the worst because the data is still there just a weird by-product. – SavageTiner Apr 07 '16 at 16:12
  • You can set the format in a cell. [Apps Script documentation](https://developers.google.com/apps-script/reference/spreadsheet/range#setnumberformatnumberformat) It's also possible to copy a format: [Link - Copy a Format](https://developers.google.com/apps-script/reference/spreadsheet/range#copyformattorangegridid-column-columnend-row-rowend) – Alan Wells Apr 07 '16 at 16:36
  • It works! Here is the code for future user's use – SavageTiner Apr 08 '16 at 15:43

1 Answers1

1

Change this:

var timestamp = ss.getRange(lastRowInx, 1);

To:

var timestamp = ss.getRange(lastRowInx, 1).getValue();

Right now, it's a range.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • I am not sure if it is actually tagging you. Thank you for your help. Any ideas on the remaining formatting issue (See update comment above) – SavageTiner Apr 06 '16 at 21:22