在Google表单提交触发器上获取targetCell错误

时间:2014-08-26 18:15:10

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

我正在尝试创建唯一的票号。每个表单提交后但收到以下错误。

TypeError: Cannot read property "range" from undefined. (line 3, file "Code")

这是完整的脚本。

function myFunction() {
  // Generates a unique ticket number for every form submission.
  function onFormSubmit(e) {
    var targetCell = e.range.offset(0, e.range.getNumColumns(), 1, 1);

    // Get a public lock on this script, because we're about to modify a shared resource.
    var lock = LockService.getPublicLock();
    // Wait for up to 30 seconds for other processes to finish.
    lock.waitLock(30000);

    var ticketNumber = Number(ScriptProperties.getProperty('lastTicketNumber')) + 1;
    ScriptProperties.setProperty('lastTicketNumber', ticketNumber);

    // Release the lock so that other processes can continue.
    lock.releaseLock();

    targetCell.setValue(ticketNumber);
  }
}

1 个答案:

答案 0 :(得分:0)

您收到该错误是因为在您调用e时未定义事件对象onFormSubmit(e)。这是调试时的典型情况,并且已从编辑器/调试器中调用参数化函数。

对于您的测试,请使用How can I test a trigger function in GAS?

中的技术模拟事件

(您打算将onFormSubmit(e)包裹在myFunction()内吗?我想不会。)