Google Script - Move new submissions to another sheet based on the responses

拟墨画扇 提交于 2019-11-30 19:30:22

问题


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,


回答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?




回答2:


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




回答3:


the triggers can be set by going to: then set it:



来源:https://stackoverflow.com/questions/15768569/google-script-move-new-submissions-to-another-sheet-based-on-the-responses

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