Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 可安装触发器在表单提交时随机触发多次_Google Apps Script_Google Sheets_Google Forms_Google Workspace - Fatal编程技术网

Google apps script 可安装触发器在表单提交时随机触发多次

Google apps script 可安装触发器在表单提交时随机触发多次,google-apps-script,google-sheets,google-forms,google-workspace,Google Apps Script,Google Sheets,Google Forms,Google Workspace,我有一个谷歌表单,它将填写的数据提交到谷歌表单。在可安装触发器的帮助下(每当提交表单时触发),我通过在新行中添加此数据,成功地将最近的一行复制到新的工作表中。但是,有时触发器会随机触发多次,因此会将两行或更多行的值完全相同的行追加到另一个工作表中。我确信触发器是问题所在,因为我在触发器的G Suite Developer hub概览页面中发现了触发器多次触发的证据。 还有谁遇到过同样的问题,或者知道什么样的解决办法会奏效 编辑: 这是完整的代码。我有两张表格,其中一张叫做“FormRespons

我有一个谷歌表单,它将填写的数据提交到谷歌表单。在可安装触发器的帮助下(每当提交表单时触发),我通过在新行中添加此数据,成功地将最近的一行复制到新的工作表中。但是,有时触发器会随机触发多次,因此会将两行或更多行的值完全相同的行追加到另一个工作表中。我确信触发器是问题所在,因为我在触发器的G Suite Developer hub概览页面中发现了触发器多次触发的证据。 还有谁遇到过同样的问题,或者知道什么样的解决办法会奏效

编辑: 这是完整的代码。我有两张表格,其中一张叫做“FormResponse”,表格输入会自动复制。第二张表叫做“Checkliste”,每当收到表单响应时,就会触发一个可安装的触发器。然后它触发函数“insertIntoChecklist()”。我有第二个“edited()”的可安装触发器。该方法尚未实现。但是,将来需要在用户编辑“Checkliste”表时触发一个方法

可安装触发器在G套件开发者中心内设置:

