3

I'm trying to create a script that will take a new form response and move it to another sheet based on the information submitted. For example, let's say the form has two answer choices A, B. The spreadsheet has three sheets; Form Responses, Sheet A, Sheet B. If someone submits the form and selects A, I need that new row to be moved from "Form Responses" to "Sheet A." I found someone else's script that does exactly this but using the OnEdit function. I cannot figure out how to modify this script to work when a new form response is submitted.

function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

if(s.getName() == "Form Responses" && r.getColumn() == 2 && r.getValue() == "A") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet A");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
  }
}

I used the installable triggers and replaced the OnEdit function with onFormSubmit but that doesn't work. I'd really appreciate it if anyone could help me with this.

Thanks,

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user2236706
  • 31
  • 1
  • 2

4 Answers4

1

To achieve what you want, you will need to:

  1. Create a function write_to_new_sheet that we'll use in a trigger function whenever a new response hits the form. This function will take the form response as an event object e:
function write_to_new_sheet(e){
  let responses = e.response.getItemResponses()
  let new_row = get_new_response_data_as_row(responses)
  let sheet_to_write = SpreadsheetApp.openById('your spreadsheet id').getSheetByName('sheet A') // or 'sheet B', you can set this dynamically by checking the new_row, corresponding to the response as a gsheet row
  write_values_in_first_row(sheet_to_write, new_row)
}

this are the auxiliary functions to write_to_new_sheet:

function get_new_response_data_as_row(responses){
  let new_row = []
  responses.forEach(response => {
     new_row.push(response.getResponse())
  })
  return new_row
}

function write_values_in_first_row(sheet, new_row_values){
  let row_to_write_from = 2 // assuming you have a header
  let sheet_with_new_row = sheet.insertRowBefore(row_to_write_from)
  let number_of_rows = 1
  let number_of_columns = new_row_values.length
  let range = sheet_with_new_row.getRange(row_to_write_from, 1, number_of_rows, number_of_columns)
  let results =range.setValues([new_row_values])
  return new_row_values
}

  1. Set up an installable trigger that works whenever you submit a new response to the form:
function setup_write_to_new_sheet_on_form_submit(){
  ScriptApp.newTrigger('write_to_new_sheet')
  .forForm('your form id goes here')
  .onFormSubmit()
  .create();
}
  1. Run the above function once, to set up the trigger.
  2. try submitting a new response on the form, and check the changes in the sheets you want it to be written.
ihojmanb
  • 452
  • 8
  • 17
0

Try something a little less broad in your comparing of variables,, For instance the sheet that submissions are sent to is a constant and already address.

function formSubmission() {
 var s = SpreadsheetApp.getActiveSheet();
 var data = range.getValues(); // range is a constant that always contains the submitted answers
 var numCol = range.getLastColumn();
 var row = s.getActiveRow;
 var targetinfo = s.getRange(row,(Yourcolumn#here).getValue);
    if(targetinfo() == "Desired Sheet Name") {
      var targetSheet = ss.getSheetByName("Sheet A");
      var targetrow = targetSheet.getLastrow()+1);
      var Targetcol = numCol();
      targetSheet.getRange(targetrow,1,1,Targetcol).setValues(data);
     }
  }

I didn't test that but hopefully it helps look up Event Objects in the developer guide i just recently found it and it clarifies a lot

0

the triggers can be set by going to: enter image description here then set it: enter image description here

minky_boodle
  • 311
  • 3
  • 11
-1

I have a spreadsheet that collects the form submissions and then has extra sheets that have filtered out these submission based on the answers. All this is just with formulas. Could that do the trick also?

J Steurs
  • 151
  • 1
  • 7
  • 1
    To make your answer useful specify further how your solution works and how it would work for the problem presented. Think about partial screenshots, which formulas you use and how... thank you for your contribution! – K_B Apr 03 '13 at 13:11
  • I my case Formdata catches the submissions. The formula in the extra sheets could be in cel B2 something like =QUERY( 'Formdata'!A2:Z ; "Select * where B='aswerB' " ; 0 ) followed by the cell underneath with formula =CONTINUE(B2, 2, 1) – J Steurs Apr 03 '13 at 13:29
  • Thank you for your suggestion. The reason I wanted to do a script is because there will be other people that will eventually modify the data that was submitted. With a formula, the data will be in multiple places so we would be required to change the original data in the sheet which contains the submissions and also the additional sheet that collects the data (since it will have additional fields that will be filled out post submission). Hope that makes sense. – user2236706 Apr 03 '13 at 18:08
  • But with the formulas, when the submitted data is changed, the other sheets will recalculate and show adjusted data. Most biggest problem you will face is that after you let people mess with the submitted data your form submissions will behave unpredictable.Best practice is always to protect this sheet and let people work on copied data only. – J Steurs Apr 04 '13 at 12:42
  • Well the script would copy the submitted data to another sheet so the form submission sheet wouldn't be touched (I would hide it). If I use a formula and protect the form submisisons, I would never be able to modify the data that was submitted even on a new sheet since the query formula will always override whatever change is made with the original data. – user2236706 Apr 04 '13 at 14:55
  • What if you copy the last row that is triggered on form submission to the other data sheet, and from there on you use the formulas. – J Steurs Apr 04 '13 at 19:12
  • That's what I wanted the script to do :) – user2236706 Apr 05 '13 at 13:59