Appscript未在onFormSubmit上运行

时间:2018-03-20 19:21:11

标签: javascript google-apps-script google-sheets google-form

所以我已经查看了很多有关此问题的问题,并尝试了所有修复程序,但仍然无法运行我的代码。

我测试了它,因此它没有创建表单和方法,它在电子表格中创建表单,并且都无法更新我希望它更新的单元格。

要执行其他方法,我只需从代码中删除前3个函数,并创建一个在提交表单时调用的新触发器。

我已经测试过该函数是否能够更新正确的单元格并且正确执行。但是,当我引入表单数据元素时,由于某种原因它不能这样做。

如果有人能帮我解决这个问题,我将不胜感激!

/**
 * A special function that inserts a custom menu when the spreadsheet opens.
 */
function onOpen() {
  var menu = [{name: 'Create RSVP', functionName: 'RSVP_'}];
  SpreadsheetApp.getActive().addMenu('RSVP', menu);
}

/**
 * A set-up function
 */
function RSVP_() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Sheet1');
  setUpForm_(ss);
  ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit().create();
  ss.removeMenu('RSVP');
}

/**
 * Creates a Google Form that allows students to RSVP
 *
 */
function setUpForm_(ss) {
  var form = FormApp.create('RSVP Form');
  form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
  form.addTextItem().setTitle('First Name').setRequired(true);
  form.addTextItem().setTitle('Last Name').setRequired(true);
  form.addTextItem().setTitle('Email').setRequired(true);
  form.addMultipleChoiceItem().setTitle('Are you still interested in attending?')
          .setChoiceValues(['Yes','No']).setRequired(true);
}

/**
 * Updates RSVP cell
 *
 * @param {Object} e The event parameter for form submission to a spreadsheet;
 *     see https://developers.google.com/apps-script/understanding_events
 */
function onFormSubmit(e) {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Sheet1');
  var range = sheet.getDataRange();
  var values = range.getValues();
  for (var i = 1; i < values.length; i++) {
    var session = values[i];
    var title = session[2];
    if (e.namedValues['Email'][0] && e.namedValues['Email'][0] == title) {
      sheet.getRange(i+1, 5, 1, 1).setValue([[e.namedValues['Are you still interested in attending?'][0]]])

    }
  }
  var sheet = ss.getSheetByName('Sheet2');
  var nextRow = sheet.getLastRow()+1;
  sheet.getRange(nextRow, 1, 1, 3).setValues([[e.namedValues['First Name'][0], e.namedValues['Last Name'][0], e.namedValues['Email'][0], e.namedValues['Are you still interested in attending?'][0]]]);
}

1 个答案:

答案 0 :(得分:0)

试试这个:

function onFormSubmit(e) {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Sheet1');
  var range = sheet.getDataRange();
  var values = range.getValues();
  for (var i = 1; i < values.length; i++) {
    if (e.namedValues['Email'] == values[i][2]) {
      sheet.getRange(i+1, 5, 1, 1).setValue(e.namedValues['Are you still interested in attending?'])
    }
  }
}