Appending initial timestamp from Google Form to end of record in order to permanently store create date onFormSubmit

穿精又带淫゛_ 提交于 2019-12-06 12:39:55

问题


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

回答1:


Change this:

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

To:

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

Right now, it's a range.



来源:https://stackoverflow.com/questions/36437888/appending-initial-timestamp-from-google-form-to-end-of-record-in-order-to-perman

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!