1

我正在使用 GAS 将数据写入电子表格,除了每次保存 TWICE 记录的部分外,一切都按预期工作。我不认为客户端或服务器功能会触发两次,所以它可能是我错过的其他东西。任何见解将不胜感激。谢谢!

客户端功能:

//save data
function savedata() {
  var data = $('#dataSetter');
  var startTime = data.find('#startTime').val();
  var firstName = data.find('#firstName').val();
  var lastName = data.find('#lastName').val();
  var phone = data.find('#phone').val();
  var email = data.find('#email').val();
  var obj = $(this).parent();
  var weekDate = getSuffix(obj.attr('id'));
      weekDate = weekDate.replace('-', '/');
  var year = getSuffix(obj.attr('class'));
  var date = weekDate+'/'+year;

  google.script.run.withSuccessHandler(dataSaved).setdata(office, firstName, lastName, phone, email, date, startTime);
  google.script.run.withFailureHandler(onFailure).setdata(office, firstName, lastName, phone, email, date, startTime);
}

服务器功能:

//saves data
function setdata(office, firstName, lastName, phone, email, date, startTime) {
  var dataSheet = dataBook.getSheets()[0];
  var membersSheet = membersBook.getSheets()[0];
  var membersLastRow = membersSheet.getLastRow();

  var lastmemberID = membersSheet.getRange('A'+membersLastRow).getValue();

  var newmemberID = memberID(lastmemberID);

  membersSheet.appendRow([newmemberID, lastName, firstName, phone, email]);
}
4

2 回答 2

2

您正在对 setdata 函数进行两次调用,因此应该会出现两次输入。尝试这个:

google.script.run.withSuccessHandler(dataSaved).withFailureHandler(onFailure).setdata(office, firstName, lastName, phone, email, date, startTime);

代替:

google.script.run.withSuccessHandler(dataSaved).setdata(office, firstName, lastName, phone, email, date, startTime);
  google.script.run.withFailureHandler(onFailure).setdata(office, firstName, lastName, phone, email, date, startTime);
于 2013-04-22T19:48:09.800 回答
0

我也曾多次经历过 tbsi 错误,尽管只是偶尔发生。它插入的行数是我要求的两倍。我用锁保护了所有代码(锁服务,你也应该使用它,因为你使用 getastrow 等)所以我知道代码没有并行运行。

于 2013-04-22T14:31:42.080 回答