函数编辑(){
var sheet=SpreadsheetApp.getActiveSheet();
开关(sheet.getSheetName()){
案例“Checkliste”:
//checkIfFinished();-->方法将在稍后实现
打破
违约:
log(“onEdit方法中的未知工作表名称!”);
}
}
//方法插入检查表,具体取决于团队
函数插入清单(){
//电子表格
var ss=SpreadsheetApp.getActiveSpreadsheet();
//FormResponse
var formResponse=ss.getSheetByName(“formResponse”);
var lastRowFormResponse=formResponse.getLastRow();
//响应值
var prename=formResponse.getRange(lastRowFormResponse,2.0).getValue();
var姓氏=formResponse.getRange(lastRowFormResponse,3.0).getValue();
var team=formResponse.getRange(lastRowFormResponse,4.0).getValue();
var date=formResponse.getRange(lastRowFormResponse,5.0).getValue();
//检查表
var检查表=ss.getSheetByName(“检查列表”);
var lastrowschecklist=checklist.getLastRow();
lastRowChecklist++;
//价值观检查表
var googleAccount=checklist.getRange(“E”+lastRowChecklist);
var phone=checklist.getRange(“F”+lastRowChecklist);
var工作站=checklist.getRange(“H”+lastRowChecklist);
//基于提交的团队创建实例化一个被调用的方法
交换机(团队){
“Immo”案:
createImmo(清单、姓名、姓氏、团队、日期、电子邮件、谷歌账户、电话、工作站);
打破
案例“IT”:
创建IT(清单、姓名、姓氏、团队、日期、电子邮件、谷歌账户、电话、工作站);
打破
违约:
检查表。附录行([“未检测到团队]);
}
}
var硬件类型={
winPC:“-Windows PC\n”,
iMac:“-iMac\n”,
};
var phoneTypes={
硬电话:“-hardphone\n”,
softphone:“-softphone\n”
};
函数createImmo(检查表、姓名、姓氏、团队、日期、电子邮件、谷歌账户、电话、工作站){
检查表。附录([姓名、姓氏、团队、日期]);
addCheckbox(googleAccount);
addCheckbox(电话);
addNote(电话、电话类型、软电话);
addCheckbox(工作站);
addNote(工作站、硬件etypes.winLap);
}
函数createIT(检查表、姓名、姓氏、团队、日期、电子邮件、谷歌账户、电话、工作站){
检查表。附录([姓名、姓氏、团队、日期]);
addCheckbox(googleAccount);
addCheckbox(电话);
addNote(电话、电话类型、硬电话);
addCheckbox(工作站);
setNotAvailableCell(检查表);
}
函数addCheckbox(checkboxRange){
var enforceCheckbox=SpreadsheetApp.newDataValidation();
enforceCheckbox.requireCheckbox();
enforceCheckbox.setAllowInvalid(false);
enforceCheckbox.build();
setDataValidation(enforceCheckbox);
}
函数addNote(范围,注释){
范围。设置注释(注释);
}
功能设置NotAvailableCell(检查表){
var rowRange=checklist.getRange(checklist.getLastRow(),1,1,
checklist.getLastColumn());
var values=rowRange.getValues();
对于(var i=0;我希望问题的主题是:包含一个与问题重复的问题。寻求调试帮助的问题(“为什么此代码不能按我希望的方式工作?”)必须包括:(1)所需的行为,(2)特定的问题或错误,以及(3)在问题本身中复制该问题所需的最短代码。另请参阅:,并请将问题放在主题上:包括一个复制该问题的问题。寻求调试帮助的问题(“为什么此代码不能按我希望的方式工作?”)必须包括:(1)所需的行为,(2)特定的问题或错误以及(3)在问题本身中复制它所需的最短代码。另请参见:,和
function edited() {
  var sheet = SpreadsheetApp.getActiveSheet();

  switch(sheet.getSheetName()){
    case "Checkliste":
      //checkIfFinished(); --> Method will be implemented later
      break;
    default:
      Logger.log("Unknown Sheet name in onEdit method!");
  }
}

// Method to insert into the Checklist sheet depending on the team
function insertIntoChecklist() {
  // Spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // FormResponse
  var formResponse = ss.getSheetByName("FormResponse");
  var lastRowFormResponse = formResponse.getLastRow();

  // Values FormResponse
  var prename = formResponse.getRange(lastRowFormResponse, 2.0).getValue();
  var surname = formResponse.getRange(lastRowFormResponse, 3.0).getValue();
  var team = formResponse.getRange(lastRowFormResponse, 4.0).getValue();
  var date = formResponse.getRange(lastRowFormResponse, 5.0).getValue();

  // Checklist
  var checklist = ss.getSheetByName("Checkliste");
  var lastRowChecklist = checklist.getLastRow();
  lastRowChecklist++;
  // Values Checklist
  var googleAccount = checklist.getRange("E" + lastRowChecklist);
  var phone = checklist.getRange("F" + lastRowChecklist);
  var workstation = checklist.getRange("H" + lastRowChecklist);

  // Based on the submitted team create instantiate a method that gets evoked
  switch(team){
    case "Immo":
      createImmo(checklist, prename, surname, team, date, email, googleAccount, phone, workstation);
      break;
    case "IT":
      createIT(checklist, prename, surname, team, date, email, googleAccount, phone, workstation);
      break;
    default:
      checklist.appendRow(["no team detected"]);
  }
}


var hardwareTypes = {
  winPC : "- Windows PC\n",
  iMac:  "- iMac\n",
};

var phoneTypes = {
  hardphone : "- Hardphone\n",
  softphone : "- Softphone\n"
};

function createImmo(checklist, prename, surname, team, date, email, googleAccount, phone, workstation) {
  checklist.appendRow([prename, surname, team, date]);
  addCheckbox(googleAccount);
  addCheckbox(phone);
  addNote(phone, phoneTypes.softphone);
  addCheckbox(workstation);
  addNote(workstation, hardwareTypes.winLap);
}

function createIT(checklist, prename, surname, team, date, email, googleAccount, phone, workstation) {
  checklist.appendRow([prename, surname, team, date]);
  addCheckbox(googleAccount);
  addCheckbox(phone);
  addNote(phone, phoneTypes.hardphone);
  addCheckbox(workstation);
  setNotAvailableCell(checklist);
}

function addCheckbox(checkboxRange) {
  var enforceCheckbox = SpreadsheetApp.newDataValidation();
  enforceCheckbox.requireCheckbox();
  enforceCheckbox.setAllowInvalid(false);
  enforceCheckbox.build();
  checkboxRange.setDataValidation(enforceCheckbox);
}

function addNote(range, note) {
  range.setNote(note);
}

function setNotAvailableCell(checklist) {
  var rowRange = checklist.getRange(checklist.getLastRow(), 1, 1, 
checklist.getLastColumn());
  var values = rowRange.getValues();

  for (var i = 0; i <= 0; i++){
    for (var j = 4; j < values[i].length; j++) {
      var currentValue = rowRange.getCell(i+1,j+1);
      var currentRange = 
checklist.getRange(currentValue.getRow(),currentValue.getColumn());

      if (values[i][j] !== false && values[i][j] !== true) {
        currentRange.setValue("n/a");
        currentRange.setBackground("#A9A9A9");
        currentRange.setHorizontalAlignment("center");
      }
    }
  } 
}